oktavia-von-gwwcendorff:

shieldfoss:

So basically, I’m calling out C(++) on its utilization of programmer resources because things that should be simple and trivial …aren’t.

That’s fair.

Incidentally, this is how to actually implement it in C++

(sans constructor, destructor, or any sense whatsoever)

class troll
{
  private:
    static std::vector<uint64> awake[16384];
    // 64 bits times 16k = 1 Mega and
    // in a single vector assingnment,
    // they're as tightly packed as the
    // phsycal RAM cells will allow
    // (We're assuming a 64 bit system)

    uint64 trollNumber;
    // This'll easily hold a million unique trolls.

  public:        
    void setSleep(uint64 number);
    void setAwake(uint64 number);

    bool getAwake(uint64 number);

    bool queerTheSleepBinary(uint64 number);
}

void troll::setSleep(unint64 number)
{
  awake[number/64] &= ~(1 << (number%64));
}

void troll::setAwake(unint64 number)
{
  awake[number/64] |= (1 << (number%64));
}

bool troll::getAwake(unint64 number)
{
  return (awake[number/64] >> (number%64)) & 1;
}

bool troll::queerTheSleepBinary(uint64 number)
{
  return ~(awake[number/64] ^= 1 << (number%64));
}

So now the interesting questions are:

1) Why did I make two separate functions setSleep/setAwake instead of a single function that takes a bool? (There’s a real reason, but can you guess?)

2) What does it mean to queer the sleep binary?

(Also, I have not spent 5000 hours on this and I haven’t done bitwise operations in a while so there might be mistakes. Especially in queerTheSleepBinary there’s one thing happenning that I’m not sure is allowed but I cannot be bothered to look it up right now)

1) [confidence level: ~50%] you pass less parameters because (function, bool) is strictly more than (function) and there is no super-elegant way to do it with a bool which would save you any amount of code; this way you do it “statically” which may be advantageous in some circumstances because the code for switching based on true/false gets placed elsewhere

2) it gives me a headache, flips the sleep state of this one troll, then casts the bitwise NOT of all local trolls into a boolean (effectively returning false if all trolls are awake and true if any are sleeping if I remember how them booleans work on a bit level?) and it’s not obvious that this would be a syntax error nor does it require any extra caution or explicitness when you’re munging the bits this way and while wikipedia says operator precedence shouldn’t cause issues here (although it’s inconsistent with the usage at setAwake which is why I got suspicious about it in the first place) I don’t trust the C family

1) Close. The obvious was to have a troll::setSleepState(uint64 number, bool sleepState) that then did one of two things with a conditional on the sleep state. However, say you want to put all outdoor trolls to sleep at sundown and wake them all at dawn[1].

I’m going to inline the functions to show it more obviously:

trollSleepCycle1()
{
  for each troll in trolls
  {
    setState(troll, day) //this is where the function starts
    {
      if(day)
      {
        troll wakes up;
      }
      else
      {
        troll sleeps;
      }
    }
  }
}

At every dusk, when you call this function passing “night”, you’ll need an extra jump when the conditional is false.

Whereas if I have the conditional before I do the foreach, I can call a function without a conditional and avoid up to 1 million jumps.

trollSleepCycle2()
{
  if(day)
  {
    for each troll in trolls
    {
      setTrollWakesUp(troll)
      {
        troll wakes up;
      }
    }
  }
  else
  {
    for each troll in trolls
    {
      setTrollSleeps(troll)
      {
        troll sleeps;
      }
    }
  }
}

There’s still the jump, of course, but it’s outside the foreach, so it only happens once.

If I’m going for speed, I’d rather have an extra function taking up space than an extra jump taking up cycles.

[1] Indoor trolls depend on local artificial light and are handled in a difrerent pass

2) Much simpler. It toggles the trolls sleep state, then returns what state it was in before you bothered it. If you ask about walking troll, it’ll fall asleep and then tell you “I’m awake.”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s