ikod-containers 0.0.15
containers library
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
ikod-containers
HashMap
Main differences from language AA:
- HashMap itself is value(not reference), so any
assign
from one map to another copy all data. If you need performance - use reference or pointer. - HashMap have no "in" operator.
- Any
get
get method returns value stored in table, and never - pointer. This is safe. AA can return pointer as it allocates on each insertion.
Main advantages:
- It is fast, as it do not allocate on every insert and has optimized storage layout.
- It inherit @nogc and @safe properties from key and value types (but opIndex can throw exception so it is not @nogc in any case - use fetch or get with default value)
- Provide stable iteration over container (you can modify/delete items from table while iterating over it)
code sample
import std.range;
import std.algorithm;
import ikod.containers.hashmap;
void main()
{
HashMap!(string, int) counter;
string[] words = [
"hello", "this", "simple", "example", "should", "succeed", "or", "it",
"should", "fail"
];
// count words, simplest and fastest way
foreach (word; words) {
counter[word] = counter.getOrAdd(word, 0) + 1; // getOrAdd() return value from table or add it to table
}
assert(counter.fetch("hello").ok); // fetch() is replacement to "in": you get "ok" if key in table
assert(counter.fetch("hello").value == 1); // and value itself
assert(counter["hello"] == 1);
assert(counter["should"] == 2);
assert(counter.contains("hello")); // contains check presence
assert(counter.length == words.length - 1); // because "should" counts only once
// iterators
assert(counter.byKey.count == counter.byValue.count);
assert(words.all!(w => counter.contains(w))); // all words in table
assert(counter.byValue.sum == words.length); // sum of counters must equals to number of words
}
- Registered by Igor Khasilev
- 0.0.15 released 4 years ago
- ikod/ikod-containers
- BSL-1
- Copyright © 2019, Igor Khasilev
- Authors:
- Dependencies:
- unit-threaded, automem
- Versions:
-
0.0.22 2021-Oct-26 0.0.21 2021-Aug-19 0.0.20 2020-Oct-11 0.0.19 2020-Oct-01 0.0.18 2020-Oct-01 - Download Stats:
-
-
13 downloads today
-
48 downloads this week
-
363 downloads this month
-
106237 downloads total
-
- Score:
- 3.1
- Short URL:
- ikod-containers.dub.pm