Hash
Hash unordered table of generics
Example
# insert elements
hash := Collection.Hash->New()<IntRef, String>;
hash->Insert(415, "San Francisco");
hash->Insert(510, "Oakland");
hash->Insert(925, "East Bay");
# get size
hash->Size()->PrintLine();
# get value by key
hash->Find(510)->PrintLine();
# get key/values
key_values := hash->GetKeyValues()<Pair<IntRef, String>>;
each(key_value := key_values) {
key_value->GetFirst()->PrintLine();
};
# get values
values := hash->GetValues()<String>;
each(value := values) {
value->PrintLine();
};
# check for key
hash->Has(408)->PrintLine();Operations
- New
- Dict
- Empty
- Find
- GetCapacity
- GetKeyValues
- GetKeys
- GetValues
- Has
- Insert
- IsAutoResize
- IsEmpty
- Remove
- Resize
- SetAutoResize
- SetCapacity
- Size
- ToString
Dict # function
Converts a vector pairs to a hash
function : Dict(pairs:Vector<Collection.Tuple.Pair<K,V>>) ~ Hash<K,V>Parameters
| Name | Type | Description |
|---|---|---|
| pairs | Vector<CollectionTuplePair<K,V>> | vector of pairs |
Return
| Type | Description |
|---|---|
| Hash<K,V> | hashed pairs |
Example
pairs := Collection.Vector->New()<Collection.Tuple.Pair<String, IntRef>>;
pairs->AddBack(Collection.Tuple.Pair->New("a", IntRef->New(1))<String, IntRef>);
pairs->AddBack(Collection.Tuple.Pair->New("b", IntRef->New(2))<String, IntRef>);
hash := Collection.Hash->Dict(pairs)<String, IntRef>;
hash->Find("a")->PrintLine(); # 1Empty #
Clears the map
method : public : Empty() ~ NilExample
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
hash->Empty();
hash->IsEmpty()->PrintLine(); # trueFind # native
Searches for a value in a hash
method : public : native : Find(key:K) ~ VParameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| V | found value, Nil if not found |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("price", 99);
hash->Find("price")->PrintLine(); # 99GetCapacity #
Gets the hash table capacity
method : public : GetCapacity() ~ Hash->CapacityReturn
| Type | Description |
|---|---|
| Hash->Capacity | hash table capacity |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->GetCapacity()->PrintLine();GetKeyValues #
Gets a collection of key/value pairs
method : public : GetKeyValues() ~ Vector<Pair<K,V>>Return
| Type | Description |
|---|---|
| Vector<Pair<K,V>> | vector of key/value pairs |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("m", 100);
kvs := hash->GetKeyValues()<Collection.Pair<String, IntRef>>;
each(kv := kvs) {
"{kv->GetFirst()}: {kv->GetSecond()}"->PrintLine();
};GetKeys # native
Get a collection of keys
method : public : native : GetKeys() ~ Vector<K>Return
| Type | Description |
|---|---|
| Vector<K> | vector of keys |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
hash->Insert("b", 2);
keys := hash->GetKeys()<String>;
each(k := keys) {
k->PrintLine();
};GetValues # native
Gets a collection of values
method : public : native : GetValues() ~ Vector<V>Return
| Type | Description |
|---|---|
| Vector<V> | vector of values |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("x", 10);
hash->Insert("y", 20);
vals := hash->GetValues()<IntRef>;
each(v := vals) {
v->PrintLine();
};Has #
Checks for a value in a hash
method : public : Has(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | search key |
Return
| Type | Description |
|---|---|
| Bool | true if found, false otherwise |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("k", 1);
hash->Has("k")->PrintLine(); # true
hash->Has("z")->PrintLine(); # falseInsert #
Inserts a value into the hash
method : public : Insert(key:K, value:V) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| key | K | key |
| value | V | value |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("count", 42);
hash->Find("count")->PrintLine(); # 42IsAutoResize #
Gets auto-resize setting
method : public : IsAutoResize() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if auto-resize is set, false otherwise |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->IsAutoResize()->PrintLine(); # trueIsEmpty #
Checks to see if the hash table is empty
method : public : IsEmpty() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if empty, false otherwise |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->IsEmpty()->PrintLine(); # true
hash->Insert("a", 1);
hash->IsEmpty()->PrintLine(); # falseNew # constructor
Default constructor
New(auto_resize:Bool)Parameters
| Name | Type | Description |
|---|---|---|
| auto_resize | Bool | if true, automatically resized the hash table |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("apples", 3);
hash->Insert("bananas", 5);
hash->Size()->PrintLine(); # 2New # constructor
Default constructor
New(capacity:Hash->Capacity, auto_resize:Bool)Parameters
| Name | Type | Description |
|---|---|---|
| capacity | Hash->Capacity | capacity of hash table |
| auto_resize | Bool | if true, automatically resized the hash table |
Example
hash := Collection.Hash->New(Collection.Hash->Capacity->MEDIUM)<String, IntRef>;
hash->Insert("key", 42);
hash->Find("key")->PrintLine(); # 42Remove # native
Removes a value from the hash
method : public : native : Remove(key:K) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| key | K | key for value to remove |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("del", 9);
hash->Remove("del")->PrintLine(); # true
hash->Has("del")->PrintLine(); # falseResize # native
Resizes the hash table
method : public : native : Resize(capacity:Hash->Capacity, auto_resize:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| capacity | Hash->Capacity | table capacity |
| auto_resize | Bool | true for hash table auto resizing, false otherwise |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
hash->Resize(Collection.Hash->Capacity->LARGE);
hash->Find("a")->PrintLine(); # 1SetAutoResize #
Set auto-resize setting
method : public : SetAutoResize(auto_resize:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| auto_resize | Bool | true if auto-resize is enable, false otherwise |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->SetAutoResize(false);
hash->IsAutoResize()->PrintLine(); # falseSetCapacity #
Sets hash table capacity
method : public : SetCapacity(capacity:Hash->Capacity) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| capacity | Hash->Capacity | hash table capacity |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->SetCapacity(Collection.Hash->Capacity->MEDIUM);
hash->GetCapacity()->PrintLine();Size #
Size of map
method : public : Size() ~ IntReturn
| Type | Description |
|---|---|
| Int | size of map |
Example
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("a", 1);
hash->Insert("b", 2);
hash->Size()->PrintLine(); # 2ToString #
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
hash := Collection.Hash->New()<String, IntRef>;
hash->Insert("x", 10);
hash->Insert("y", 20);
hash->ToString()->PrintLine();