🧱 B PRECURSOR DIFFICULTY: Adept Structural

The Flyweight

Sharing the sacred untyped words to conserve the scarce memory of the ancestors.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Flyweight
ARCANUM SCHOOL: Conjuration // Creation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// B PATTERN //
/* Memory is scarce. We keep a cache of sacred words */
ext word_cache[5];
ext cache_count;

init_cache() {
    cache_count = 0;
}

get_sacred_word(val) {
    auto i;
    i = 0;
    while (i < cache_count) {
        if (word_cache[i] == val) {
            return &word_cache[i]; /* Return shared address */
        }
        i = i + 1;
    }

    /* Manifest new flyweight */
    word_cache[cache_count] = val;
    cache_count = cache_count + 1;
    return &word_cache[cache_count - 1];
}
Runic Tags: #b #design-pattern #precursor #bell-labs-magic #untyped