\seekquarry\yioop\libraryPriorityQueue

Code used to manage a memory efficient priority queue.

Weights for the queue must be flaots. The queue itself is implemented using heaps

Summary

Methods
Properties
Constants
__construct()
load()
save()
get()
put()
checkSave()
peek()
poll()
insert()
adjustWeight()
printContents()
getContents()
normalize()
percolateUp()
percolateDown()
compare()
getRow()
putRow()
totalWeight()
$num_values
$data_size
$string_array_size
$string_array
$filename
$unsaved_operations
$save_frequency
$value_size
$weight_size
$count
$min_or_max
$notifier
$adjust_weight_callback
DEFAULT_SAVE_FREQUENCY
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

Properties

$num_values

$num_values : integer

Number of values that can be stored in the priority queue

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

$value_size

$value_size : integer

Number of bytes needed to store a value associated with a weight

Type

integer

$weight_size

$weight_size : integer

Number of bytes needed to store a weight in the queue

Type

integer

$count

$count : integer

Number of items that are currently stored in the queue

Type

integer

$min_or_max

$min_or_max : string

When the polling the queue returns the least or most weighted value

Type

string

$notifier

$notifier : object

An object that implements the Notifier interface (for instance, WebQueueArchive)

Type

object

$adjust_weight_callback

$adjust_weight_callback : string

Name of a function to be called if an element in the priority has moved because its weight was adjusted this allows other data structures the ability to know where the element has moved to in the priority queue

Type

string

Methods

__construct()

__construct(string  $fname, integer  $num_values, integer  $value_size, string  $min_or_max, object  $notifier = null, integer  $save_frequency = self::DEFAULT_SAVE_FREQUENCY, string  $adjust_weight_callback = null) 

Makes a priority queue (implemented as an array heap) with the given operating parameters

Parameters

string $fname

filename to store the data associated with the queue

integer $num_values

number of values the queue can hold

integer $value_size

the size in a bytes of a value

string $min_or_max

whether this priority queue return least or most weight values when polled

object $notifier

object to call when a value changes in the queue

integer $save_frequency

how often the data in the queue should be save to disk. (It's default location is RAM)

string $adjust_weight_callback

name of a function to be called if an element in the priority has moved because its weight was adjusted this allows other data structures the ability to know where the element has moved to in the priority queue

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

peek()

peek(integer  $i = 1) : mixed

Gets the data stored at the ith location in the priority queue

Parameters

integer $i

location to return data from

Returns

mixed —

array data if the value of $i is between 1 and count, false otherwise

poll()

poll(integer  $i = 1) : mixed

Removes and returns the ith element out of the Priority queue.

Since this is a priority queue the first element in the queue will either be the min or max (depending on queue type) element stored. If $i is not in range an error message is written to the log. This operation also performs a check to see if the queue should be saved to disk

Parameters

integer $i

element to get out of the queue

Returns

mixed —

array data if the value of $i is between 1 and count, false otherwise

insert()

insert(string  $data, integer  $weight) : mixed

Inserts a new item into the priority queue.

Parameters

string $data

what to insert into the queue

integer $weight

how much the new data should be weighted

Returns

mixed —

index location in queue where item was stored if successful, otherwise false.

adjustWeight()

adjustWeight(integer  $i, integer  $delta) 

Add $delta to the $ith element in the priority queue and then adjusts the queue to store the heap property

Parameters

integer $i

element whose weight should be adjusted

integer $delta

how much to change the weight by

printContents()

printContents() 

Pretty prints the contents of the queue viewed as an array.

getContents()

getContents() : array

Return the contents of the priority queue as an array of value weight pairs.

Returns

array —

contents of the queue

normalize()

normalize(integer  $new_total = \seekquarry\yioop\configs\NUM_URLS_QUEUE_RAM) 

Scaless the weights of elements in the queue so that the sum fo the new weights is $new_total

This function is used periodically to prevent the queue from being gummed up because all of the weights stored in it are too small.

Parameters

integer $new_total

what the new sum of weights of elements in the queue will be after normalization

percolateUp()

percolateUp(integer  $i) : integer

If the $ith element in the PriorityQueue violates the heap property with its parent node (children should be of lower priority than the parent), this function tries modify the heap to restore the heap property.

Parameters

integer $i

node to consider in restoring the heap property

Returns

integer —

final position $ith node ends up at

percolateDown()

percolateDown(integer  $i) 

If the ith element in the PriorityQueue violates the heap property with some child node (children should be of lower priority than the parent), this function tries modify the heap to restore the heap property.

Parameters

integer $i

node to consider in restoring the heap property

compare()

compare(integer  $value1, integer  $value2) : integer

Computes the difference of the two values $value1 and $value2

Which is subtracted from which is determined by whether this is a min_or_max priority queue

Parameters

integer $value1

a value to take the difference between

integer $value2

the other value

Returns

integer —

the differences

getRow()

getRow(integer  $i) : array

Gets the ith element of the PriorityQueue viewed as an array

Parameters

integer $i

element to get

Returns

array —

value stored in queue together with its weight as a two element array

putRow()

putRow(integer  $i, array  $row) 

Add data to the $i row of the priority queue viewed as an array Calls the notifier associated with this queue about the change in data's location

Parameters

integer $i

location to add data

array $row

data to add (a two element array in the form key, int value).

totalWeight()

totalWeight() : integer

Computes and returns the weight of all items in prority queue

Returns

integer —

weight of all items stored in the priority queue