\seekquarry\yioop\libraryHashTable

Code used to manage a memory efficient hash table Weights for the queue must be flaots

The standard array objects in php and even spl have a large amount of overhead. The point of this class is to have the size as close to the optimal as possible

Summary

Methods
Properties
Constants
__construct()
load()
save()
get()
put()
checkSave()
insert()
lookup()
lookupArray()
delete()
getEntry()
hash()
printContents()
$num_values
$data_size
$string_array_size
$string_array
$filename
$unsaved_operations
$save_frequency
$key_size
$value_size
$null
$deleted
$count
DEFAULT_SAVE_FREQUENCY
ALWAYS_RETURN_PROBE
RETURN_PROBE_ON_KEY_FOUND
RETURN_VALUE
RETURN_BOTH
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Constants

DEFAULT_SAVE_FREQUENCY

DEFAULT_SAVE_FREQUENCY

If not specified in the constructor, this will be the number of operations between saves

ALWAYS_RETURN_PROBE

ALWAYS_RETURN_PROBE

Flag for hash table lookup methods

RETURN_PROBE_ON_KEY_FOUND

RETURN_PROBE_ON_KEY_FOUND

Flag for hash table lookup methods

RETURN_VALUE

RETURN_VALUE

Flag for hash table lookup methods

RETURN_BOTH

RETURN_BOTH

Flag for hash table lookup methods

Properties

$num_values

$num_values : integer

Number of items to be stored in the StringArray

Type

integer

$data_size

$data_size : integer

Size of each item in bytes to be stored

Type

integer

$string_array_size

$string_array_size : integer

Number of bytes of storage need by the string array

Type

integer

$string_array

$string_array : string

Character string used to store the packed data of the StringArray

Type

string

$filename

$filename : string

Name of the file in which to store the PersistentStructure

Type

string

$unsaved_operations

$unsaved_operations : integer

Number of operations since the last save

Type

integer

$save_frequency

$save_frequency : integer

Number of operation between saves. If == -1 never save using checkSave

Type

integer

$key_size

$key_size : integer

The size in bytes for keys stored in the hash table

Type

integer

$value_size

$value_size : integer

The size in bytes of values associated with values

Type

integer

$null

$null : string

Holds an all \0 string used of length $this->key_size

Type

string

$deleted

$deleted : string

Holds \0\0 followed by an all \FF string of length $this->key_size -1 Used to indicate that a slot once held data but that data was deleted.

Such a slot tells a lookup to keep going, but on an insert can be overwritten in the inserted key is not already in the table

Type

string

$count

$count : integer

Number of items currently in the hash table

Type

integer

Methods

__construct()

__construct(string  $fname, integer  $num_values, integer  $key_size, integer  $value_size, integer  $save_frequency = self::DEFAULT_SAVE_FREQUENCY) 

Makes a persistently stored (i.e., on disk and ram) hash table using the supplied parameters

Parameters

string $fname

filename to use when storing the hash table to disk

integer $num_values

number of key value pairs the table can hold

integer $key_size

number of bytes to store a hash table key

integer $value_size

number of bytes to store a hash table value

integer $save_frequency

how many non read operation before saving to disk

load()

load(string  $fname) : object

Load a PersistentStructure from a file

Parameters

string $fname

the name of the file to load the PersistentStructure from

Returns

object —

the PersistentStructure loaded

save()

save() 

Save the PersistentStructure to its filename This method is generic but super memory inefficient, so reimplement for subclasses is needed

get()

get(integer  $i) : string

Looks up the ith item in the StringArray

Parameters

integer $i

array index of item to look up

Returns

string —

the looked-up item of length $this->data_size

put()

put(integer  $i, string  $data) 

Puts data into the ith item of the StringArray

Parameters

integer $i

array index of where to store data

string $data

at least $this->data_size many bytes of data to store

checkSave()

checkSave() 

Add one to the unsaved_operations count. If this goes above the save_frquency then save the PersistentStructure to secondary storage

insert()

insert(string  $key, string  $value, integer  $probe = false) : boolean

Inserts the provided $key - $value pair into the hash table

Parameters

string $key

the key to use for the insert (will be needed for lookup)

string $value

the value associated with $key

integer $probe

if the location in the hash table is already known to be $probe then this variable can be used to save a lookup

Returns

boolean —

whether the insert was successful or not

lookup()

lookup(string  $key, integer  $return_probe_value = self::RETURN_VALUE) : mixed

Tries to lookup the key in the hash table either return the location where it was found or the value associated with the key.

Parameters

string $key

key to look up in the hash table

integer $return_probe_value

one of self::ALWAYS_RETURN_PROBE, self::RETURN_PROBE_ON_KEY_FOUND, self::RETURN_VALUE, or self::BOTH. Here value means the value associated with the key and probe is either the location in the array where the key was found or the first location in the array where it was determined the key could not be found.

Returns

mixed —

would be string if the value is being returned, an int if the probe is being returned, and false if the key is not found

lookupArray()

lookupArray(string  $key, array  $null_array, integer  $return_probe_value = self::RETURN_VALUE) : mixed

Tries to lookup the key in the hash table either return the location where it was found or the value associated with the key.

If the key is not at the initial probe value, linear search in the table is done. The values which cut-off the search are stored in $null_array. Using an array allows for flexibility since a deleted entry needs to be handled different when doing a lookup then when doing an insert.

Parameters

string $key

key to look up in the hash table

array $null_array

key values that would cut-off the search for key if the initial probe failed

integer $return_probe_value

one of self::ALWAYS_RETURN_PROBE, self::RETURN_PROBE_ON_KEY_FOUND, or self::RETURN_VALUE. Here value means the value associated with the key and probe is either the location in the array where the key was found or the first location in the array where it was determined the key could not be found.

Returns

mixed —

would be string if the value is being returned, an int if the probe is being returned, and false if the key is not found

delete()

delete(string  $key, integer  $probe = false) : boolean

Deletes the data associated with the provided key from the hash table

Parameters

string $key

the key to delete the entry for

integer $probe

if the location in the hash table is already known to be $probe then this variable can be used to save a lookup

Returns

boolean —

whether or not something was deleted

getEntry()

getEntry(integer  $i) : array

Get the ith entry of the array for the hash table (no hashing here)

Parameters

integer $i

an index of the hash table array

Returns

array —

the key value pair stored at this index

hash()

hash(string  $key) : integer

Hashes the provided key to an index in the array of the hash table

Parameters

string $key

a key to hashed into the hash table

Returns

integer —

an index in the array of the hash table

printContents()

printContents() 

Pretty prints the contents of the hash table viewed as an array.