Composite Types

Composite types in SNAP are TPair, TVec, THash, and THashSet.

TPair

The name TPair refers to a general data structure that consists of two values, which can be of different types. All of the following methods are available for objects that are classified as TPair objects.

class TPair
class TPair(Val1, Val2)
class TPair(SIn)

Creates a TPair object consisting of the two values, if provided. If Val1 and Val2 are not given, the default value for each of their respective types is used. If SIn is provided, the pair of values are loaded from the binary stream SIn.

The TPair constructor cannot be directly called. To create a TPair object, the correct constructor must be chosen, which indicates the types of both the values in the pair. The naming convention is as follows: <type_name_1> of the first object in the pair, <type_name_2> for the second object in the pair (without the T), and finally a Pr. If <type_name_1> and <type_name_2> are the same, then the name may be condensed to <type_name> followed by Pr.

The following TPair types are supported: TIntPr, TFltPr, TIntStrPr, TBoolFltPr, TIntBoolPr, TIntUInt64Pr, TIntIntPrPr, TIntIntVPr, TIntFltPr, TIntStrVPr, TIntPrIntPr, TUIntUIntPr, TUIntIntPr, TUInt64IntPr, TUInt64Pr, TUint64FltPr, TUInt64StrPr, TFltIntPr, TFltUInt64Pr, TFltStrPr, TAscFltIntPr, TAscFltPr, TAscFltStrPr, TStrIntPr, TStrFltPr, TStrPr, TStrStrVPr, TStrVIntPr, TIntStrPrPr, and TFltStrPrPr.

To illustrate, the following examples all return a TIntPr with both values set to 0:

>>> snap.TIntPr(0, 0)
>>> snap.TIntPr(snap.TInt(0), snap.TInt(0))
>>> snap.TIntPr()

The following public functions are supported by the TPair class:

Load(SIn)

Loads the pair from a binary stream SIn.

Save(SOut)

Saves the pair to a binary stream SOut.

GetMemUsed()

Returns the size of the TPair object in bytes.

GetVal1()

Returns the first value in the TPair.

GetVal2()

Returns the second value in the TPair.

GetPrimHashCd()

Returns the primary hash code, which is computed using the primary hash codes of the two values in the pair.

GetSecHashCd()

Returns the secondary hash code, which is computed using the secondary hash codes of the two values in the pair.

The following public attributes are available:

Val1

The first value in the pair. Supports assignment, which requires the use of Snap.py types rather than Python types.

Val2

The second value in the pair. Supports assignment, which requires the use of Snap.py types rather than Python types.

Below is some code demonstrating the use of the TPair type:

>>> pr = snap.TIntPr(10, 15)
>>> print pr.Val1.Val
10
>>> pr.Val1 = snap.TInt(21)
>>> print pr.GetVal1()
21
>>> pr.GetPrimHashCd()
687

TVec

Vectors are sequences of values of the same type. Existing vector values can be accessed or changed by their index in the sequence. New values can be added at the end of a vector. All of the following methods are available for objects that are classified as TVec objects.

class TVec
class TVec(NumVals)
class TVec(MxVals, NumVals)
class TVec(Vec)
class TVec(SIn)

Creates a TVec object of size NumVals, if specified. It MxVals is given, enough memory to store MxVals will be reserved. MxVals must be larger than NumVals. If Vec - a TVec of the same type - is given, the values from Vec are copied into the new TVec. It SIn is provided, the contents of the vector are loaded from the binary stream SIn.

The TVec constructor cannot be directly called. To create a TVec object, the correct constructor must be chosen, which indicates the type stored in the TVec. Vector types in Snap.py and SNAP use a naming convention of being named as <type_name>, followed by V. For example, a vector of integers is named TIntV.

The following TVec types are supported: TIntV, TFltV, TIntPrV, TFltPrV, TIntTrV, TIntFltKdV, TBoolV, TChV, TUChV, TUIntV, TUInt64V, TSFltV, TAscFltV, TStrV, TChAV, TIntQuV, TFltTrV, TUChIntPrV, TUChUInt64PrV, TIntUInt64PrV, TIntUInt64KdV, TIntFltPrV, TIntFltPrKdV, TFltIntPrV, TFltUInt64PrV, TFltStrPrV, TAscFltStrPrV, TIntStrPrV, TIntIntStrTrV, TIntIntFltTrV, TIntFltIntTrV, TIntStrIntTrV, TIntKdV, TUIntIntKdV, TIntPrFltKdV, TIntStrKdV, TIntStrPrPrV, TIntStrVPrV, TIntIntVIntTrV, TUInt64IntPrV, TUInt64FltPrV, TUInt64StrPrV, TUInt64IntKdV, TUInt64FltKdV, TUInt64StrKdV, TFltBoolKdV, TFltIntKdV, TFltUInt64KdV, TFltIntPrKdV, TFltKdV, TFltStrKdV, TFltStrPrPrV, TFltIntIntTrV, TFltFltStrTrV, TAscFltIntPrV, TAscFltIntKdV, TStrPrV, TStrIntPrV, TStrIntKdV, TStrFltKdV, TStrAscFltKdV, TStrTrV, TStrQuV, TStrFltFltTrV, TStrStrIntTrV, TStrKdV, TStrStrVPrV, TStrVIntPrV, TFltIntIntIntQuV, TIntStrIntIntQuV, and TIntIntPrPrV.

To illustrate, the following examples show how to create a TVec:

>>> snap.TIntV()
>>> snap.TIntV(5)
>>> v1 = snap.TIntV(8, 5)
>>> v1.Add(1)
>>> v2 = snap.TIntV(v1)
>>> for val in v2:
...     print val
...
1

TVec offers iterators of type TInt for fast access through the vector. The TInt returned by any iterator method represents the value at a given index in the vector.

The following public functions are supported by the TVec class:

V[Index]

Returns the value at index Index in vector v.

V[Index] = Value

Set V[Index] to Value.

del V[Index]

Removes the value at index index from the vector.

Val in V

Return True if Val is a value stored in vector V, else False.

Val not in V

Equivalent to not Val in V.

GetV(Val1)
GetV(Val1, Val2)
GetV(Val1, Val2, Val3)
GetV(Val1, Val2, Val3, Val4)
GetV(Val1, Val2, Val3, Val4, Val5)
GetV(Val1, Val2, Val3, Val4, Val5, Val6)
GetV(Val1, Val2, Val3, Val4, Val5, Val6, Val7)
GetV(Val1, Val2, Val3, Val4, Val5, Val6, Val7, Val8)
GetV(Val1, Val2, Val3, Val4, Val5, Val6, Val7, Val8, Val9)

Returns a vector with the given values.

Load(SIn)

Loads a graph from a binary stream SIn and returns a pointer to it.

Save(SOut)

Saves the graph to a binary stream SOut.

GetMemUsed()

Returns the size of the vector object in bytes.

GetPrimHashCd()

Returns the primary hash code for the vector.

GetSecHashCd()

Returns the secondary hash code for the vector.

Gen(Vals)
Gen(MxVals, Vals)

Resizes the vector to hold Vals and initializes each position in the vector with the default value for the given type of the vector (i.e. 0 for TInt). If MxVals is provided, the function reserves enough memory for MxVals values in the vector.

Reserve(MxVals)
Reserve(MxVals, Vals)

Reserves enough memory for MxVals values in the vector. If Vals is provided, it resizes the vector to hold NumVals and initializes each position in the vector with the default value for the given type of the vector.

Clr()

Clears the contents of the vector.

Trunc(Vals=-1)

Truncates the vector to length Vals. If Vals is not given, the vector is left unchanged.

Pack()

The vector reduces its capacity (frees memory) to match its size.

MoveFrom(Vec)

Moves all the data from Vec into the current vector and changes its capacity to match that of Vec. The contents and capacity of Vec are cleared in the process.

Swap(Vec)

Swaps the contents and the capacity of the current vector with Vec.

Empty()

Returns a bool indicating whether the vector is empty.

Len()

Returns the length of the vector.

Reserved()

Returns the reserved capacity for the vector.

Last()

Returns the last value in the vector.

LastValN()

Returns the index of the last value in the vector.

LastLast()

Returns the second to last element in the vector.

BegI()

Returns an iterator pointing to the first element in the vector.

EndI()

Returns an iterator referring to the past-the-end element in the vector.

GetI(ValN)

Returns an iterator an element at position ValN.

Add()
Add(Val)
Add(Val, ResizeLen)

Appends Val to the end of the vector. If Val is not specified, it adds an element with the default value for the given type of the vector. Returns the index at which the value was appended. If ResizeLen is given, it increases the capacity of the vector by ResizeLen.

AddV(ValV)

Appends the contents of the vector ValV onto the vector. Returns the index of the last element in the vector.

AddSorted(Val, Asc=True)

Adds Val to a sorted vector. If Asc is True, the vector is sorted in ascending order.

AddBackSorted(Val, Asc)

Adds Val to a sorted vector. If Asc is True, the vector is sorted in ascending order.

AddMerged(Val)

Adds element Val to a sorted vector only if the element val is not already in the vector. Returns the index at which val was inserted or -1.

AddVMerged(ValV)

Adds elements of ValV to a sorted vector only if a particular element is not already in the vector. Returns the new length of the vector.

AddUnique(Val)

Adds element Val to a vector only if the element val is not already in the vector. Returns the index at which val was inserted or -1.

GetVal(ValN)

Returns the value at index ValN.

SetVal(ValN, Val)

Sets the value of the element at index ValN to Val.

GetSubValV(BValN, EValN, vec)

Fills ValV with the elements at positions BValN to EValN, inclusive, in this vector.

Ins(ValN, Val)

Inserts the value Val into the vector before the element at position ValN.

Del(ValN)

Deletes the value at index ValN.

Del(MnValN, MxValN)

Deletes all elements from index MnValN to MxValN, inclusive.

DelLast()

Deletes the last element in the vector.

DelIfIn(Val)

Deletes the first instance of value val from the vector. Returns a boolean indicating whether Val was found in the vector.

DelAll(Val)

Deletes all occurrences of Val from the vector.

PutAll(Val)

Sets all elements of the vector to value Val.

Swap(ValN1, ValN2)

Swaps elements at positions ValN1 and ValN2.

NextPerm()

Generates next permutation of the elements in the vector. Returns a boolean indicating whether the previous permutation is different from the original permutation.

GetPivotValN(LValN, RValN)

Picks three random elements at positions LValN ... RValN and returns the index of the middle one.

BSort(MnLValN, MxRValN, Asc)

Bubble sorts values in the portion of the vector starting at MnLVal and ending at MxRValN. If Asc is True, it sorts the vector in ascending order.

ISort(MnLValN, MxRValN, Asc)

Insertion sorts the values in the portion of the vector starting at MnLVal and ending at MxRValN. If Asc is True, it sorts the vector in ascending order.

Partition(MnLValN, MxRValN, Asc)

Partitions the values in the portion of the vector starting at MnLVal and ending at MxRValN. If Asc is True, it partitions using ascending order.

QSort(MnLValN, MxRValN, Asc)

Quick sorts the values in the portion of the vector starting at MnLVal and ending at MxRValN. If Asc is True, it sorts the vector in ascending order.

Sort(Asc)

Sorts the vector. If Asc is True, it sorts it in ascending order.

IsSorted(Asc)

Checks whether the vector is sorted in ascending (if Asc == True) or descending (if Asc == False) order.

Shuffle(Rnd)

Shuffles the contents of the vector in random order, using the TRnd object Rnd.

Reverse()

Reverses the contents of the vector.

Reverse(LValN, RValN)

Reverses the order of elements in the portion of the vector starting at index LValN and ending at RValN.

Merge()

Sorts the vector and only keeps a single element of each value.

Intrs(ValV)

Updates this vector with the intersection of this vector with ValV.

Union(ValV)

Updates this vector with the union of this vector with ValV.

Diff(ValV)

Updates this vector with the difference of this vector with ValV.

Intrs(ValV, DstValV)

DstValV is the intersection of vectors this and ValV.

Union(ValV, DstValV)

DstValV is the union of vectors this and ValV.

Diff(ValV, DstValV)

DstValV is the difference of vectors this and ValV.

IntrsLen(ValV)

Returns the length of the intersection of this vector with ValV.

UnionLen(ValV)

Returns the length of the union of this vector with ValV.

Count(Val)

Returns the number of times Val appears in the vector.

SearchBin(Val)

Returns the index of an element with value Val or -1.

SearchForw(Val, BValN=0)

Returns the index of an element with value Val or -1. Starts looking at index BValN.

SearchBack(Val)

Returns the index of an element wiht value Val or -1.

SearchVForw(ValV, BValN=0)

Returns the starting position of vector ValV or -1. Starts looking at index BValN.

IsIn(Val)

Returns a bool checking whether element Val is a member of the vector.

GetMxValN()

Returns the position of the largest element in the vector.

Below is some code demonstrating the use of the TVec type:

>>> vec1 = snap.TIntV.GetV(1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> vec1.IsIn(5)
True
>>> vec2 = snap.TIntV(vec1)
>>> vec2.Add(10)
>>> vec2.Diff(vec1)
>>> for val in vec2:
...     print val
...
10

THash

Hash tables contain values of the same type. Each value has a user provided key associated with it. All the keys are of the same type. Table values can be accessed or changed through their keys. New values can be added as (key, value) pairs. All objects classified as THash objects have access to the following methods.

class THash
class THash(ExpectVals, AutoSizeP=False)
class THash(Hash)
class THash(SIn)

Creates a THash object with a capacity of ExpectVals, if specified. If Hash - a THash of the same type - is given, the values from Hash are copied into the new THash. If SIn is provided, the contents of the hash table are loaded from the binary stream SIn.

The THash constructor cannot be directly called. To create a THash object, the correct constructor must be chosen, which indicates the types of the key and value in the THash. Hash table types in Snap.py and SNAP use a naming convention of being named as <key_type_name><value_type_name>, followed by H. For example, a hash table with integer key and string values is named TIntStrH. If <key_type_name> and <value_type_name> have the same type, only one type name might be used, such as TIntH.

The following THash types are supported: TIntH, TIntIntH, TIntFltH, TIntStrH, TIntPrFltH, TUInt64H, TIntBoolH, TIntUInt64H, TIntIntVH, TIntIntHH, TIntFltPrH, TIntFltTrH, TIntFltVH, TIntStrVH, TIntIntPrH, TIntIntPrVH, TUInt64StrVH, TIntPrIntH, TIntPrIntPrVH, TIntTrIntH, TIntVIntH, TUIntH, TIntPrIntVH, TIntTrFltH, TIntPrStrH, TIntPrStrVH, TIntStrPrIntH, TFltFltH, TStrH, TStrBoolH, TStrIntH, TStrIntPrH, TStrIntVH, TStrUInt64H, TStrUInt64VH, TStrIntPrVH, TStrFltH, TStrFltVH, TStrStrH, TStrStrPrH, TStrStrVH, TStrStrPrVH, TStrStrKdVH, TStrIntFltPrH, TStrStrIntPrVH, TStrStrIntKdVH, TStrPrBoolH, TStrPrIntH, TStrPrFltH, TStrPrStrH, TStrPrStrVH, TStrTrIntH, TStrIntPrIntH, TStrVH, TStrVIntVH, TStrVStrH, and TStrVStrVH.

To illustrate, the following examples show how to create a THash with each of the constructors:

>>> snap.TIntH()
>>> h1 = snap.TIntH(5)
>>> h1[5] = 5
>>> h2 = snap.TIntH(h1)
>>> for key in h2:
...     print key, h2[key]
...
5 5

THash offers iterators of type THashKeyDatI for fast access through the hash table.

The following public functions are supported by the THash class:

H[Key]

Returns the item of H with key Key.

H[Key] = Value

Set H[Key] to Value.

del H[Key]

Removes H[Key] from H.

Key in H

Return True if Key is a key in hash table H, else False.

Key not in H

Equivalent to not Key in H.

Load(SIn)

Loads the hash table from a binary stream SIn.

Save(SOut)

Saves the hash table to a binary stream SOut.

GetMemUsed()

Returns the size of the hash table in bytes.

BegI()

Returns an iterator to the beginning of the hash table.

EndI()

Returns an iterator to the past-the-end element of the hash table.

GetI(Key)

Returns an iterator starting at the node with key value Key.

Clr(DoDel=True, NoDelLim=-1, ResetDat=True)

Clears the contents of the hash table.

Empty()

Returns a boolean indicating whether the hash table is empty.

Len()

Returns the the number of key-value pairs in the hash table.

GetPorts()

Returns the number of ports.

IsAutoSize()

Returns whether it is auto-size, meaning the hash table can be resized.

GetMxKeyIds()

Returns the first key id that is larger than all those currently stored in the hash table.

GetReservedKeyIds()

Returns the size of the allocated storage capacity for the hash table.

IsKeyIdEqKeyN()

Returns a boolean whether there have been any gaps in the key ids, which can occur if a key is deleted and the hash table has not been defraged.

AddKey(Key)

Adds key Key to the hash table and returns the key id.

AddDatId(Key)

Adds a key-value mapping to the hash table, using Key as the key and the key id of Key as the value. The value is then returned.

AddDat(Key)

Adds a key-value mapping to the hash table, using Key as the key and the default value for the datatype as the value (i.e. for TInt values, the default value would be 0) if Key was not already in the hash table. If Key was already in the hash table, the value remains unchanged. The value is then returned.

AddDat(Key, Dat)

Adds a key-value mapping to the hash table, using Key as the key and Dat as the value. The value, Dat, is then returned.

DelKey(Key)

Removes the mapping using Key as the key from the hash table. Raises an exception if Key is not a key in the hash table.

DelIfKey(Key)

Removes the mapping using Key as the key from the hash table if it exists. Returns a boolean indicating whether Key was a key in the hash table.

DelKeyId(KeyId)

Removes the mapping using the key with id KeyId from the hash table. Raises an exception if KeyId is not a valid id for a key in the hash table.

DelKeyIdV(KeyIdV)

Removes all the mappings that use a key with an id in KeyIdV from the hash table. Raises an exception if one of the key ids in KeyIdV is not a valid id for a key in the hash table.

GetKey(KeyId)

Returns the key with id KeyId.

GetKeyId(Key)

Returns the key id for key Key.

GetRndKeyId(Rnd)

Get the index of a random key. If the hash table has many deleted keys, this may take a long time.

GetRndKeyId (Rnd, EmptyFrac)

Get the index of a random key. If the hash table has many deleted keys, defrag the hash table first.

IsKey(Key)

Returns a bool indicating whether Key is a key in the hash table.

IsKeyId(KeyId)

Returns a bool indicating whether there is a key in the hash table with id KeyId.

GetDat(Key)

Returns the value in the hash table that Key maps to.

FFirstKeyId()

Returns 1 less than the smallest key id.

GetKeyV(KeyV)

Adds all the keys in the hash table to the vector KeyV.

GetDatV(DatV)

Adds all the values/data in the hash table to the vector DatV.

GetKeyDatPrV(KeyDatPrV)

Adds all the key-value pairs (as TPair objects) to the vector KeyDatPrV.

GetDatKeyPrV(DatKeyPrV)

Adds all the value-key pairs (as TPair objects) to the vector DatKeyPrV.

GetKeyDatKdV(KeyDatKdV)

Adds all the key-value pairs (as TKeyDat objects) to the vector KeyDatKdV.

GetDatKeyKdV(DatKeyKdV)

Adds all the value-key pairs (as TKeyDat objects) to the vector DatKeyKdV.

Swap(Hash)

Swaps the contents of this hash table with those of Hash.

Defrag()

Defrags the hash table.

Pack()

Reduces the capacity of the memory used to hold the hash table to match its size.

Sort(CmpKey, Asc)

Sorts the hash table. If CmpKey is True, it sorts based on keys rather than values.

SortKey(Asc)

Sorts the hash table based on keys.

SortDat(Asc)

Sorts the hash table based on the values.

Below is some code demonstrating the use of the THash type:

>>> h1 = snap.TIntH()
>>> for i in range(10):
...     h1[i] = i + 1
...
>>> h2 = snap.TIntH(h1)
>>> del h2[0]
>>> h1.Swap(h2)
>>> h1.IsKey(0)
False

TKeyDat

Object used to represent the key-value pairs in a THash object.

class TKeyDat
class TKeyDat(KeyDat)
class TKeyDat(Key)
class TKeyDat(Key, Dat)
class TKeyDat(SIn)

Creates a TKeyDat object. If KeyDat is provided, which is of type TKeyDat, its contents will be copied into the newly created object. If Key and/or Dat are provided, the key for TKeyDat will be set to Key and the value will be set to Dat. If SIn is provided, the contents of the TKeyDat will be read from the stream.

The TKeyDat constructor cannot be directly called. To create a TKeyDat object, the correct constructor must be chosen, which indicates the types of the key and value in the TKeyDat. Key-value pair types in Snap.py and SNAP use a naming convention of being named as <key_type_name><value_type_name>, followed by Kd. For example, a hash table with integer key and string values is named TIntStrKd. If <key_type_name> and <value_type_name> have the same type, only one type name might be used, such as TIntKd.

The following TKeyDat types are supported: TIntKd, TIntUInt64Kd, TIntPrFltKdKd, TIntFltPrKd, TIntSFltKd, TIntStrKd, TUIntIntKd, TUIntKd, TUInt64IntKd, TUInt64FltKd, TUInt64StrKd, TFltBoolKd, TFltIntKd, TFltUInt64Kd, TFltIntPrKd, TFltUIntKd, TFltKd, TFltStrKd, TFltBoolKd, TFloatIntBoolPrKd, TAscFltIntKd, TStrBoolKd, TStrIntKd, TStrFltKd, TStrAscFltKd, TStrKd, and TIntFltKd.

The following public functions are supported by the TKeyDat class:

Save(SOut)

Saves the contents to the binary stream SOut

GetPrimHashCd()

Returns the primary hash code.

GetSecHashCd()

Returns the secondary hash code.

The following public attributes are available:

Key

The key in the key-value pair. Currently does not support assignment.

Dat

The value in the key-value pair. Currently does not support assignment.

THashKeyDatI

An iterator over the values in a THash object. Normally, these objects are not created directly, but via a call to the hash table class THash method, such as BegI().

class THashKeyDatI
class THashKeyDatI(HashKeyDatI)

Creates a THashKeyDatI iterator object. The contents of HashKeyDatI, if provided, will be copied into the iterator.

The THashKeyDatI constructor cannot be directly called. To create a THashKeyDatI object, the correct constructor must be chosen, which indicates the types of the key and value. Hash table iterator types in Snap.py and SNAP use a naming convention of being named as <key_type_name><value_type_name>, followed by HI. For example, a hash table iterator with integer key and string values is named TIntStrHI. If <key_type_name> and <value_type_name> have the same type, only one type name might be used, such as TIntHI.

The following iterator types are currently supported: TIntHI, TIntIntHI, TIntFltHI, TIntStrHI, TIntPrFltHI, TUInt64HI, TIntBoolHI, TIntUint64HI, TIntIntVHI, TIntIntHHI, TIntFltPrHI, TIntFltTrHI, TIntFltVHI, TIntStrVHI, TIntIntPrHI, TIntIntPrVHI, TUInt64StrVHI, TIntPrIntPrVHI, TIntTrIntHI, TIntVIntHI, TUIntHI, TIntPrIntHI, TIntPrIntVHI, TIntTrFltHI, TIntPrStrHI, TIntPrStrVHI, TIntStrPrIntHI, TFltFltHI, TStrHI, TStrBoolHI, TStrIntHI, TStrIntPrHI, TStrIntVHI, TStrUInt64HI, TStrUInt64VHI, TStrIntPrVHI, TStrFltHI, TStrFltVHI, TStrStrHI, TStrStrPrHI, TStrStrVHI, TStrStrPrVHI, TStrStrKdVHI, TStrIntFltPrHI, TStrStrIntPrVHI, TStrStrIntKdVHI, TStrPrBoolHI, TStrPrIntHI, TStrPrFltHI, TStrPrStrHI, TStrPrStrVHI, TStrTrIntHI, TStrIntPrIntHI, TStrVHI, TStrVStrHI, and TStrVStrVHI.

The following public functions are supported by the THashKeyDatI class:

Next()

Returns an iterator starting at the next key-value pair in the THash.

IsEmpty()

Returns a bool indicating whether the iterator is empty.

IsEnd()

Returns a bool indicating whether the end of the iterator has been reached.

GetKey()

Get the key for the key-value pair at the current position in the iterator.

GetDat()

Get the value for the key-value pair at the current position in the iterator.

Below is some code demonstrating the use of the THashKeyDatI type:

>>> h1 = snap.TIntH()
>>> for i in range(5):
...     h1[i] = i + 1
...
>>> it = h1.BegI()
>>> while not it.IsEnd():
>>>     print it.GetKey(), it.GetDat()
>>>     it = it.Next()
0 1
1 2
2 3
3 4
4 5

THashSet

Hash sets contain keys are of the same type. Specific keys can be accessed through their key ids. New values can be added to the hash set only if they are unique. All objects classified as THashSet objects have access to the following methods.

class THashSet
class THashSet(ExpectVals, AutoSizeP=False)
class THashSet(KeyV)
class THashSet(SIn)

Creates a THashSet object with a capacity of ExpectVals, if specified. If KeyV is provided, which should hold the same type of object the hash set holds, a hash set with the unique values in the vector is created. If SIn is provided, the contents of the hash set are loaded from the binary stream SIn.

The THashSet constructor cannot be directly called. To create a THashSet object, the correct constructor must be chosen, which indicates the type of the key in the hash set. Hash set types in Snap.py and SNAP use a naming convention of being named as <key_type_name>, followed by Set. For example, a hash set with integer key is named TIntSet.

The only THashSet currently supported is TIntSet.

To illustrate, the following examples show how to create a THashSet with each of the constructors:

>>> snap.TIntSet()
>>> snap.TIntSet(5)
>>> v = snap.TIntV()
>>> for i in range(5):
...     v.Add(i)
...     v.Add(i)
...
>>> hs = snap.TIntSet(v)
>>> for key in hs:
...     print key
...
0
1
2
3
4

For fast access through the hashset, iterators of type THashSetKeyI are provided.

The following public functions are supported by the THashSet class:

Key in HS

Return True if Key is a key in hash set HS, else False.

Key not in HS

Equivalent to not Key in HS.

GetSet(Key1)
GetSet(Key1, Key2)
GetSet(Key1, Key2, Key3)
GetSet(Key1, Key2, Key3, Key4)
GetSet(Key1, Key2, Key3, Key4, Key5)
GetSet(Key1, Key2, Key3, Key4, Key5, Key6)
GetSet(Key1, Key2, Key3, Key4, Key5, Key6, Key7)
GetSet(Key1, Key2, Key3, Key4, Key5, Key6, Key7, Key8)
GetSet(Key1, Key2, Key3, Key4, Key5, Key6, Key7, Key8, Key9)

Returns a hash set with the given keys.

Load(SIn)

Loads the hash set from a binary stream SIn.

Save(SOut)

Saves the hash set to a binary stream SOut.

GetMemUsed()

Returns the size of the hash set in bytes.

BegI()

Returns an iterator pointing to the first element in the hash set.

EndI()

Returns an iterator referring to the past-the-end element in the hash set.

Gen(ExpVals)

Clears the hash set and resizes it with a capacity of at least ExpVals.

Clr()

Clears the contents of the hash set.

Empty()

Returns a bool indicating whether the hash set is empty.

Len()

Returns the number of keys in the hash set.

GetPorts()

Returns the number of ports.

IsAutoSize()

Returns a bool indicating whether it is auto-size, meaning the hash set can be resized.

GetMxKeyIds()

Returns the first key id that is larger than all those currently stored in the hash set.

GetReservedKeyIds()

Returns the size of the allocated storage capacity for the hash set.

IsKeyIdEqKeyN()

Returns a boolean whether there have been any gaps in the key ids, which can occur if a key is deleted and the hash set has not been defraged.

AddKey(Key)

Adds key Key to the hash set, if it not already in the hash set, and returns the key id.

AddKeyV(KeyV)

Adds each key in KeyV not already in the hash set to the hash set.

DelKey(Key)

Removes Key from the hash set. Raises an exception if Key is not a key in the hash set.

DelIfKey(Key)

Removes Key from the hash set. Returns a boolean indicating whether Key was a key in the hash set.

DelKeyId(KeyId)

Removes the key with id KeyId from the hash set. Raises an exception if KeyId is not a valid id for a key in the hash set.

DelKeyIdV(KeyIdV)

Removes all the keys with an id in KeyIdV from the hash set. Raises an exception if one of the key ids in KeyIdV is not a valid id for a key in the hash set.

GetKey(KeyId)

Returns the key with id KeyId.

GetKeyId(Key)

Returns the key id for key Key.

GetRndKeyId(Rnd)

Get an index of a random key. If the hash set has many deleted keys, this may take a long time.

IsKey(Key)

Returns a bool indicating whether Key is a key in the hash set.

IsKeyId(KeyId)

Returns a bool indicating whether there is a key in the hash table with id KeyId.

GetDat(Key)

Returns the value in the hash table that Key maps to.

FFirstKeyId()

Returns 1 less than the smallest key id.

GetKeyV(KeyV)

Adds all the keys in the hash table to the vector KeyV.

Swap(Set)

Swaps the contents of this hash set with those of Set.

Defrag()

Defrags the hash set.

Pack()

Reduces the capacity of the memory used to hold the hash set to match its size.

Below is some code demonstrating the use of the THashSet type:

>>> hs = snap.TIntSet()
>>> for i in range(30):
...     hs.AddKey(i)
...
>>> hs.IsKey(0)
True
>>> v = snap.TIntV()
>>> hs.GetKeyV(v)

THashSetKeyI

An iterator over the values in a THashSet object. Normally, these objects are not created directly, but via a call to the hash table class THashSet method, such as BegI().

class THashSetKeyI
class THashSetKeyI(SetKeyI)

Creates a THashSetKeyI iterator object. The contents of SetKeyI, if provided, will be copied into the iterator.

The THashSetKeyI constructor cannot be directly called. To create a THashSetKeyI object, the correct constructor must be chosen, which indicates the type of the key in the hash set iterator. Hash set iterator types in Snap.py and SNAP use a naming convention of being named as <key_type_name>, followed by HSI. For example, a hash set iterator with integer key is named TIntHSI.

The following iterator types are currently supported: TIntHSI.

The following public functions are supported by the THashSetKeyI class:

GetKey()

Get the key at the current position in the iterator.

Below is some code demonstrating the use of the THashSetKeyI type:

>>> hs1 = snap.TIntSet()
>>> hs1.AddKey(0)
>>> it = hs1.BegI()
>>> print it.GetKey()
0