HashTable¶
-
class
hirola.HashTable(max, dtype, almost_full=0.9, 'warn')¶ The raw core behind a set or a dictionary’s keys.
A hash table resembles a dictionary where its keys are the
keysarray but the values are an just an enumeration. It’s core API:Use
addto add new keys if they’ve not been already added.The
keyslists all keys that have been added in the order that they were added.
-
__init__(max, dtype, almost_full=0.9, 'warn')[source]¶ - Parameters
max (
Number) – An upper bound for the number of keys which can fit in this table. Sets themaxattribute.dtype (
Union[dtype,generic,type,str,list,tuple]) – The data type for the table’s keys. Sets thedtypeattribute.almost_full – The handling of almost full hash tables. Sets the
almost_fullattribute.
The max parameter is silently normalised to
intand clipped to a minimum of 1 if it is less than 1.
-
add(keys)[source]¶ Add keys to the table.
Any key which is already in
keysis not added again. Returns the index of each key inkeyssimilarly toget.- Raises
ValueError – If the
dtypeof keys doesn’t match thedtypeof this table.exceptions.HashTableFullError – If there is no space to place new keys.
exceptions.HashTableDestroyed – If the
destroymethod has been previously called.exceptions.AlmostFull – If the table becomes nearly full and is configured to raise an error (set by the
almost_fullattribute).
- Warns
exceptions.AlmostFull – If the table becomes nearly full and is configured to warn (set by the
almost_fullattribute).- Return type
-
contains(keys)[source]¶ Check if a key or keys are in the table.
- Parameters
keys – Elements to check for.
- Return type
- Returns
Either true or false for each key in keys.
This function is equivalent to but faster than
table.get(keys) != -1. To check only one key you may also usekey in table.
-
destroy()[source]¶ Release a writeable version of
keysand permanently disable this table.Modifying
keyswould cause an internal meltdown and is therefore blocked by setting the writeable flag to false. However, if you no longer need this table then it is safe to do as you please with thekeysarray. This function grants you full access tokeysbut blocks you from adding to or getting from this table in the future. If you want both a writeablekeysarray and functional use of this table then usetable.keys.copy().
-
get(keys, default=- 1)[source]¶ Lookup indices of keys in
keys.- Parameters
keys – Elements to search for.
default – Returned inplace of a missing key. May be any object.
- Return type
- Returns
The index/indices of keys in this table’s
keys. If a key is not there, returns-1in its place.- Raises
ValueError – If the
dtypeof keys doesn’t match thedtypeof this table.exceptions.HashTableDestroyed – If the
destroymethod has been previously called.
-
resize(new_size, in_place=False)[source]¶ Copy the contents of this table into a new
HashTableof a different size.- Parameters
new_size – The new value for
max.in_place – If true resize this table. Otherwise make a modified copy.
- Return type
- Returns
Either a new hash table with attributes
keysandlengthmatching those from this table or this table.- Raises
ValueError – If requested size is too small (
new_size < len(table)).
Changed in version 0.3.0: Add the in_place option.
-
property
almost_full¶ The response to an almost full hash table. Hash tables become dramatically slower, the closer they get to being full. Hirola’s default behaviour is to warn if this happens but can be configured to ignore the warning, raise an error or automatically make a new, larger table.
This is an overloaded parameter.
almost_full = None:Disable the almost full warning entirely.
almost_full = (0.8, “warn”):Issue a
hirola.exceptions.AlmostFullwarning if the table reaches 80% full.
almost_full = (0.7, “raise”):Raise a
hirola.exceptions.AlmostFullexception if the table reaches 80% full.
almost_full = (0.7, 2):Whenever the table reaches 70% full, double the table size.
For reference, Python’s
dictgrows 8-fold when two thirds full. To mimic this behaviour, settable.almost_full = (2 / 3, 8).
-
property
dtype¶ The data type for the table’s keys.
Use a structured
numpy.dtypeto indicate that several numbers make up a single key.Examples
Individual float keys:
HashTable(100, np.float64).Triplets of floats keys:
HashTable(100, (np.float64, 3)).Strings of up to 20 characters:
HashTable(100, (str, 20)).Mixed types records data:
HashTable(100, [(“firstname”, str, 20), (“lastname”, str, 20), (“age”, int)]).
- Return type
-
property
keys¶ The unique elements in the table.
Unlike Python’s builtin
dict, thiskeysis apropertyrather than a method. Keys must be immutable hence this array is a readonly view. Seedestroyto release the writable version.- Return type
-
property
length¶ The number of elements currently in this table. Aliased via
len(table).- Return type
-
property
max¶ The maximum number of elements allowed in this table.
Adding keys to exceed this maximum will trigger a
HashTableFullError. Nearly full tables willaddandgetmuch slower. For best performance, choose a maximum size which is 25-50% larger than you expect it to get.- Return type