# Implementing a full fence

Episode 34 of my ongoing series will be slightly delayed because I spent the time on the weekend I normally spend writing instead rebuilding one of my backyard fences.

I forgot to take a before picture, but believe me, it was ruinous when I bought the place in 1997 and to the point in 2020 where it was actually falling to pieces in a stiff breeze.

I replaced it with an identical design and materials:

…and divided and re-homed some sixteen dozen irises in the process.

It’s nice implementing something that requires no typing every now and then.

# Life, part 33

Last time in this series we learned about the fundamental (and only!) data structure in Gosper’s algorithm: a complete quadtree, where every leaf is either alive or dead and every sub-tree is deduplicated by memoizing the static factory.

Suppose we want to make this 2-quad, from episode 32:

This is easy enough to construct recursively:

```Quad q = Make(
Empty(1), // NE

Since Make is memoized, the NW and SW corners will be reference-equal.

But suppose we had this quad in hand and we wanted to change one of the cells from dead to alive — how would we do that? Since the data structure is immutable, a change produces a new object; since it is persistent, we re-use as much of the old state as possible. But because we re-use quads arbitrarily often, a quad cannot know its own location.

The solution is: every time we need to refer to a specific location within a quad, we must also say what the coordinates are in the larger world of the lower left corner cell of that quad.

Let’s start with some easy stuff. Suppose we have a quad (this), the coordinate of its lower-left corner, and a point of interest (p). We wish to know: is p inside this quad or not?

(Source code for this episode is here.)

```private bool Contains(LifePoint lowerLeft, LifePoint p) =>
lowerLeft.X <= p.X && p.X < lowerLeft.X + Width &&
lowerLeft.Y <= p.Y && p.Y < lowerLeft.Y + Width;```

Easy peasy. This enables us to implement a getter; again, this is a method of Quad:

```// Returns the 0-quad at point p
private Quad Get(LifePoint lowerLeft, LifePoint p)
{```

```  if (!Contains(lowerLeft, p))

The point is inside the quad. Did we find the 0-quad we’re after? Return it!

```  if (Level == 0)
return this;```

We did not find it, but it is around here somewhere. It must be in one of the four children, so figure out which one and recurse. Remember, we’ll need to compute the lower left corner of the quad we’re recursing on.

```  long w = Width / 2;
if (p.X >= lowerLeft.X + w)
{
if (p.Y >= lowerLeft.Y + w)
return NE.Get(new LifePoint(lowerLeft.X + w, lowerLeft.Y + w), p);
else
return SE.Get(new LifePoint(lowerLeft.X + w, lowerLeft.Y), p);
}
else if (p.Y >= lowerLeft.Y + w)
return NW.Get(new LifePoint(lowerLeft.X, lowerLeft.Y + w), p);
else
return SW.Get(lowerLeft, p);
}
}```

Once we’ve seen the getter, it’s easier to understand the setter. The setter returns a new Quad:

```private Quad Set(LifePoint lowerLeft, LifePoint p, Quad q)
{
Debug.Assert(q.Level == 0);```

If the point is not inside the quad, the result is no change.

```  if (!Contains(lowerLeft, p))
return this;```

The point is inside the quad. If we are changing the value of a 0-quad, the result is the 0-quad we have in hand.

```if (Level == 0)
return q;```

We need to recurse; you might think that the setter logic is going to be a mess right now, but actually it is very straightforward. Since setting a point that is not in range is an immediate identity, we can just recurse four times!

```long w = Width / 2;
return Make(
NW.Set(new LifePoint(lowerLeft.X, lowerLeft.Y + w), p, q),
NE.Set(new LifePoint(lowerLeft.X + w, lowerLeft.Y + w), p, q),
SE.Set(new LifePoint(lowerLeft.X + w, lowerLeft.Y), p, q),
SW.Set(lowerLeft, p, q)) ;
}```

That’s the internal logic for getting and setting a 0-quad inside a larger quad. I’ll also add public get and set methods that are just wrappers around these; the details are not particularly interesting.

What about drawing the screen? We haven’t talked about it for a while, but remember the interface that we came up with way back in the early episodes: the UI calls the Life engine with the screen rectangle in Life grid coordinates, and a callback that is called on every live cell in that rectangle:

` void Draw(LifeRect rect, Action<LifePoint> setPixel);`

The details of “zooming in” to draw the pixels as squares instead of individual pixels was entirely handled by the UI, not by the Life engine, which should make sense.

We can quickly determine whether a given quad is empty, and therefore we can optimize the drawing engine to skip over all empty quads without considering their contents further, and that’s great. But there is another possibility here: because we can determine quickly if a quad is not empty, we could implement “zooming out”, so that every on-screen pixel represented a 1-quad or a 2-quad or whatever; we can zoom out arbitrarily far.

Let’s implement it! I’m going to add a new method to Quad that takes four things:

• What is the coordinate address of the lower left corner of this quad?
• What rectangle, in Life coordinates, are we drawing?
• The callback
• What is the level of the smallest quad we’re going to draw? This is the zoom factor.
```private void Draw(
LifePoint lowerLeft,
LifeRect rect,
Action<LifePoint> setPixel,
int scale)
{
Debug.Assert(scale >= 0);```

If this quad is empty then that’s easy; there’s nothing to draw.

```  if (IsEmpty)
return;```

The quad is not empty. If this quad does not overlap the rectangle, there’s nothing to draw. Unfortunately I chose to make the rectangle be “top left corner, width, height” but we are given the bottom left corner, so I have a small amount of math to do.

```  if (!rect.Overlaps(new LifeRect(
lowerLeft.X, lowerLeft.Y + Width - 1, Width, Width)))
return;```

(“Do these rectangles overlap?” is of course famously an interview problem; see the source code for my implementation.)

If we made it here we have a non-empty quad that overlaps the rectangle. Is this the lowest level we’re going to look at? Then set that pixel.

```  if (Level <= scale)
{
setPixel(lowerLeft);
return;
}```

We are not at the lowest level yet. Simply recurse on the four children; if any of them are empty or not overlapping, they’ll return immediately without doing more work.

```  long w = Width / 2;
NW.Draw(new LifePoint(lowerLeft.X, lowerLeft.Y + w), rect, setPixel, scale);
NE.Draw(new LifePoint(lowerLeft.X + w, lowerLeft.Y + w), rect, setPixel, scale);
SE.Draw(new LifePoint(lowerLeft.X + w, lowerLeft.Y), rect, setPixel, scale);
SW.Draw(lowerLeft, rect, setPixel, scale);
}```

That’s the internal logic; there is then a bunch of public wrapper methods around it that I will omit, and a new interface IDrawScale. We now have enough gear that we can start implementing our actual engine:

```sealed class Gosper : ILife, IReport, IDrawScale
{
public Gosper()
{
Clear();
}
public void Clear()
{
cells = Empty(9);
}
public bool this[LifePoint p]
{

That’s all straightforward. But what about the setter? We’ve made a 9-quad, but what if we try to set a point outside of it? Fortunately it is very cheap to expand a 9-quad into a 10-quad, and then into an 11-quad, and so on, as we’ve seen.

```    set
{
while (!cells.Contains(p))
cells = cells.Embiggen();
cells = cells.Set(p, value ? Alive : Dead);
}
}```

The implementation of Embiggen is just what you would expect:

```public Quad Embiggen()
{
Debug.Assert(Level >= 1);
if (Level >= MaxLevel)
return this;
Quad q = Empty(this.Level - 1);
return Make(
Make(q, q, NW, q), Make(q, q, q, NE),
Make(SE, q, q, q), Make(q, SW, q, q));
}```

And the rest of the methods of our Gosper class are uninteresting one-liners that just call the methods we’ve already implemented.

I’ve updated the UI to support “zoom out” functionality and created a Life engine that does not step, but just displays a Quad. Previously we could display a zoomed-in board:

or one pixel per cell:

But now we can display one pixel per 1-quad:

or one pixel per 2-quad, or whatever; we can zoom out arbitrarily far. If any cell in the quad is on, the pixel is on, which seems reasonable. This will let us much more easily visualize patterns that are larger than the screen.

Next time on FAIC: How do we step a quad forward one tick?

# Approximate results may vary

Part 33 of my ongoing series is coming but I did not get all the code written that I wanted to this week, so it will be delayed. In the meanwhile:

Living in Canada as a child, of course I grew up learning the metric system (with some familiarity with the imperial and US systems of course). If you want to know how many milliliters of liquid a cubic box holds, you just compute the volume of the box in cubic centimeters and you’re done, because a milliliter is by definition the same volume as a cubic centimeter.

Despite having lived in Seattle for over 25 years, I still sometimes do not have an intuitive sense of conversions of US units; for a particular project I knew that I needed an amount of liquid equal to about the volume of a two-inch cube, but the bottle was measured in fluid ounces. Fortunately we have this operation in every browser:

Thanks Bing for those important eight digits after the decimal place there, manufactured from the zero digits after the decimal place of the input. For context, that extra 0043 on the end represents a volume equivalent to roughly the size of a dozen specs of microscopic dust.

But the punchline that made me LOL was “for an approximate result, divide the volume by 1.8046875”, because of course when I am quickly approximating the volume of eight cubic inches in fluid ounces, the natural operation that immediately comes to mind is divide by 1.8046875.

I have some questions.

That was Bing; what does Google do with the same query?

OK, that’s an improvement in that the amount of precision is still unnecessary, but not outright absurd. But the other problems are all the same.

Why division? Maybe it is just me, but I find division by uneven quantities significantly harder mental arithmetic than multiplication; assuming we want the over-precision, would it not be better to say “for an approximate result, multiply the volume by 0.5541125″?

Why say “to approximate…” and then give an absurd amount of precision in the conversion factor? Approximation is by definition about deliberately not computing an over-precise value.

Surely the right way to say this is “for an approximate result, multiply the volume by 5/9” or even better, “divide by two“. When I saw “divide by 1.8046875” the first thing I thought after “wow that’s so over-precise” was “1.8 is 18/10 is 9/5, so multiply by 5/9“.

I’m going to get there eventually; software can shorten that journey. And I’m going to remember that 5/9ths of a cubic inch is a fluid ounce much more easily than I remember to divide by 1.8046875.

Once you start to see this pattern of over-precision in conversions, it’s like the FedEx arrow: you can’t stop seeing it. Let’s ask the browser how much does an American robin weigh?

64.8 grams. An underweight robin is not 64.5 grams, and not 65.0 grams but 64.8 grams.

To be fair, it looks like this over-precision was the fault of a human author (and their editor) not thinking clearly about how to communicate the fact at hand, rather than bad software this time; if you’re converting “two and a third ounces” to grams it would be perfectly reasonable to round to 65, or even 60. (That third of an ounce is already suspect; surely “two to three ounces” is just fine.) Most odd though is that the computations are not even correct! 2 and 1/3rd ounces is 66.15 grams, and 3 ounces is 85.05 grams, making it rather mysterious where the extra few hundred milligrams went.

I was wondering how many earthworms a robin would have to eat to make up a discrepancy of 0.2 grams. A largish earthworm has got to weigh on the order of a gram, right?

Wow! (For my metric readers out there: 1.5 pounds is 680.388555 grams according to Bing.)

Again: what the heck, Bing? I did not ask for “world’s largest earthworm” or “unusually large earthworms” or even “Australian earthworms”. You know where I live, Bing. (And Google search does no better.)

For some reason I am reminded of Janelle Shane’s “AI Weirdness” tweets; the ones about animal facts are particularly entertaining. These earthworm facts at least have the benefit of being both interesting and correct, but this is hardly the useful result about normal garden-variety annelids that I wanted.

I am also reminded of my favourite animal fact: the hippopotamus can jump higher than a house. It sounds impressive until you remember that houses can’t jump at all.

Obviously all these issues are silly and unimportant, which is why I chose them for my fun-for-Friday blog. And the fact that I can type “8 cubic inches in oz” into my browser or say “how much does a robin weigh?” into a smart speaker and instantly get the answer is already a user interface triumph; I don’t want to minimize that great work. But there is still work to do! Unwarranted extra precision is certainly not the worst sort of fallacious reasoning we see on the internet, but it is one of the most easily mitigated by human-focused software design. I’d love to see improvements to these search functions that show even more attention to what the human user really needs.

# Life, part 32

All right, after that excessively long introduction let’s get into Gosper’s algorithm, also known as “HashLife” for reasons that will become clear quite soon. Since the early days of this series I’ve mostly glossed over the code that does stuff like changing individual cells in order to load in a pattern, or the code that draws the UI, but Gosper’s algorithm is sufficiently different that I’m going to dig into every part of the implementation.

The first key point to understand about Gosper’s algorithm is that it only has one data structure. One way to describe it is:

A quad is an immutable complete tree where every non-leaf node has exactly four children which we call NE, NW, SE, SW, and every leaf is either alive or dead.

However the way I prefer to describe it is via a recursive definition:

Let’s write the code!

```sealed class Quad
{
public const int MaxLevel = 60;```

Because there will be math to do on the width of the quad and I want to keep doing the math in longs instead of BigIntegers, I’m going to limit us to a 60-quad, max. Just because it is a nice round number less than the 64 bits we have in a long.

Now, I know what you’re thinking. “Eric, if we built a monitor with a reasonable pixel density to display an entire 60-quad it would not fit inside the orbit of Pluto and it would have more mass than the sun.” A 60-quad is big enough for most purposes. I feel good about this choice. However I want to be clear that in principle nothing is stopping us from doing math in a larger type and making arbitrarily large quads.

```public static readonly Quad Dead = new Quad();

```public Quad NW { get; }
public Quad NE { get; }
public Quad SE { get; }
public Quad SW { get; }```

We will need to access the level and width of a quad a lot, so I’m going to include the level in every quad. The width we can calculate on demand.

```public int Level { get; }
public long Width => 1L << Level;```

We’ll need some constructors. There’s never a need to construct 0-quads because we have both of them already available as statics. For reasons which will become apparent later, we have a public static factory for the rest.

```public static Quad Make(Quad nw, Quad ne, Quad se, Quad sw)
{
Debug.Assert(nw.Level == ne.Level);
Debug.Assert(ne.Level == se.Level);
Debug.Assert(se.Level == sw.Level);
return new Quad(nw, ne, se, sw);
}
private Quad() => Level = 0;
{
NW = nw;
NE = ne;
SE = se;
SW = sw;
Level = nw.Level + 1;
}```

We’re going to need to add a bunch more stuff here, but this is the basics.

Again I know what you’re probably thinking: this is bonkers. In QuickLife, a 3-quad was an eight byte value type. In my astonishingly wasteful implementation so far, a single 0-quad is a reference type, so it is already eight bytes for the reference, and then it contains four more eight byte references and a four byte integer that is never more than 60. How is this ever going to work?

Through the power of persistence, that’s how. As I’ve discussed many times before on this blog, a persistent data structure is an immutable data structure where, because every part of it is immutable, you can safely re-use portions of it as you see fit. You therefore save on space.

Let’s look at an example. How could we represent this 2-quad?

Remember, we only have two 0-quads and we cannot make any more. The naïve way would be to make this tree:

But because every one of these objects is immutable, we could instead make this tree, which has one fewer object allocation:

This is the second key insight to understanding how Gosper’s algorithm works: it uses a relatively enormous data structure for each cell, but it achieves compression through deduplication:

• There are only two possible 0-quads, and we always re-use them.
• There are 16 possible 1-quads. We could just make 16 objects and re-use them.
• There are 65536 possible 2-quads, but the vast majority of them we never see in a given Life grid. The ones we do see, we often see over and over again. We could just make the ones we do see, and re-use them.

The same goes for 3-quads and 4-quads and so on. There are exponentially more possibilities, and we see a smaller and smaller fraction of them in any board. Let’s memoize the Make function!

We’re going to make heavy use of memoization in this algorithm and it will have performance implications later, so I’m going to make a relatively fully-featured memoizer whose behaviour can be analyzed:

```sealed class Memoizer<A, R>
{
private Dictionary<A, R> dict;
private Dictionary<A, int> hits;
[Conditional("MEMOIZER_STATS")]
private void RecordHit(A a)
{
if (hits == null)
hits = new Dictionary<A, int>();
if (hits.TryGetValue(a, out int c))
hits[a] = c + 1;
else
}

public R MemoizedFunc(A a)
{
RecordHit(a);
if (!dict.TryGetValue(a, out R r))
{
r = f(a);
}
return r;
}
public Memoizer(Func<A, R> f)
{
this.dict = new Dictionary<A, R>();
this.f = f;
}
public void Clear(Dictionary<A, R> newDict = null)
{
dict = newDict ?? new Dictionary<A, R>();
hits = null;
}
public int Count => dict.Count;
public string Report() =>
hits == null ? "" :
string.Join("\n", from v in hits.Values
group v by v into g
select \$"{g.Key},{g.Count()}");
}```

The core logic of the memoizer is the same thing I’ve presented many times on this blog over the years: when you get a call, check to see if you’ve memoized the result; if you have not, call the original function and memoize the result, otherwise just return the result.

I’ve added a conditionally-compiled hit counter and a performance report that tells me how many items were hit how many times; that will give us some idea of the load that is being put on the memoizer, and we can then tune it.

Later in this series we’re going to need to “reset” a memoizer and optionally we’ll need to provided “pre-memoized” state, so I’ve added a “Clear” method that optionally takes a new dictionary to use.

The memoizer for the “Make” method can be static, global state so I’m going to make a helper class for the cache. (I did not need to; this could have been a static field of Quad. It was just convenient for me while I was developing the algorithm to put the memoizers in one central location.)

```static class CacheManager
{
}```

We’ll initialize it when we create our first Quad:

```static Quad()
{
}```

And we’ll redo the Make static factory:

```private static Quad UnmemoizedMake((Quad nw, Quad ne, Quad se, Quad sw) args)
{
Debug.Assert(args.nw.Level == args.ne.Level);
Debug.Assert(args.ne.Level == args.se.Level);
Debug.Assert(args.se.Level == args.sw.Level);
return new Quad(args.nw, args.ne, args.se, args.sw);
}

All right, we have memoized construction of arbitrarily large quads. A nice consequence of this fact is that all quads can be compared for equality by reference equality. In particular, we are going to do two things a lot:

• Create an arbitrarily large empty quad

We’re on a memoization roll here, so let’s keep that going and add a couple more methods to Quad, and another memoizer to the cache manager (not shown; you get how it goes.)

```private static Quad UnmemoizedEmpty(int level)
{
Debug.Assert(level >= 0);
if (level == 0)
var q = Empty(level - 1);
return Make(q, q, q, q);
}

public static Quad Empty(int level) =>
CacheManager.EmptyMemoizer.MemoizedFunc(level);

public bool IsEmpty => this == Empty(this.Level);```

The unmemoized “construct an empty quad” function can be as inefficient as we like; it will only be called once per level. And now we can quickly tell if a quad is empty or not. Well, relatively quickly; we have to do a dictionary lookup and then a reference comparison.

What then is the size in memory of an empty 60-quad? It’s just 61 objects! The empty 1-quad refers to Dead four times, the empty 2-quad refers to the empty 1-quad four times, and so on.

Suppose we made a 3-quad with a single glider in the center; that’s a tiny handful of objects. If you then wanted to make a 53-quad completely filled with those, that only increases the number of objects by 50. Deduplication is super cheap with this data structure — provided that the duplicates are aligned on boundaries that are a power of two of course.

A major theme of this series is: find the characteristics of your problem that admit to optimization and take advantage of those in pursuit of asymptotic efficiency; don’t worry about the small stuff. Gosper’s algorithm is a clear embodiment of that principle. We’ve got a space-inefficient data structure, we’re doing possibly expensive dictionary lookups all over the show; but plainly we can compress down grids where portions frequently reoccur into a relatively small amount of memory at relatively low cost.

Next time on FAIC: There are lots more asymptotic wins to come, but before we get into those I want to explore some mundane concerns:

• If portions of the data structure are reused arbitrarily often then no portion of it can have a specific location. How are we going to find anything by its coordinates?
• If the data structure is immutable, how do we set a dead cell to alive, if, say, we’re loading in a pattern?
• How does the screen drawing algorithm work? Can we take advantage of the simplicity of this data structure to enable better zooming in the UI?

# Socially distant abbreviated summer vacation

Normally this time of year I would be visiting friends and family in Canada, but obviously that’s impossible right now. Instead we took a long weekend at a rental on Bainbridge Island and strolled around some parks in a socially distant manner instead.

The rental had a balcony at tree level, so at least I got to indulge in my vacation pastime of taking pictures of birds. There were a lot of common feeder birds in the neighbourhood — house finches, house sparrows, a few different species of chickadees, that sort of thing — and I got a few shots in. This little female Rufous hummingbirb posed for glamour shots in a birch tree all day long:

(Click on any picture for a larger image)

She was vexed that a female Anna’s hummingbird kept horning in on her territory, hence her ticked-off expression there.

This super fuzzy juvenile black-capped chickadee also spent days posing:

despite being harassed all day long by the noisy gang of teenager Stellar jays on the rooftop next door.

And of course once again I was completely incapable of taking a non-blurry picture of a belted kingfisher despite many attempts.

Sigh. Better luck next time.

# Life, part 31

Today we will finish off our implementation of Hensel’s QuickLife algorithm, rewritten in C#. Code for this episode is here.

Last time we saw that adding change tracking is an enormous win, but we still have not got an O(changes) solution in time or an O(living) solution in space. What we really want to do is (1) avoid doing any work at all on stable Quad4s; only spend processor time on the active ones, and (2) deallocate all-dead Quad4s, and (3) grow the set of active Quad4s when activity reaches the current borders.

We need more state bits, but fortunately we only need a small number; so small that I’m not even going to bit twiddle them. (The original Java implementation did, but it also used some of those bits for concerns such as optimizing the display logic.) Recall that we are tracking 32 regions of the 8 Quad3s in a Quad4 to determine if they are active, stable or dead. It should come as no surprise that we’re going to do the same thing for a Quad4, and that once again we’re going to maintain state for both the even and odd cycles:

```enum Quad4State
{
Active,
Stable,
}```

```public Quad4State State { get; set; }
public Quad4State EvenState { get; set; }
public Quad4State OddState { get; set; }
public bool StayActiveNextStep { get; set; }```

The idea here is:

• All Quad4s are at all times in exactly one of three buckets: active, stable and dead. Which bucket is represented by the State property.
• If a Quad4 is active then it is processed on each tick.
• If a Quad4 is stable then we draw it but do not process it on each tick.
• If a Quad4 is dead then we neither process nor draw it, and eventually we deallocate it. (We do not want to deallocate too aggressively in case it comes back to life in a few ticks.)

How then do we determine when an active Quad4 becomes stable or dead?

• If the “stay active” flag is set, it stays active no matter what.
• The odd and even cycles each get a vote on whether an active Quad4 should become stable or dead.
• If one votes for stable and the other votes for stable or dead, it becomes stable. (Exercise: under what circumstances is a Quad4 dead on the odd cycles but stable on the even?)
• If one or both vote for active, it stays active.

How do we determine if a stable (or dead but not yet deallocated) Quad4 becomes active? That’s straightforward: if an active Quad4 has an active edge that abuts a stable Quad4, the stable one becomes active. Or, if there is no Quad4 there at all, we allocate a new one and make it active; this is how we achieve our goal of a dynamically growing board.

You might be wondering why we included a “stay active” flag. There were lots of things about this algorithm that I found difficult to understand at first, but this took the longest for me to figure out, oddly enough.

This flag means “there was activity on the edge of a neighbouring Quad4 recently”. There are two things that could go wrong in that situation that we need to prevent. First, we could have an active Quad4 that is about to become stable or dead, but it has activity on a border that should cause it to remain active for processing. Second, we could have a stable (or dead) Quad4 that has just been marked as active because there is activity on a bordering Quad4, and we need to ensure that it stays active even though it wants to go back to being stable (or dead).

The easiest task to perform is to keep each Quad4 in one of three buckets. The original implementation did so by ensuring that every Quad4 was on exactly one of three double-linked lists, and I see no reason to change that. I have, however, encapsulated the linking and unlinking into a helper class:

```interface IDoubleLink<T> where T : class
{
T Prev { get; set; }
T Next { get; set; }
}

{
public int Count { get; private set; }
public void Add(T item) { ... }
public void Remove(T item) { ... }
public void Clear() { ... }
}```

I won’t go through the implementation; it is more or less the standard double-linked list implementation you know from studying for boring technical interviews. The only unusual thing about it is that I’ve ensured that you can remove the current item from a list safely even while the list is being enumerated, because we will need to do that.

All the Quad4-level state manipulation will be done by our main class; we’ll add some lists to it:

```sealed class QuickLife : ILife, IReport
{
private int generation;```

I’ll skip the initialization code and whatnot.

We already have a method which allocates Quad4s and ensures their north/south, east/west, northwest/southeast references are initialized. We will need a few more helper functions to encapsulate some operations such as: make an existing Quad4 active, and if it does not exist, create it. Or, make an active Quad4 into a stable Quad4. They’re for the most part just updating lists and state flags:

```private Quad4 EnsureActive(Quad4 q, int x, int y)
{
if (q == null)
MakeActive(q);
return q;
}
{
q.StayActiveNextStep = true;
if (q.State == Active)
return;
else
stable.Remove(q);
q.State = Active;
}
{
Debug.Assert(q.State == Active);
active.Remove(q);
}
{
Debug.Assert(q.State == Active);
active.Remove(q);
q.State = Stable;
}```

Nothing surprising there, except that as I mentioned before, when you force an already-active Quad4 to be active, it sets the “stay active for at least one more tick” flag.

There is one more bit of list management mechanism we should consider before getting into the business logic: when and how do dead Quad4s get removed from the dead list and deallocated? The how is straightforward: run down the dead list, orphan all of the Quad4s by de-linking them from every living object, and the garbage collector will eventually get to them:

```private void RemoveDead()
{
{
if (q.S != null)
q.S.N = null;
... similarly  for N, E, W, SE, NW
}
}```

This orphans the entire dead list; the original implementation had a more sophisticated implementation where it would keep around the most recently dead on the assumption that they were the ones most likely to come back.

We have no reason to believe that this algorithm’s performance is going to be gated on spending a lot of time deleting dead nodes, so we’ll make an extremely simple policy; every 128 ticks we’ll check to see if there are more than 100 Quad4s on the dead list.

```private bool ShouldRemoveDead =>
(generation & 0x7f) == 0 && dead.Count > 100;
public void Step()
{
if (IsOdd)
StepOdd();
else
StepEven();
generation += 1;
}```

All right, that’s the mechanism stuff. By occasionally pruning away all-dead Quad4s we attain O(living) space usage. But how do we actually make this thing both fast and able to dynamically add new Quad4s on demand?

In keeping with the pattern of practice so far, we’ll write all the code twice, once for the even cycle and once, slightly different, for the odd cycle. I’ll only show the even cycle here.

Remember our original O(cells) prototype stepping algorithm:

```private void StepEven()
{
q.StepEven();
}```

We want to (1) make this into an O(changes) algorithm, (2) detect stable or dead Quad4s currently on the active list, and (3) expand the board by adding new Quad4s when the active region reaches the current edge. We also have (4) a small piece of bookkeeping from earlier to deal with:

```private void StepEven()
{
foreach (Quad4 q in active)      // (1) O(changes)
{
{
q.StepEven();
MakeOddNeighborsActive(q);   // (3) expand
}
q.StayActiveNextStep = false;  // (4) clear flag
}
}```

The first one is straightforward; we now only loop over the not-stable, not-dead Quad4s, so this is O(changes), and moreover, remember that we consider a Quad4 to be stable if both its even and odd generations are stable, so we are effectively skipping all computations of Quad4s that contain only still Lifes and period-two oscillators, which is a huge win.

The fourth one is also straightforward: if the “stay active for at least one step” flag was on, well, we made it through one step, so it can go off. If it was already off, it still is.

The interesting work comes in removing stable Quad4s, and expanding the board on the active edge. To do the former, we will need some more helpers that answer questions about the Quad4 and its neighbors; this is a method of Quad4:

```public bool EvenQuad4OrNeighborsActive =>
(S != null && S.EvenNorthEdgeActive) ||
(E != null && E.EvenWestEdgeActive) ||
(SE != null && SE.EvenNorthwestCornerActive);```

This is the Quad4 analogue of our earlier method that tells you if any of a 10×10 region is active; this one tells you if an 18×18 region is active, and for the same reason: because that region entirely surrounds the new shifted-to-the-southeast Quad4 we’re computing. We also have the analogous methods to determine if that region is all stable or all dead; I’ll omit showing them.

Let’s look at our “remove a stable/dead Quad4 from the active list” method. There is some slightly subtle stuff going on here. First, if the Quad4 definitely can be skipped for this generation because it is stable or dead, we return true. However, that does not guarantee that the Quad4 was removed from the active list! Second, remember that our strategy is to have both the even and odd cycles “vote” on what should happen:

```private bool RemoveStableEvenQuad4(Quad4 q)
{
{
q.EvenState = Active;
q.OddState = Active;
return false;
}```

If anything in the 18×18 region containing this Quad4 or its relevant neighbouring Quad4s are active, we need to stay active. We set the votes of both even and odd cycle back to active if they were different.

If nothing is active then it must be stable or dead. Is this 18×18 region dead? (Remember, we only mark it as dead if it is both dead and stable.)

```  if (q.EvenQuad4AndNeighborsAreDead)
{
if (!q.StayActiveNextStep && q.OddState == Dead)
}```

Let’s go through each line here in the consequence of the if:

• Everything in an 18×18 region is dead and stable. The even cycle votes for death.
• We know that an 18×18 region is all dead and stable; that region entirely surrounds the odd Quad4. We therefore know that the odd Quad4 will be all dead on the next tick if it is not already, so we get aggressive and set all its “region dead” bits now.
• If we’re forcing this Quad4 to stay active then it stays active; however, even is still voting for death! We’ll get another chance to kill this Quad4 on the next tick.
• By that same logic, if the odd cycle voted for death on the previous tick but the Quad4 stayed active for some reason then the odd cycle is still voting for death now. If that’s true then both cycles are voting for death and the Quad4 gets put on the dead list.

We then have nearly identical logic for stability; the only difference is that if one cycle votes for stability, it suffices for the other cycle to vote for stability or death:

```  else
{
q.EvenState = Stable;
if (!q.StayActiveNextStep && q.OddState != Active)
MakeStable(q);
}
return true;
}```

And finally: how do we expand into new space? That is super easy, barely an inconvenience. If we have just processed an active Quad4 on the even cycle then we’ve created a new odd Quad4 and in doing so we’ve set the activity bits for the 16 regions in the odd Quad4. If the south 16×2 edge of the odd Quad4 is active then the Quad4 to the south must be activated, and so on:

```private void MakeOddNeighborsActive(Quad4 q)
{
if (q.OddSouthEdgeActive)
EnsureActive(q.S, q.X, q.Y - 1);
if (q.OddEastEdgeActive)
EnsureActive(q.E, q.X + 1, q.Y);
if (c.OddSoutheastCornerActive)
EnsureActive(q.SE, q.X + 1, q.Y - 1);
}```

Once again we are taking advantage of the fact that the even and odd generations are offset by one cell; we only have to expand the board on two sides of each Quad4 during each tick, instead of all four. When we’re completing an even cycle we check for expansion to the south and east for the upcoming odd cycle; when we’re completing an odd cycle we check for expansion to the north and west for the upcoming even cycle.

It’s a little hard to wrap your head around, but it all works. This “staggered” property looked like it was going to be a pain when we first encountered it, but it is surprisingly useful; that insight is one of the really smart things about this algorithm.

There is a small amount of additional state management code I’ve got to put here and there but we’ve hit all the high points; see the source code for the exact changes if you are curious.

And that is that! Let’s take it for a spin and run our usual 5000 generations of “acorn”.

```Algorithm           time(ms) size  Mcells/s bits/cell O-time
Naïve (Optimized):   4000     8      82     8/cell     cells
Abrash (Original)     550     8     596     8/cell     cells
Stafford              180     8    1820     5/cell     change
Sparse array         4000    64      ?   >128/living   change
Proto-QuickLife 1     770     8     426     4/cell     cells
Proto-QuickLife 2     160     8    2050     4/cell     cells
QuickLife              65    20      ?      5/living*  change
```

WOW!

We have an O(changes) in time and O(living) in space algorithm that maintains a sparse array of Quad4s that gives us a 20-quad to play with, AND it is almost three times faster than Stafford’s algorithm on our benchmark!

Our memory usage is still pretty efficient; we are spending zero memory on “all dead” Quad4s with all dead neighbours. We’ve added more state to each Quad4, so now for active and stable Quad4s we’re spending around five bits per cell; same as Stafford’s algorithm. (Though as I noted in my follow-up, he did find ways to get “maintain a count of living neighbours” algorithms down to far fewer bits per cell.) I added an asterisk to the table above because of course an active or stable Quad4 will contain only 50% or fewer living cells, so this isn’t quite right, but you get the idea.

Again, it is difficult to know how to characterize “cells per second” for sparse array approaches where we have an enormous board that is mostly empty space that costs zero to process, so I’ve omitted that metric.

If you ran the code on your own machine you probably noticed that I added counters to the user interface to give live updates of the size of the active, stable and dead lists. Here’s a graph of the first 6700 generations of acorn (click on the image for a larger version.)

You can really see how the pattern evolves from this peek into the workings of the algorithm!

• We start with a small number of active Quad4s; soon small regions of stability start to appear as the pattern spreads out and leaves still Lifes and period-two oscillators in its wake.
• The number of dead Quad4s remains very small right until the first glider escapes; from that moment on we have one or more gliders shooting off to infinity. In previous implementations they hit the wall of death, but now they are creating new active Quad4s in their bow shocks, and leaving dead Quad4s in their wakes.
• The stable count steadily grows as the active region is a smaller and smaller percentage of the total. Around the 5000th generation everything except the escaped gliders is stable, and we end up with around 100 stable Quad4s and 20 active Quad4s for the gliders.
• The action of our trivial little “garbage collector” is apparent here; we throw away the trash only when there are at least 100 dead Quad4s in the list and we are on a generation divisible by 128, so it is unsurprising that we have a sawtooth that throws away a little over 100 dead Quad4s every 128 cycles.
• The blue line is proportional to time taken per cycle, because we only process active Quad4s.
• The sum of all three lines is proportional to total memory used.

That finishes off our deep dive into Alan Hensel’s QuickLife algorithm. I was quite intimidated by this algorithm when I first read the source code, but once you name every operation and reorganize the code into functional areas it all becomes quite clear. I’m glad I dug into it and I learned a lot.

Coming up on FAIC:

We’ve seen a lot of varied ways to solve the problem of simulating Life, and there are a pile more in my file folder of old articles from computer magazines that we’re not going to get to in this series. Having done this exploration into many of them and skimmed a lot more, two things come to mind.

First, so far they all feel kind of the same to me. There is some sort of regular array of data, and though I might layer object-oriented features on top of it to make the logic easier to follow or better encapsulated, fundamentally we’re doing procedural work here. We can be more or less smart about what work we can avoid or precompute, and thereby eliminate or move around the costs, but the ideas are more or less the same.

Second, every optimization we’ve done increases the amount of redundancy, mutability, bit-twiddliness, and general difficulty of understanding the algorithm.

Gosper’s algorithm stands in marked contrast.

• There is no “array of cells” at all
• The attack on the problem is purely functional programming; there is very little state mutation.
• There is no redundancy. In the Abrash, Stafford and Hensel algorithms we had ever-increasing amounts of redundant state that had to be carefully kept in sync with the board state. In Gosper’s algorithm, there is board state and nothing else.
• No attempt whatsoever is made to make individual cells smaller in memory, but it can represent grids a trillion cells on a side with a reasonable amount of memory.
• It can compute trillions of ticks per second on quadrillion-cell grids on machines that only do billions of operations per second.
• Though there are plenty of tricky details to consider in the actual implementation, the core algorithm is utterly simple and elegant. The gear that does the optimization of the algorithm uses off-the-shelf parts and completely standard, familiar functional programming techniques. There is no mucking around with fiddly region change tracking, or change lists, or bit sets, or any of those mechanisms.
• And extra bonus, the algorithm makes “zoom in or out arbitrarily far” in the UI super easy, which is nice for large boards.

This all sounds impossible. It is not an exaggeration to say that learning about this algorithm changed the way I think about the power of functional programming and data abstraction, and I’ve been meaning to write a blog about it for literally over a decade.

It will take us several episodes to get through it:

• Next time on FAIC we’ll look at the core data structure. We’ll see how we can compress large boards down to a small amount of space.
• Then we’ll closely examine the “update the UI” algorithm and see how we can get a nice new feature.
• After that we’ll build the “step forward one generation algorithm” and do some experiments with it.
• Finally, we’ll discover the key insight that makes Gosper’s algorithm work: you can enormously compress time if you make an extremely modest addition of extra space.
• We will finish off this series with an answer to a question I’ve been posing for some time now: are there patterns that grow quadratically?

# Life, part 30

Holy goodness, we are on part 30; I never expected this to go for so long and we have not even gotten to Gosper’s algorithm yet! We will finish up Hensel’s QuickLife algorithm soon I hope. Code for this episode is here. (UPDATE: There is a small defect in my implementation below which I did not discover for some time; for details see episode 36.)

And holy goodness again: it is August. In a normal year I’d be taking the month off from blogging and traveling to see my family, but thanks to criminally stupid coronavirus response, here I am, working from home. I suppose it could be a lot worse; I am glad to still have my health.

When last we left off we had computed whether each of 32 “regions” of the eight Quad3s in a Quad4 were active (they had changed at least once cell since the previous tick of the same parity), stable (no change), or dead (stable and also all dead cells).

How can we make use of this to save on time?

Let’s suppose we are currently in an even generation, call it K, and we are about to step the northwest Quad3 to get the new Quad3 and state bits for odd generation K+1. Under what circumstances could we skip doing that work? Let’s draw a diagram:

The green square is oddNW, which is what we are about to compute. If any of the light blue shaded “even” regions are marked as active then the green “odd” Quad3 in generation K+1 might be different from how it was in generation K-1. The only way to find out is to execute the step and compare.

But now suppose all the light blue shaded regions are marked as either dead or stable. Remember what this means: in generation K-1 we compared the even Quad3s that we had just computed for generation K to those we had in hand from generation K-2. If all of those cells did not change from generation K-2 to generation K on the even cycle, then generation K+1 will be the same as generation K-1 on the odd cycle, so we can skip doing all work! (Note: the “all work” is a small lie. Do you see why? We’ll come back to this point in a moment.)

What is that light blue shaded region? It is the entirety of evenNW plus the north 8×2 edge of evenSW, the west 2×8 edge of evenNE, and the northwest corner of evenSE. We have a uint that tells us with a single bit operation whether any of those regions are active, but you know what I’m like; I’m going to put it in a descriptive helper:

``` private bool EvenNorthwestOrBorderingActive =>
(evenstate & 0x08020401) != 0x08020401;```

And then a method that describes the semantics with respect to the odd generation:

```private bool OddNWPossiblyActive() =>
EvenNorthwestOrBorderingActive;```

And only then am I going to add it to our step function:

```private void StepEvenNW()
{
if (OddNWPossiblyActive())
{
oddNW = newOddNW;
}```

And presto, we just did a small number of bit operations to determine when we can avoid doing a much larger number of bit operations! That’s a win.

I said before that I lied when I said we could avoid all work; we still have some work to do here. (Though in the next episode, we’ll describe how we really, truly can avoid this work!) The problem is: the odd NW quad3 probably still has regions marked as active, and that will then cause unnecessary work to get done on the next tick. If the condition of the if statement is not met then we know that this Quad3 is either stable or dead without having to compute the next generation and compare but we still have to set the Quad3 state bits as though we had.

```  else
{
}
}```

We do not have that method yet, but fortunately it is not difficult; we need to do only the work to distinguish dead from stable. We add a method to Quad3:

```public Quad3State MakeOddStableOrDead(Quad3State s)
{
s = s.SetAllRegionsStable();
return s;
}```

Super. All that remains is to work out for each of the remaining seven step functions, what regions do we need to check for activity, then make an efficient bit operation that returns the result. For example, suppose we wish to know if the odd SW Quad3 could possibly be active this round:

```private bool OddSWPossiblyActive() =>
EvenSouthwestOrBorderingActive ||
S != null && S.EvenNorthEdge10WestActive;```

That is: if the evenSW Quad3 is active, or the 2×8 eastern edge of the evenSE is active, or the 10×2 western side of the north edge of the Quad4 to the south is active. Of course those are:

```private bool EvenSouthwestOrBorderingActive =>
(evenstate & 0x00080004) != 0x00080004;
private bool EvenNorthEdge10WestActive =>
(evenstate & 0x02000100) != 0x02000100;```

I know I keep saying this, but it is just so much more pleasant to read the code when it is written in English and the bit operations are encapsulated behind helpers with meaningful names.

Anyways, we have eight helper methods that determine whether a 10×10 region is active, and if not, then we skip stepping and instead mark the regions as stable or dead; I won’t write them all out. Let’s take it for a spin:

```Algorithm           time(ms)  ticks  size(quad)    megacells/s
Naïve (Optimized):   4000       5K      8               82
Abrash (Original)     550       5K      8              596
Stafford              180       5K      8             1820
Proto-QuickLife 1     770       5K      8              426
Proto-QuickLife 2     160       5K      8             2050
```

HOLY GOODNESS; NEW RECORD!

We are now 25 times faster than our original naïve implementation.

But wait, there’s more! We still have not quite implemented the QuickLife algorithm. There are still three problems left to solve:

• We do not yet have an O(changes) solution in time. On each tick we examine each of 256 Quad4s; we do very little work for the stable ones, we do a little bit of work for the active ones, but we are still doing work for every Quad4. Can we do no work at all for the stable or dead Quad4s? That would give us a speed win.
• We do not yet have an O(living) solution in space. An all-dead Quad4 takes up just as much space as any other. Can we deallocate all-dead Quad4s? That would give us a space win.
• We still are not taking advantage of the sparse array of Quad4s to dynamically grow the board into the 20-quad we logically have available to us; we’re trapped in a tiny little 8-quad still. Can we dynamically grow the board to support large patterns?

Next time on FAIC: Yes we can do all those things. But you know what we need? More bits to twiddle, that’s what we need.

# Life, part 29

We’ve built the foundation of the QuickLife algorithm; now let’s make it go fast. This is going to be a longer episode than usual because we have a large volume of code to get through to perform a relatively straightforward task, and we won’t get through all of it today. Code for this episode is here.

Abrash and Stafford’s algorithms both achieved significant wins by maintaining neighbour counts and updating them when something changes. Hensel’s QuickLife algorithm does not count neighbours at all! The neighbour counting has been entirely moved to the construction of the lookup table. So there cannot be any savings there.

Stafford’s algorithm achieved a significant win by tracking what changes from one tick to the next, but there is a flaw in that plan — or, maybe it would be better to say, there is an opportunity for further optimization.

Suppose we have the simplest possible always-changing grid: a single blinker.

Four cells change on every tick: two are born and two die. Stafford’s algorithm works on triples; for every tick we need to consider on the order of a dozen triples that might be changing next.

Now consider the gear we have built so far for QuickLife: we have the state of one even and one odd generation saved. Who cares whether there is a difference between the even generation and the odd generation? In the example above every even generation is the same as every other, and every odd generation is the same as every other.

Period-two oscillators are extremely common in Life, but from QuickLife’s perspective, given that it saves two generations in every Quad4, period-two oscillators could be treated as still Lifes! And remember that of course that still Lifes are also period-two oscillators; they just have the same state in both cycles.

In short: If we know that for some region of a quad4 the next even generation is the same as the previous even generation, then we can skip all computations on that region, and similarly for the odd generations. That works right up until the point where some “active” cells in a neighbouring region get close enough to the stable region to destabilize it.

It seems like there is a powerful optimization available here; how are we going to achieve it?

Let’s start by crisping up the state that we are tracking. We will be considering rectangular “regions” of a Quad3, and classifying them into three buckets:

• Active — a cell changed value from the previous generation to the next generation. That is, if we are currently in generation 3, stepping to even generation 4, then we check to see whether the region in question is the same as it was in generation 2.
• Stable — a region which is not active; every cell in the region is going to be the same in the next generation as it was in the previous generation.
• Dead — a region which is stable, and moreover, every cell in the region is dead

Notice that there is a subtlety here: a region can be entirely dead cells but is still considered active if it just recently became all dead. A region does not become classified as dead until it is both stable and all dead cells.

What regions do we care about? For odd generation Quad3s we will classify the following regions as dead, stable or active:

That is, did anything at all in these 64 cells change? and if not, are any of them alive?

• The 2×8 eastern edge:

• The 8×2 southern edge:

• And finally, the 2×2 southeastern corner:

For the even generation Quad3s, we will track the same stuff, except different. For evens, we track the state of:

• The 2×8 western edge
• The 8×2 northern edge
• The 2×2 northwest corner

That sounds like a lot of information to deal with: we’ve got three possible states for each of four regions of each of eight Quad3s in a Quad4. Of course, we’re going to deal with it by twiddling bits, and of course I am going to encapsulate every bit operation into a helper method with a sensible name.

Our bit format will be as follows.

• Each of the eight Quad3s in a Quad4 will have eight bits of state data.
• Those 64 bits will be stored in two uints, one for the evens, one for the odds, and they will be fields of Quad4.
• Bits 0-7 will be for the SE Quad3, bits 8-15 the NE, 16-23 the SW and 24-31 the NW

Pretty straightforward so far. What does each of the eight bits in the byte associated with a Quad3 mean? We consider the bits in pairs.

• bits 0 and 4 give the state of the SE (odd) or NW (even) corner
• bits 1 and 5 give the state of the south/north edge
• bits 2 and 6 give the state of the east/west edge
• bits 3 and 7 give the state of the Quad3 as a whole
• If both bits are off, then the region is active
• If both bits are on, then the region is dead
• If the lower bit is on and the higher is off, then the region is stable
• The higher bit is never turned on if the lower bit is off.

This scheme has some nice properties:

• If you want to check for “not active” you just have to look at the low bit
• If you want to check for “dead” you just have to look at the high bit
• If you want to set a region to “stable” and it is already marked “dead”, that’s OK, it stays dead
• And so on

But because I am only going to access these bits via helper functions, it really doesn’t matter what the format is so long as we get all the transitions correct.

I’m going to make a helper struct for the setters, solely to make the code easier to read. (Why just the setters? We will see in the next episode. Foreshadowing!) We will never store one of these, so there is no reason to make it wrap a byte instead of a uint; no reason to make the runtime truncate it on each operation.

```struct Quad3State
{
public Quad3State(int b) => this.b = (uint)b;
public Quad3State(uint b) => this.b = b;
public static explicit operator uint(Quad3State s) => s.b;
}```

A few things to notice here:

• Calling a “set stable” helper keeps the “dead” bit set if it is already set, which is what we want. If we’ve deduced that a region is stable and we already know it is dead, that’s fine.
• When we set an edge to active we set the whole Quad3 to active also, since plainly if there is activity in the edge, there is activity in the Quad3.
• Contradicting the previous point, when we set an edge to stable or dead, we do not set the corner contained in that edge region to stable or dead, even though obviously it is; we cannot have the entire edge be stable and its corner be unstable. It just so happens that in the original implementation, every code path on which the edge is set to stable, the corner has already been set to stable, and I will maintain this property in my port. It wouldn’t hurt to fix that here, but I wanted to match the behaviour of the original code as much as possible.
• There is no “set corner active” because if you’re setting the corner active, you’re setting all the regions active.

All right, we have our helper to set state for a Quad3. We need eight state bytes, one for each of the eight Quad3s in a Quad4. We declare our state words as fields in Quad4:

```private uint evenstate;
private uint oddstate;```

And make eight helpers to shift the state bits in and out:

```private Quad3State EvenSEState
{
get => new Quad3State(evenstate & 0x000000ff);
set => evenstate = (evenstate & 0xffffff00) | (uint)value;
}
...```

Suppose we’re on an odd generation and we’ve just computed the next (even) generation for the NW Quad3. We have the previous even generation in hand; how are we going to know which setters to call? We need to figure out two things:

• Given an old Quad3 and a new one, which regions of the new one are different?
• Of the regions that are not different, which of them are all dead?

I mentioned in a previous episode that I was one day going to add more code to the “step a Quad3” methods in Quad4, and that day has come. Remember that the original code was:

```private void StepOddNW()
{
}```

The new method is:

```private void StepOddNW()
{
evenNW = newEvenNW;
}```

That is: compute the next even generation, update the state bits by comparing the previous even generation to the next even generation, and then set the new state bits and new even NW Quad3.

How does the UpdateEvenQuad3State work? Plainly we have to compare the previous generation’s Quad3 to the newly computed Quad3, and in particular we will need to know if any of its regions are dead.

```bool AllDead => (NW | NE | SW | SE).Dead;

That is, OR together the relevant portions of each component Quad2, mask out the irrelevant portions, and check whatever is left for deadness.

Notice that I’ve made these private; we’ll put all the code that needs these in Quad3. (I told you these helper types were not going to stay simple!)

How will we compute what parts are stable? If we have a previous Quad3 in hand and a new Quad3, we need to know what regions are unchanged. The XOR operation gives us that on bits; XOR is one if the bits are different and zero if they are the same. I’ll add an XOR operation to Quad2, just like we have an OR operation already:

```public static Quad2 operator ^(Quad2 x, Quad2 y) =>

Remember that my goal for this series is to make the code as clear as possible by making it read like “business domain” code rather than “mechanism domain” code. I need to know what regions of a Quad3 have changed, so I’m going to do that by making a new struct called “change report” that gives a pleasant, readable interface overtop of the bit-twidding mechanisms. In Quad3:

```private Quad3ChangeReport Compare(Quad3 q) =>

The implementation is basically just a rename of the operations we just added to Quad3!

```private struct Quad3ChangeReport
{
{
q3 = new Quad3(x.NW ^ y.NW, x.NE ^ y.NE, x.SW ^ y.SW, x.SE ^ y.SE);
}
}```

Adding a few extra lines of code in order to make the call sites readable is very worthwhile in my opinion. Remember, the jitter is pretty smart and will inline these operations, and if it doesn’t, well, it is easier to make a readable program more performant than an unreadable one.

Note also that this struct is nested inside Quad3, so that it can use the private helper methods I just added. I don’t often program with nested types, but this is an ideal use case; it is an implementation detail of a method of the outer type.

We’ve digressed slightly from the question at hand: how do we update state bits given old state bits, an old Quad3 and a new Quad3?

I know the following method looks a little bit hard to read, but imagine how it looks with every one of those bit-twiddling operations written out as a bit operation with a hexadecimal mask! Believe me, it is more understandable when you can read the business logic in English:

```public Quad3State UpdateEvenQuad3State(Quad3 newQ3, Quad3State s)
{
if (changes.NorthwestCornerNoChange)
{
else
s = s.SetCornerStable();
if (changes.NorthEdgeNoChange)
{
else
s = s.SetHorizontalEdgeStable();
}
else
{
}
if (changes.WestEdgeNoChange)
{
else
s = s.SetVerticalEdgeStable();
if (changes.NoChange)
{
else
s = s.SetAllRegionsStable();
}
else
{
}
}
else
{
}
}
else
{
s = s.SetAllRegionsActive();
}
return s;
}
```

If you trace all the logic through you’ll see that with only a handful of bit operations we manage to correctly update the eight state bits for the even-cycle Quad3.

We then cut-n-paste this code to do the same thing to the odd cycle, just looking at the opposite edges and corners; obviously I’ll omit that.

Once again we’ve managed to write a whole lot of bit-twiddling code; we now know at the end of every tick whether each of thirty-two regions of a Quad4 had any change from how they were two generations previous.

How are we going to use this information to save time?

Next time on FAIC: We’ll write getters to read out the state we’ve just written, and draw a bunch more diagrams.