Bundle Generic collections library: Vector (dynamic array), Map (hash map), Set, MultiMap, Stack, Queue, and Pair. Provides typed iteration, sorting, filtering, and functional operations (Reduce, Any, All). Compile with -lib gen_collect.
Cache
MRU/LRU object cache
cache := Collection.Cache->New(Cache->Type->LRU, 3)<IntRef, String>;
cache->Insert(415, "San Francisco");
cache->Insert(925, "East Bay");
cache->Insert(650, "Mountain View");
cache->Find(650)->PrintLine();
cache->Find(650)->PrintLine();
cache->Find(415)->PrintLine();
cache->Find(925)->PrintLine();
cache->Find(925)->PrintLine();
cache->Insert(510, "Oakland");
cache->Find(510)->PrintLine();
cache->Find(510)->PrintLine();
cache->Find(510)->PrintLine();
"---"->PrintLine();
values := cache->GetKeys()<IntRef>;
each(value := values) {
value->PrintLine();
};Operations
Empty #
Clears the cache
method : public : Empty() ~ NilExample
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 3)<IntRef, String>;
cache->Insert(1, "a");
cache->Empty();
cache->IsEmpty()->PrintLine(); # trueFind #
Searches for a value in cache and updates access priority for LRU/MRU
method : public : Find(key:K) ~ SParameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| S | found value, Nil if not found |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 3)<IntRef, String>;
cache->Insert(1, "one");
cache->Find(1)->PrintLine(); # one
cache->Find(99)->PrintLine(); # (Nil — nothing printed)GetKeyValues #
Gets a collection of key/value pairs
method : public : GetKeyValues() ~ Vector<Pair<K,S>>Return
| Type | Description |
|---|---|
| Vector<Pair<K,S>> | vector of key/value pairs |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 2)<IntRef, String>;
cache->Insert(10, "ten");
kvs := cache->GetKeyValues()<Collection.Pair<IntRef, String>>;
kvs->Size()->PrintLine(); # 1GetKeys #
Get a collection of keys
method : public : GetKeys() ~ Vector<K>Return
| Type | Description |
|---|---|
| Vector<K> | vector of keys |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 3)<IntRef, String>;
cache->Insert(1, "a");
cache->Insert(2, "b");
keys := cache->GetKeys()<IntRef>;
keys->Size()->PrintLine(); # 2GetValues #
Gets a collection of values
method : public : GetValues() ~ Vector<S>Return
| Type | Description |
|---|---|
| Vector<S> | vector of values |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 3)<IntRef, String>;
cache->Insert(1, "alpha");
cache->Insert(2, "beta");
vals := cache->GetValues()<String>;
each(v := vals) {
v->PrintLine();
};Has #
Checks for a value in a cache
method : public : Has(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| Bool | true if found, false otherwise |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 3)<IntRef, String>;
cache->Insert(7, "seven");
cache->Has(7)->PrintLine(); # true
cache->Has(99)->PrintLine(); # falseInsert #
Inserts a value into the hash
method : public : Insert(key:K, value:S) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
| value | S | value |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 3)<IntRef, String>;
cache->Insert(1, "alpha");
cache->Insert(2, "beta");
cache->Size()->PrintLine(); # 2IsEmpty #
Checks to see if the cache is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 3)<IntRef, String>;
cache->IsEmpty()->PrintLine(); # true
cache->Insert(1, "a");
cache->IsEmpty()->PrintLine(); # falseNew # constructor
Default constructor
New(type:Cache->Type, max:Int)Parameters
| Name | Type | Description |
|---|---|---|
| type | Cache->Type | MRU or LRU cache |
| max | Int | cache max size |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 3)<IntRef, String>;
cache->Insert(1, "one");
cache->Insert(2, "two");
cache->Find(1)->PrintLine(); # oneRemove #
Removes a value from the cache
method : public : Remove(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key for value to remove |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 3)<IntRef, String>;
cache->Insert(5, "five");
cache->Remove(5)->PrintLine(); # true
cache->Has(5)->PrintLine(); # falseSize #
Size of cache
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size of cache |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 3)<IntRef, String>;
cache->Insert(1, "a");
cache->Size()->PrintLine(); # 1ToString #
Formats the collection into a string. If an element implements the 'Stringify' interface, it's 'ToString()' is called.
method : public : ToString() ~ StringReturn
| Type | Description |
|---|---|
| String | string representation |
Example
cache := Collection.Cache->New(Collection.Cache->Type->LRU, 2)<IntRef, String>;
cache->Insert(10, "ten");
cache->Insert(20, "twenty");
cache->ToString()->PrintLine();