Indigo 0.95 |
indigo. tools.hash
Contains a generic hash map. See Hash for details. Summary
HashIteratorImplements an unidirectional-iterator for Hash. Obtain an iterator from a hash with Hash.begin(), Hash.end() or Hash.find(). You can then navigate through the hash with the incremention operators, and compare iterators with each other on equality. Note that the keys will be traversed in arbitrary order, which is different from MapIterator. The value property can be used to obtain/change the value the iterator points to (note that you cannot use operator * for this purpose). The ptr property returns a pointer to the item, which is especially useful for structs as items. Summary
keyReturns the key pointed to by the iterator. The iterator has to point to a valid item in a hash. You cannot change the key of an item. See also value. valueReadReturns the value pointed to by the iterator. The iterator has to point to a valid item in a hash. If the hash contains structs, you cannot use this function to call a member function of the struct which needs to change the struct. Use ptr for this purpose, it may also be faster. WriteSets the value pointed to by the iterator. The iterator has to point to a valid item in a hash. See also key. ptrReturns a pointer of the value pointed to by the iterator. The iterator has to point to a valid item in a hash. See also value. gotoNextEqualKey()
Moves the iterator forward in the hash, as long as the key of the next item is identical to the current key, and returns true. Otherwise it returns false, and the iterator is not changed. This function should be used instead of opAddAssign() if you want to traverse all items with a specific key: Hash!(int, char[]) hash; opAddAssign()
Moves the iterator delta items forward in the hash. delta must be >= 0. See also gotoNextEqualKey(). HashImplements a generic associative array, based on a hash-table. It automatically handles allocation of needed space in an efficient manner, and provides fast insertions, lookups and removals (all O(1)). There are some hints on which container to choose (Container types). Hash provides almost all of the functions QHash (http://doc.trolltech.com/4.0/qhash.html) has, thus also providing an STL-compatible interface, as far as this is possible in D. Finally Hash implements the standard operators and properties of associative arrays in D. Note that there is no rehash property, though. A hash is instantiated with a key-type and a value-type. The key must provide opEquals and a function that calculates its hash value. You can provide such a function with the third template argument. It must have the following prototype: HashType myFancyHashFunction(Key* key); If you do not provide your own hash function, Hash will use the FNV hash function from indigo.tools.hashfunctions if possible (that is, for the basic D types), and the getHash() function from the TypeInfo of Key otherwise. If possible, provide your own hash function for all complex types, and implement it using fnvHash() or a better algorithm. Use a static function for structs and classes. To insert a (key, value) pair into the hash, you can use insert(), insertMulti() or operator []. Use reserve() if you know in advance how many items you will insert at most. hash["one"] = 1; You can look up values with ptrAt(), operator [] or value(Key). If there is no item with the specified key in the hash, the latter 2 functions return a default-constructed value. If you just want to check whether the hash contains an entry, use contains(). Note that, in contrary to Qt or D, rvalue operator [] does not insert an item silently if no one with the key exists! Items can be removed with remove(), or you can remove all elements in the hash with clear(). If you want to iterate over all items stored in the hash, use an Iterator or a foreach-loop. Note that the foreach-loop is much faster. As with the other containers, use HashIterator.value to retrieve the value the iterator points to, and HashIterator.key for the key of the value. NoteIf you store objects (i.e. instances of a class) as values in a hash, the hash only stores references to the objects. They are initialized with null by default. opEquals() compares them on identity. If you store pointers, object references or dynamic arrays in the container, they will not get nullified automatically when deleting items. This may lead to space waste because the garbage collector thinks they are still being used. Call squeeze() to get rid of them. Summary
IteratorA unidirectional-iterator for the hash. See HashIterator. iteratorAlias for Iterator. Provided for STL compatibility. countReturns the number of items in the hash. Synonym for length. sizeReturns the number of items in the hash. Synonym for length. isEmptyReturns true if the hash has length 0, otherwise false. capacityReadReturns the number of elements that can be stored in the hash without forcing a rehash. Note that this is different to Qt, where capacity returns the number of buckets. WriteChanges the capacity. Synonym for reserve(). endReturns an iterator pointing behind the last item of the hash. See also begin. find()
Returns an iterator pointing to the item with key key in the hash, or end if the hash does not contain such a key. If the hash contains more than one item with key key, this function an iterator pointing to the most recently inserted one. The other values are accessible by incrementing the iterator. See also value(Key). ptrAt()
Returns a pointer to the value associated with key key. The hash must contain this key. If there are multiple values for key in the hash, the function returns a pointer the most recently inserted one. This function must be used if you want to call member functions of the value that change it. See also value(Key). valuePtr()
Returns a pointer to the value associated with a key. This is an alias for ptrAt(). value(Key)
Returns the value associated with key key, or a default constructed value if the hash does not contain such a key. If there are multiple values for key in the hash, the function returns the most recently inserted one. Note that you cannot use this function to call a member function of the value that needs to change the value. This also applies to setting the length of a dynamic array. values()
Returns an array containing all values in the hash, in ascending order of their keys. If multiple values are associated with a key, all of them will be in the array. See also value(Key), keys(). keys()
Returns an array containing all keys in the hash in arbitrary order. Keys that occur multiple times in the hash occur multiple times in the array, too. values() is ordered exactly the same way. reserve()
Preallocates enough memory for size elements. This does not change the length of the hash. But if you know in advance how many elements you need, you will get better performance if the hash has to be resized often. Ideally size should be a bit larger than the actual number of elements you want to store. See also capacity. clear()
Removes all elements from the hash. Note that the internal storage is not freed with this operation, therefore the hash can still occupy a lot of memory. Use free() to release it as well. squeeze()
Releases unused internal storage to the garbage collector. This will not affect the nodes in the hash. Call this function after deleting a lot of nodes, and only if you are sure you will not insert new nodes again. Otherwise you will definitely fragment the memory. The hash may still occupy a lot of unused memory after calling this function, especially if you inserted and deleted a lot of elements before. See also free(). free()
Removes all elements from the hash and makes the internal storage reavailable to the garbage collector. Note that this storage will only be available again after a garbage collection, and if the hash contains objects their destructors will not be called until the collection. To immediately delete the objects, use deleteAll() before free(). See also squeeze(). insert()
Inserts a new item with the key key and the value value. If there already is an item with key key, its value is replaced by value. If there are multiple items with key key, the most recently inserted item is altered. See also insertMulti(). insertMulti()
Inserts a new item with the key key and the value value, even if there already is an item with the same key. See also insert(). remove()
Removes all items from the hash that have the key key. Returns the number of removed items. See also take(). erase()
Removes items from the hash. Alias for remove(). take()
Removes the item with the key key from the hash and returns the value associated with it. It the item does not exist in the hash, the function returns a default constructed value. If there are multiple items with key key, only the most recently inserted one is removed. See also remove(). opIndex()
Returns the value associated with the key key. If the hash does not contain key, this function returns a default constructed value, but does not insert this value into the hash. If the hash contains multiple items with key key, this function returns the most recently inserted one. See also value(Key). opIndexAssign()
Sets the value associated with the key key to value. If the hash does not contain key, a new item with value is inserted into the hash. If the hash contains multiple items with key key, this function alters the most recently inserted one. See also insert(). opApply(Key, T)
Applies dg to all elements in the hash, or until dg returns a nonzero value. This function enables the foreach-loop for the hash, iterating over keys and values. Returns the last value returned by dg. NoteIt is not allowed to change the key in the delegate, even if it is passed by reference. D does not allow a foreach-loop if the function is not defined this way, otherwise i would change it. Violate this rule and the hash will go crazy. writeTo()
Serializes this hash and all its items into the DataStream st. This function is only available if the used types support serialization with DataStream, either because they are D builtin types, or because they provide writeTo() and readFrom() or createFrom() functions themselves. Refer to the DataStream documentation for details. readFrom()
Reads the contents of the hash previously serialized with writeTo() from the DataStream st. This function is only available if the used types support serialization with DataStream, either because they are D builtin types, or because they provide writeTo() and readFrom() or createFrom() functions themselves. Refer to the DataStream documentation for details. |
Sets the value pointed to by the iterator to val.
void setValue( T val )
Moves the iterator forward in the hash, as long as the key of the next item is identical to the current key, and returns true.
int gotoNextEqualKey()
Moves the iterator one item forward in the hash, and returns the previous item.
HashIterator opPostInc()
Moves the iterator delta items forward in the hash.
HashIterator opAddAssign( ptrdiff_t delta )
Returns an iterator pointing to the item with key key in the hash, or end if the hash does not contain such a key.
Iterator find( Key key )
Returns true if the hash contains key, otherwise false.
int contains( Key key )
Returns the number of occurences of key in the hash.
size_t count( Key key )
Returns a pointer to the value associated with key key.
T* ptrAt( Key key )
Returns a pointer to the value associated with a key.
alias ptrAt valuePtr
Returns the value associated with key key, or a default constructed value if the hash does not contain such a key.
T value( Key key )
Returns the value associated with key key, or defaultValue if the hash does not contain such a key.
T value( Key key, T defaultValue )
Returns an array containing all values in the hash, in ascending order of their keys.
T[] values()
Returns an array with all values associated to key key, from the most recently inserted to the least inserted one.
T[] values( Key key )
Returns an array containing all keys in the hash in arbitrary order.
Key[] keys()
Preallocates enough memory for size elements.
void reserve( size_t size )
Removes all elements from the hash.
void clear()
Releases unused internal storage to the garbage collector.
void squeeze()
Removes all elements from the hash and makes the internal storage reavailable to the garbage collector.
void free()
Inserts a new item with the key key and the value value.
Iterator insert( Key key, T value )
Inserts a new item with the key key and the value value, even if there already is an item with the same key.
Iterator insertMulti( Key key, T value )
Removes all items from the hash that have the key key.
size_t remove( Key key )
Removes items from the hash.
alias remove erase
Removes the item with the key key from the hash and returns the value associated with it.
T take( Key key )
Returns the value associated with the key key.
alias value opIndex
Sets the value associated with the key key to value.
alias insert opIndexAssign
Applies dg to all elements in the hash, or until dg returns a nonzero value.
int opApply( int delegate(inout T) dg )
Applies dg to all elements in the hash, or until dg returns a nonzero value.
int opApply( int delegate(inout Key, inout T) dg )
Compares the items of hash with the items in this hash, and returns true if they are all equal, false otherwise.
int opEquals( Hash hash )
Serializes this hash and all its items into the DataStream st.
void writeTo( DataStream st )
Reads the contents of the hash previously serialized with writeTo() from the DataStream st.
void readFrom( DataStream st )
Hashes the key with the FNV hash function.
public static HashType fnvHash( ubyte[] key, HashType start = fnvOffsetBasis )
This is a template function with several variants.
public template deleteAll( ForwardIteratorOrContainer )