Discard the notion of calling them “booleans.”   They are Work Items, which periodically change state.   And, for each one of these, there is a “cloud” of Interested Parties out there which are waiting for them to become what they are now not.   Those same Interested Parties also need to know when any Work Item ceases to be as it is now.

So, when we introduce a new Interested Party, we associate with it a hash containing, for each Work Item, its last known state.   The Interested Party also contains a count (>= 0) of the number of Work Items it is waiting-upon to change state.   Each Work Item has a list of all of the Interested Parties who are now watching it.

When a Work Item changes state, it notifies all of the Interested Parties, providing its unique-ID and new state.   The Interested Party retrieves the last known state and determines if it has changed.   The appropriate action is determined by an easy four-row state table:
New State Last State Action
TrueTrueTake no action.
TrueFalseSignaled: update last-known-state, decrement wait count. If zero, remove yourself from all interested-parties lists and destroy yourself. (If negative, die!)
FalseFalseTake no action.
FalseTrueUnsignaled: update last-known-state, increment wait count.

You have now completely eliminated the problem of “searching” for anything.

Please notice that I stipulated that Work Items contain a list of references to Interested Parties, but that they provide the parties a unique-id.   This somewhat arbitrary arrangement avoids circular references which can cause headaches for a garbage collector.


In reply to Re: Better algorithm or data structure? by Anonymous Monk
in thread Better algorithm or data structure? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.