GCGlib  0.04.228
GCG Graphics Engine
gcgPATRICIAMAP Class Reference

Concrete class that implements a PATRICIA tree for key/value mappings. Insertion or search for a random key in a PATRICIA trie built from N random bitstrings requires about log N bit comparisons on the average, and about 2 log N bit comparisons in the worst case. The number of bit comparisons is never more than the length of the key. PATRICIA tree (“Practical Algorithm To Retrieve Information Coded In Alphanumeric”) was proposed by Morrison in 1968. In GCGlib, we use the internal organization proposed in "Chapter 15: Radix Search" of the course of Algorithms and Data Structures, Princeton University, avaliable at http://www.cs.princeton.edu/courses/archive/spring09/cos226/handouts/Algs3Ch15.pdf and developed by Robert Sedgewick since 1978. It has only one type of nodes and has no one-way branching. The remove operation was implemented using the logic presented in the book "A Practical Guide to Data Structures and Algorithms using Java", Sally A. Goldman and Kenneth J. Goldman, Chapman & Hall/CRC, 2008, ISBN‑13: 978‑1‑58488‑455‑2. The gcgPATRICIAMAP can be used with digital keys of any size. A key is a unique digital sequence that refers to a gcgDATA pointer. The bitstrings are scanned in MSB first order (lexicographic). To use gcgPATRICIAMAP, the programmer must first define the key size keysize. If keysize size is 0, the keys in the data map are null terminated strings. Otherwise, the keys have fixed size of keysize bytes. If keysize is 0 (for null terminated strings) or greater than sizeof(void*) (for very long fixed size keys), only the pointer to the actual key content is stored in the tree. Thus, in that cases, the keys are passed by reference in the methods parameters. To enhance performance, if keysize is less than or equal to sizeof(long) bytes, the keys are passed by value in the parameters and are copied internally. This results in much faster searchings in the tree. The stored key pointers (when key size is 0 or greater than sizeof(intptr_t), i.e. integers that fit within the same number of bits of a pointer) are never deleted or freed by gcgPATRICIAMAP. Use gcgPATRICIAMAP when you need key/value mappings where the key has variable size (strings) or has long fixed size. More...

#include <gcg.h>

Inheritance diagram for gcgPATRICIAMAP:
gcgDATAMAP gcgDATASTRUCTURE gcgCLASS

Public Member Functions

 gcgPATRICIAMAP (unsigned int keysize)
 Constructs a valid PATRICIA tree. More...
 
virtual ~gcgPATRICIAMAP ()
 Destroys the PATRICIA tree, releasing all internal nodes. All gcgDATA values are deleted. The stored key pointers (when keysize is 0 or greater than sizeof(intptr_t)), are never deleted or freed. More...
 
bool setKeySize (unsigned int keysize)
 Sets the key size of an empty PATRICIA tree. If the current PATRICIA tree is in use, i.e. has nodes with different key size than the new keysize, an error is reported and setKeySize() returns false. More...
 
gcgITERATORsuffixIterator (GCGDIGITALKEY prefixkey, unsigned int prefixsize=0, int traversemode=0)
 Returns an iterator for traversing the key/value pairs of the PATRICIA tree whose prefixes are prefixkey. The prefix size in bytes or bits are defined by prefixsize. The traversal order is defined by the parameter traversemode. Currently, the key/value pairs can be traversed in two ways: traversemode = 0, to iterate in ascending order (default) of digital keys; and traversemode = 1, to iterate in descending order of digital keys. The PATRICIA tree must not be changed by insertions or removals while the iterator is in use. You must delete the returned iterator after use. The iterator copies a minimal information from the PATRICIA tree. Note that deleting a iterator does not affect the internal PATRICIA nodes. More...
 
uintptr_t getCounter ()
 Returns the number of key/value pairs stored in the PATRICIA tree. More...
 
bool deleteAll ()
 Deletes and removes all internal entries from the PATRICIA tree. It also deletes all stored gcgDATA pointers. The stored key pointers (when key size is 0 or greater than sizeof(intptr_t)), are never deleted or freed. All resources used by the PATRICIA tree are also released. More...
 
gcgITERATORgetIterator (int traversemode=0)
 Returns an iterator for traversing the key/value pairs of the PATRICIA tree. The traversal order is defined by the parameter traversemode. Currently, the key/value pairs can be traversed in two ways: traversemode = 0, to iterate in ascending order (default) of digital keys; and traversemode = 1, to iterate in descending order of digital keys. The PATRICIA tree must not be changed by insertions or removals while the iterator is being used. You must delete the returned iterator after use. The iterator copies a minimal information from the PATRICIA tree. Note that deleting a iterator does not affect the internal PATRICIA nodes. More...
 
gcgITERATORdetach (int traversemode=0)
 Force the PATRICIA tree to be empty. It has the same effect as removing all key/pairs from the PATRICIA tree but keeping their chaining (which will used by the returned iterator). The key/value pairs are NOT deleted. In order to retrieve the detached key/value pairs, a gcgITERATOR is returned. This is useful to process the data freely and without the restrictions of the underlying PATRICIA tree. Thus, the gcgITERATOR returned by gcgPATRICIAMAP::detach() MUST be used to delete all the gcgDATA or reinsert them into another data map or data structure. The traversal order of the returned iterator is defined by the parameter traversemode. Currently, the PATRICIA key/value pairs can be traversed in two ways: traversemode = 0, to iterate in ascending order (default) of digital keys; and traversemode = 1, to iterate in descending order of digital keys. All resources currently used by the PATRICIA tree are released (becomes empty) but the instance can be reused normally. You must delete the gcgITERATOR after its use. More...
 
virtual gcgDATAinsert (GCGDIGITALKEY key, gcgDATA *value)
 Inserts a new pair key/value in the PATRICIA tree. The keys must be unique in the digital sense (they have the same size and not all bits are equal). More...
 
virtual gcgDATAsearch (GCGDIGITALKEY key)
 Retrieves the gcgDATA pointer value from the PATRICIA tree that is similar in the digital sense (they have the same size and all bits are equal) to the key. More...
 
virtual gcgDATAremove (GCGDIGITALKEY key)
 Removes the key/value entry from the PATRICIA tree that has the key similar to the key in the digital sense (they have the same size and all bits are equal). More...
 
- Public Member Functions inherited from gcgCLASS
void * operator new (size_t size)
 Defines a new operator to be used by instatiations of GCGlib classes instead the global one. More...
 
void * operator new (size_t size, const std::nothrow_t &) throw ()
 Defines a new operator to be used by instantiations of GCGlib classes instead the global one. Returns a NULL pointer instead of throwing an exception if an error occurs. More...
 
void * operator new[] (size_t size)
 Defines a new operator to be used by GCGlib array allocations instead the global one. More...
 
void * operator new[] (size_t size, const std::nothrow_t &) throw ()
 Defines a new operator to be used by vector allocations instead the global one. More...
 
void operator delete (void *p)
 Defines a delete operator to free instances of GCGlib classes instead the global one. It is designed to match the new operator. More...
 
void operator delete (void *p, const std::nothrow_t &) throw ()
 Defines a delete operator to free instances of GCGlib classes instead the global one. It is designed to match the new operator. More...
 
void operator delete[] (void *p)
 Defines a delete operator to free instances of arrays for GCGlib classes instead the global one. It is designed to match the new[] operator. More...
 
void operator delete[] (void *p, const std::nothrow_t &) throw ()
 Defines a delete operator to free instances of arrays for GCGlib classes instead the global one. It is designed to match the new[] operator. More...
 

Public Attributes

uintptr_t counter
 Number of elements in this tree. Read-only.
 
unsigned int keysize
 Key size in bytes. If zero, the keys are null terminated string. Read-only.
 

Private Attributes

struct _GCG_PATRICIA_NODE * root
 Root node of the PATRICIA tree: internal only.
 

Detailed Description

Concrete class that implements a PATRICIA tree for key/value mappings. Insertion or search for a random key in a PATRICIA trie built from N random bitstrings requires about log N bit comparisons on the average, and about 2 log N bit comparisons in the worst case. The number of bit comparisons is never more than the length of the key. PATRICIA tree (“Practical Algorithm To Retrieve Information Coded In Alphanumeric”) was proposed by Morrison in 1968. In GCGlib, we use the internal organization proposed in "Chapter 15: Radix Search" of the course of Algorithms and Data Structures, Princeton University, avaliable at http://www.cs.princeton.edu/courses/archive/spring09/cos226/handouts/Algs3Ch15.pdf and developed by Robert Sedgewick since 1978. It has only one type of nodes and has no one-way branching. The remove operation was implemented using the logic presented in the book "A Practical Guide to Data Structures and Algorithms using Java", Sally A. Goldman and Kenneth J. Goldman, Chapman & Hall/CRC, 2008, ISBN‑13: 978‑1‑58488‑455‑2. The gcgPATRICIAMAP can be used with digital keys of any size. A key is a unique digital sequence that refers to a gcgDATA pointer. The bitstrings are scanned in MSB first order (lexicographic). To use gcgPATRICIAMAP, the programmer must first define the key size keysize. If keysize size is 0, the keys in the data map are null terminated strings. Otherwise, the keys have fixed size of keysize bytes. If keysize is 0 (for null terminated strings) or greater than sizeof(void*) (for very long fixed size keys), only the pointer to the actual key content is stored in the tree. Thus, in that cases, the keys are passed by reference in the methods parameters. To enhance performance, if keysize is less than or equal to sizeof(long) bytes, the keys are passed by value in the parameters and are copied internally. This results in much faster searchings in the tree. The stored key pointers (when key size is 0 or greater than sizeof(intptr_t), i.e. integers that fit within the same number of bits of a pointer) are never deleted or freed by gcgPATRICIAMAP. Use gcgPATRICIAMAP when you need key/value mappings where the key has variable size (strings) or has long fixed size.

Since
0.04.211

Constructor & Destructor Documentation

◆ gcgPATRICIAMAP()

gcgPATRICIAMAP::gcgPATRICIAMAP ( unsigned int  keysize)

Constructs a valid PATRICIA tree.

Parameters
[in]keysizeThe size in bytes of the keys to be used in the tree. A key is a unique digital sequence that refers to a gcgDATA pointer. If keysize is 0, the keys in the data map are null terminated strings. Otherwise, the keys have fixed size of keysize bytes. If keysize is 0 (for null terminated strings) or greater than sizeof(intptr_t) (for very long fixed size keys), only the pointer to the actual key content is stored in the tree. Thus, in that cases the keys are passed by reference in the methods parameters. To enhance performance, if keysize is less than or equal to sizeof(intptr_t) bytes, the keys are passed by value in the methods parameters and are copied internally. This results in much faster searchings in the tree. The stored key pointers (when key size is 0 or greater than sizeof(long)), are never deleted or freed by gcgPATRICIAMAP.
See also
~gcgPATRICIAMAP()
setKeySize()

◆ ~gcgPATRICIAMAP()

virtual gcgPATRICIAMAP::~gcgPATRICIAMAP ( )
virtual

Destroys the PATRICIA tree, releasing all internal nodes. All gcgDATA values are deleted. The stored key pointers (when keysize is 0 or greater than sizeof(intptr_t)), are never deleted or freed.

See also
gcgPATRICIAMAP()

Member Function Documentation

◆ deleteAll()

bool gcgPATRICIAMAP::deleteAll ( )
virtual

Deletes and removes all internal entries from the PATRICIA tree. It also deletes all stored gcgDATA pointers. The stored key pointers (when key size is 0 or greater than sizeof(intptr_t)), are never deleted or freed. All resources used by the PATRICIA tree are also released.

Returns
Returns true if all gcgDATA pointers were deleted and all internal entries were deleted and removed from the PATRICIA tree. If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
detach()

Implements gcgDATASTRUCTURE.

◆ detach()

gcgITERATOR* gcgPATRICIAMAP::detach ( int  traversemode = 0)
virtual

Force the PATRICIA tree to be empty. It has the same effect as removing all key/pairs from the PATRICIA tree but keeping their chaining (which will used by the returned iterator). The key/value pairs are NOT deleted. In order to retrieve the detached key/value pairs, a gcgITERATOR is returned. This is useful to process the data freely and without the restrictions of the underlying PATRICIA tree. Thus, the gcgITERATOR returned by gcgPATRICIAMAP::detach() MUST be used to delete all the gcgDATA or reinsert them into another data map or data structure. The traversal order of the returned iterator is defined by the parameter traversemode. Currently, the PATRICIA key/value pairs can be traversed in two ways: traversemode = 0, to iterate in ascending order (default) of digital keys; and traversemode = 1, to iterate in descending order of digital keys. All resources currently used by the PATRICIA tree are released (becomes empty) but the instance can be reused normally. You must delete the gcgITERATOR after its use.

Parameters
traversemodeSpecifies the order that the available elements in the PATRICIA tree must be traversed by the returned iterator. Currently, the tree key/value pairs can be traversed in two ways: traversemode = 0, to iterate in ascending order (default) of digital keys; and traversemode = 1, to iterate in descending order of digital keys.
Returns
Returns an iterator for traversing the key/value pairs that were detached from the PATRICIA tree. The PATRICIA tree becomes empty and can be reused. You MUST delete the returned gcgITERATOR object after use. If it returns NULL, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
deleteAll()
getIterator()

Implements gcgDATASTRUCTURE.

◆ getCounter()

uintptr_t gcgPATRICIAMAP::getCounter ( )
virtual

Returns the number of key/value pairs stored in the PATRICIA tree.

Returns
Returns the number of key/value pairs currently stored in the PATRICIA tree.
See also
insert()
remove()

Implements gcgDATASTRUCTURE.

◆ getIterator()

gcgITERATOR* gcgPATRICIAMAP::getIterator ( int  traversemode = 0)
virtual

Returns an iterator for traversing the key/value pairs of the PATRICIA tree. The traversal order is defined by the parameter traversemode. Currently, the key/value pairs can be traversed in two ways: traversemode = 0, to iterate in ascending order (default) of digital keys; and traversemode = 1, to iterate in descending order of digital keys. The PATRICIA tree must not be changed by insertions or removals while the iterator is being used. You must delete the returned iterator after use. The iterator copies a minimal information from the PATRICIA tree. Note that deleting a iterator does not affect the internal PATRICIA nodes.

Parameters
traversemodeSpecifies the order that the available key/pairs in the PATRICIA tree must be traversed. Currently, the tree nodes can be traversed in two ways: traversemode = 0, to iterate in ascending order (default) of digital keys; and traversemode = 1, to iterate in descending order of digital keys.
Returns
Returns an iterator for traversing the PATRICIA tree in the order specified by traversemode. The PATRICIA tree must not be changed by insertions or removals while the iterator is being used. You MUST delete the returned gcgITERATOR object after use. If it returns NULL, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
detach()
suffixIterator()

Implements gcgDATASTRUCTURE.

◆ insert()

virtual gcgDATA* gcgPATRICIAMAP::insert ( GCGDIGITALKEY  key,
gcgDATA value 
)
virtual

Inserts a new pair key/value in the PATRICIA tree. The keys must be unique in the digital sense (they have the same size and not all bits are equal).

Parameters
[in]keya unique sequence of bytes that refers to the value data. If a key already in the PATRICIA is similar to key in the digital sense (they have the same size and all bits are equal), it returns the pointer of its corresponding gcgDATA instance value. If keysize is 0, the key points to a null terminated string. Otherwise, the keys have fixed size of keysize bytes. If keysize is 0 (for null terminated strings) or greater than sizeof(intptr_t) (for very long fixed size keys), key is a pointer which is actually stored in the PATRICIA tree (the content is not). Thus, in that cases the actual key is passed by reference using key. When used as pointer, the key can point to any memory region holding the digital sequence. However, its content must NEVER change while in use. The behavior of the gcgPATRICIAMAP is undefined if that happens. A common practice is to point to an internal attribute of the gcgDATA instance value. Keys having size up to sizeof(intptr_t), i.e. integers that fit within the same number of bits of a pointer, are copied for performance enhancement. The key parameter must hold the actual binary key value. Thus, it is not used as a pointer. This results in much faster searchings in the tree. For ease of use, several constructors (GCGDIGITALKEY) are provided for integer keys.
[in]valuea pointer to a gcgDATA instance that actually holds information. If key is a pointer, it is recommended to point to a value attribute holding the key.
Returns
if key is similar in the digital sense (they have the same size and all bits are equal) to an existing key/value pair of the data map, the method returns the pointer of the existing gcgDATA value. If key/value pair is correctly inserted, it returns the gcgDATA pointer value. If it returns NULL, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
remove()
search()
setKeySize()

Implements gcgDATAMAP.

◆ remove()

virtual gcgDATA* gcgPATRICIAMAP::remove ( GCGDIGITALKEY  key)
virtual

Removes the key/value entry from the PATRICIA tree that has the key similar to the key in the digital sense (they have the same size and all bits are equal).

Parameters
[in]keya unique sequence of bytes that refers to the value data. If a key already in the PATRICIA is similar to key in the digital sense (they have the same size and all bits are equal), it returns the pointer of its corresponding gcgDATA instance value and removes the internal key/value pair from the PATRICIA tree. If a similar key is not found in the data map, it returns NULL. If keysize is 0, the key points to a null terminated string. Otherwise, the keys have fixed size of keysize bytes. If keysize is 0 (for null terminated strings) or greater than sizeof(void*) (for very long fixed size keys), key is a pointer which is actually stored in the PATRICIA tree (the content is not). Thus, in that cases the actual key is passed by reference using key. When used as pointer, the key can point to any memory region holding the digital sequence. However, its content must NEVER change while in use. The behavior of the gcgPATRICIAMAP is undefined if that happens. A common practice is to point to an internal attribute of the gcgDATA instance value. Keys having size up to sizeof(intptr_t), i.e. integers that fit within the same number of bits of a pointer, are copied for performance enhancement. The key parameter must hold the actual binary key value. Thus, it is not used as a pointer. This results in much faster searchings in the tree. For ease of use, several constructors (GCGDIGITALKEY) are provided for integer keys.
Returns
if key is similar in the digital sense (they have the same size and all bits are equal) to an existing key/value pair of the data map, the method removes the key/value pair from the PATRICIA tree and returns the pointer of the gcgDATA value. If no similar key is found, it returns NULL. Check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem, if any.
See also
insert()
search()

Implements gcgDATAMAP.

◆ search()

virtual gcgDATA* gcgPATRICIAMAP::search ( GCGDIGITALKEY  key)
virtual

Retrieves the gcgDATA pointer value from the PATRICIA tree that is similar in the digital sense (they have the same size and all bits are equal) to the key.

Parameters
[in]keya unique sequence of bytes that refers to the value data. If a key already in the PATRICIA is similar to key in the digital sense (they have the same size and all bits are equal), it returns the pointer of its corresponding gcgDATA instance value. If a similar key is not found in the data map, it returns NULL. If keysize is 0, the key points to a null terminated string. Otherwise, the keys have fixed size of keysize bytes. If keysize is 0 (for null terminated strings) or greater than sizeof(intptr_t) (for very long fixed size keys), key is a pointer which is actually stored in the PATRICIA tree (the content is not). Thus, in that cases the actual key is passed by reference using key. When used as pointer, the key can point to any memory region holding the digital sequence. However, its content must NEVER change while in use. The behavior of the gcgPATRICIAMAP is undefined if that happens. A common practice is to point to an internal attribute of the gcgDATA instance value. Keys having size up to sizeof(intptr_t), i.e. integers that fit within the same number of bits of a pointer, are copied for performance enhancement. The key parameter must hold the actual binary key value. Thus, it is not used as a pointer. This results in much faster searchings in the tree. For ease of use, several constructors (GCGDIGITALKEY) are provided for integer keys.
Returns
if key is similar in the digital sense (they have the same size and all bits are equal) to an existing key/value pair of the data map, the method returns the pointer of the existing gcgDATA value. If no similar key is found, it returns NULL. Check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem, if any.
See also
insert()
remove()

Implements gcgDATAMAP.

◆ setKeySize()

bool gcgPATRICIAMAP::setKeySize ( unsigned int  keysize)

Sets the key size of an empty PATRICIA tree. If the current PATRICIA tree is in use, i.e. has nodes with different key size than the new keysize, an error is reported and setKeySize() returns false.

Parameters
[in]keysizeThe size in bytes of the keys to be used in the tree. A key is a unique digital sequence that refers to a gcgDATA pointer. If keysize is 0, the keys in the data map are null terminated strings. Otherwise, the keys have fixed size of keysize bytes. If keysize is 0 (for null terminated strings) or greater than sizeof(intptr_t) (for very long fixed size keys), only the pointer to the actual key content is stored in the tree. Thus, in that cases the keys are passed by reference in the methods parameters. To enhance performance, if keysize is less than or equal to sizeof(intptr_t) bytes, the keys are passed by value in the methods parameters and are copied internally. This results in much faster searchings in the tree. The stored key pointers (when key size is 0 or greater than sizeof(long)), are never deleted or freed by gcgPATRICIAMAP.
See also
gcgPATRICIAMAP()
Returns
true if the key size was changed. This is only possible if the tree is empty. It there are key/value pairs with different key size, it returns false. If it returns false, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.

◆ suffixIterator()

gcgITERATOR* gcgPATRICIAMAP::suffixIterator ( GCGDIGITALKEY  prefixkey,
unsigned int  prefixsize = 0,
int  traversemode = 0 
)

Returns an iterator for traversing the key/value pairs of the PATRICIA tree whose prefixes are prefixkey. The prefix size in bytes or bits are defined by prefixsize. The traversal order is defined by the parameter traversemode. Currently, the key/value pairs can be traversed in two ways: traversemode = 0, to iterate in ascending order (default) of digital keys; and traversemode = 1, to iterate in descending order of digital keys. The PATRICIA tree must not be changed by insertions or removals while the iterator is in use. You must delete the returned iterator after use. The iterator copies a minimal information from the PATRICIA tree. Note that deleting a iterator does not affect the internal PATRICIA nodes.

Parameters
[in]prefixkeya sequence of bytes that refers to a prefix that the iterated key/value pairs must have. A search is performed in the PATRICIA tree using prefixkey. The node whose key is the smallest containing prefixkey as prefix, limited to prefixsize, is determined. An iterator with this node as root is then created and returned, i.e. the iterator will scan its subtree.
[in]prefixsizeindicates the part of the prefixkey to be considered as prefix. Its interpretation, however, depends entirely on the attribute keysize of current gcgPATRICIAMAP. If keysize is 0, meaning that the keys are null terminated strings, the prefixsize indicates how many BYTES/CHARACTERS of the string pointed by prefixkey should be considered in MSB first order (limited internally by the string length). If zero (default), the length of the string prefixkey is used. If keysize is greater than sizeof(intptr_t), meaning that the keys have fixed size in bytes greater than the machine word size, the prefixsize indicates how many BYTES of the byte array pointed prefixkey should be considered as prefix in MSB first order (it will return NULL if prefixsize is greater than keysize). If zero (default), all keysize bytes are used, i.e. the subtree of the the node with key prefixkey, if it exists, is returned by the iterator. If keysize is non zero and smaller than or equal to sizeof(uintptr_t), meaning that the keys are passed by value as integers with sizeof(intptr_t) bytes, the prefixsize indicates how many BITS of the integer key prefixkey should be considered as prefix in MSB first order (it will return NULL if prefixsize is greater than keysize * CHAR_BIT bits). If zero (default), all bits are considered, i.e. the subtree of the the node with key prefixkey, if it exists, is returned by the iterator.
traversemodeSpecifies the order that the available key/pairs in the PATRICIA tree must be traversed. Currently, the tree nodes can be traversed in two ways: traversemode = 0, to iterate in ascending order (default) of digital keys; and traversemode = 1, to iterate in descending order of digital keys.
Returns
Returns an iterator for traversing the PATRICIA subtree having prefixkey as prefix in the order specified by traversemode. The PATRICIA tree must not be changed by insertions or removals while the iterator is being used. You MUST delete the returned gcgITERATOR object after use. If it returns NULL, check GCG_REPORT_MESSAGE(gcgGetReport()) for knowing the problem.
See also
detach()
getIterator()

The documentation for this class was generated from the following file: