MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | List of all members
Mezzanine::Trie< T, V, Cmp, Items > Class Template Reference

Trie main class. More...

#include <trie.h>

Public Types

typedef Node< T, V, Cmp, Items >
::const_iterator 
const_iterator
 
typedef Node< T, V, Cmp, Items >
::iterator 
iterator
 

Public Member Functions

 Trie (const T &endSymbol)
 
iterator begin ()
 
const_iterator begin () const
 
void clear ()
 
bool empty () const
 
iterator end ()
 
const_iterator end () const
 
endSymbol () const
 
bool erase (const T *key)
 
bool erase (const std::vector< T > &key)
 
bool erase (iterator pos)
 
iterator find (const T *key)
 
iterator find (const std::vector< T > &key)
 
const_iterator find (const T *key) const
 
const_iterator find (const std::vector< T > &key) const
 
const V * get (const T *key) const
 
const V * get (const std::vector< T > &key) const
 
V * get (const T *key)
 
V * get (const std::vector< T > &key)
 
bool hasKey (const T *key) const
 
bool hasKey (const std::vector< T > &key) const
 
std::pair< iterator, bool > insert (const T *key, V const &value)
 
std::pair< iterator, bool > insert (const std::vector< T > &key, V const &value)
 
V & operator[] (const T *key)
 
V & operator[] (const std::vector< T > &key)
 
unsigned int size () const
 
iterator startsWith (const T *prefix)
 
iterator startsWith (const std::vector< T > &prefix)
 
const_iterator startsWith (const T *prefix) const
 
const_iterator startsWith (const std::vector< T > &prefix) const
 

Detailed Description

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
class Mezzanine::Trie< T, V, Cmp, Items >

Trie main class.

Template Parameters
TType for each element in the key
VType of the value that the key will be representing
CmpComparison functor
ItemsThe data structure that represents each node in the Trie. Items can be Mezzanine::SetItems<T, V, Cmp> or Mezzanine::VectorItems<T, V, Cmp, Max>, Max is the integer representing number of elements in each Trie node.

Usage of the Trie

Declarating the Trie

A Trie with key as chars and value as std::string can be declared as given below

#include <string>
#include <trie.h>
int main(int argc, char ** argv) {
return 0;
}

Populatiion and deletion from the Trie

Trie can be populated by using the Trie::insert method and element can be removed using Trie::erase.

#include <trie.h>
#include <string>
#include <iostream>
int main(int argc, char ** argv) {
// adding key value pair to the Trie
if (dictionary.insert("karma$", "Destiny or fate, following as effect from cause").second) {
std::cout << "added karma" << std::endl;
}
// removing key from the Trie
if (dictionary.erase("karma$")) {
std::cout << "removed karma" << std::endl;
}
return 0;
}

Retrieval of Value

Value for a key can be retrieved using Trie::get method and the existence of the key can be tested using Trie::hasKey method.

#include <trie.h>
#include <string>
#include <iostream>
int main(int argc, char ** argv) {
dictionary.insert("karma$", "Destiny or fate, following as effect from cause");
if (dictionary.hasKey("karma$")) {
std::cout << "key karma found" << std::endl;
}
std::string * result = dictionary.get("karma$");
if (result) {
std::cout << result->c_str() << std::endl;
}
return 0;
}

Searching keys which have common prefix

Keys which begins with a specific charactars can be retrieved using Trie::startsWith method

#include <trie.h>
#include <string>
#include <iostream>
#include <vector>
int main(int argc, char ** argv) {
dictionary.insert("karma", "Destiny or fate, following as effect from cause");
Mezzanine::Trie<char, std::string>::iterator iter = dictionary.startsWith("kar");
for (; iter != dictionary.end(); ++iter) {
std::cout << iter->first << " : " << iter->second->c_str() << std::endl;
}
return 0;
}

Trie with each Node as an array

Here each node of the Trie is an array. The advantage is that the searching of a symbol in the array takes O(1) time (is constant time). The disadvantage is that the array will have empty elements so the space used will more than actually required.

#include <trie.h>
#include <string>
#include <iostream>
#include <vector>
int main(int argc, char ** argv) {
// Here 256 is the size of array in each node
return 0;
}

Efficient use of Trie for alphabets

Below example shows how to use Trie to operate on alphabets efficiently. Here VectorItems is used to store alphabets with size of 28 (26 alphabets + space + end symbol).

#include <trie.h>
#include <string>
#include <iostream>
#include <vector>
#include <cctype>
class TrieCaseInsensitiveCompare
{
public:
bool operator()(char v1, char v2) {
int i1 = std::tolower(v1);
int i2 = std::tolower(v2);
return i1 < i2;
}
};
// key to vector index converter
// case insensitive and includes alphabets, space and end symbol
class AlphaToIndex
{
public:
unsigned int operator()(const char & c) const {
unsigned int index = 0;
if (c == ' ') {
index = 27;
} else if (c >= 'A' && c <= 'Z') {
index = c - 'A' + 1;
} else if (c >= 'a' && c <= 'z') {
index = c - 'a' + 1;
}
return index;
}
};
int main(int argc, char ** argv) {
Mezzanine::Trie<char, std::string,
TrieCaseInsensitiveCompare,
> dictionary('\0');
// adding key value pair to the Trie
if (dictionary.insert("karma", "Destiny or fate, following as effect from cause").second) {
std::cout << "added karma" << std::endl;
}
// removing key from the Trie
if (dictionary.erase("karma")) {
std::cout << "removed karma" << std::endl;
}
return 0;
}

Trie with each Node as a set

Here each node will be an ordered set. Here there will be no extra usage of space but search for a symbol in the node takes logarithmic time. Trie with this feature can also be used for caseinsensitive symbol handling.

#include <trie.h>
#include <string>
#include <iostream>
#include <set>
int main(int argc, char ** argv) {
return 0;
}

Using Trie::iterator

Trie iterator can be used the same way as STL iterator. Key and value can be accessed from iterator using first and secod member. first is vector of key characters and second is a pointer to the value.

#include <trie.h>
#include <string>
#include <iostream>
#include <vector>
int main(int argc, char ** argv) {
dictionary.insert("karma$", "Destiny or fate, following as effect from cause");
for (; iter != iend; ++iter) {
std::cout << iter->first << " " << iter->second->c_str() << std::endl;
}
return 0;
}

Definition at line 1287 of file trie.h.

Constructor & Destructor Documentation

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
Mezzanine::Trie< T, V, Cmp, Items >::Trie ( const T &  endSymbol)
inline
Parameters
endSymbolThe symbol which marks the end of key input

Definition at line 1297 of file trie.h.

Member Function Documentation

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
iterator Mezzanine::Trie< T, V, Cmp, Items >::begin ( )
inline

Returns an iterator referring to the first element in the Trie

Returns
An iterator to the first element in the Trie

Definition at line 1502 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
const_iterator Mezzanine::Trie< T, V, Cmp, Items >::begin ( ) const
inline

Returns an constant iterator referring to the first element in the Trie

Returns
An constant iterator to the first element in the Trie

Definition at line 1555 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
void Mezzanine::Trie< T, V, Cmp, Items >::clear ( )
inline

All the elements in the Trie are dropped, leaving the Trie with a size of 0.

Definition at line 1448 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
bool Mezzanine::Trie< T, V, Cmp, Items >::empty ( ) const
inline

Test whether Trie is empty

Returns
true if the Trie size is 0, false otherwise

Definition at line 1433 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
iterator Mezzanine::Trie< T, V, Cmp, Items >::end ( )
inline

Returns an iterator referring to the past-the-end element in the Trie

Returns
An iterator to the element past the end of the Trie

Definition at line 1510 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
const_iterator Mezzanine::Trie< T, V, Cmp, Items >::end ( ) const
inline

Returns an constant iterator referring to the past-the-end element in the Trie

Returns
An constant iterator to the element past the end of the Trie

Definition at line 1563 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
T Mezzanine::Trie< T, V, Cmp, Items >::endSymbol ( ) const
inline

Retrieves the end symbol

Returns
end symbol

Definition at line 1494 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
bool Mezzanine::Trie< T, V, Cmp, Items >::erase ( const T *  key)
inline

Remove the entry with the given key from the Trie

Parameters
keyKey which should be erased, should be terminated by 'end' symbol
Returns
true if the given key is erased from the Trie, false otherwise

Definition at line 1326 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
bool Mezzanine::Trie< T, V, Cmp, Items >::erase ( const std::vector< T > &  key)
inline

Remove the entry with the given key from the Trie

Parameters
keyKey which should be erased, should be terminated by 'end' symbol
Returns
true if the given key is erased from the Trie, false otherwise
Todo:
Currently this uses a hack that isn't safe for all types and circumstances. This should be changed to use "data()" function member new to C++11 when we make that dive.

Definition at line 1335 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
bool Mezzanine::Trie< T, V, Cmp, Items >::erase ( iterator  pos)
inline

Remove the entry with the given key from the Trie

Parameters
positerator pointing to a single element to be removed from the Trie
Returns
true if the given key is erased form the Trie, false otherwise

Definition at line 1345 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
iterator Mezzanine::Trie< T, V, Cmp, Items >::find ( const T *  key)
inline

Searches the Trie for an element with 'key' as key

Parameters
keyKey to be searched for, should be terminated by 'end' symbol
Returns
Iterator to the element with key 'key' if found, otherwise an iterator to Trie::end

Definition at line 1519 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
iterator Mezzanine::Trie< T, V, Cmp, Items >::find ( const std::vector< T > &  key)
inline

Searches the Trie for an element with 'key' as key

Parameters
keyKey to be searched for, should be terminated by 'end' symbol
Returns
Iterator to the element with key 'key' if found, otherwise an iterator to Trie::end
Todo:
Currently this uses a hack that isn't safe for all types and circumstances. This should be changed to use "data()" function member new to C++11 when we make that dive.

Definition at line 1528 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
const_iterator Mezzanine::Trie< T, V, Cmp, Items >::find ( const T *  key) const
inline

Searches the Trie for an element with 'key' as key

Parameters
keyKey to be searched for, should be terminated by 'end' symbol
Returns
const_iterator to the element with key 'key' if found, otherwise an const_iterator to Trie::end

Definition at line 1538 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
const_iterator Mezzanine::Trie< T, V, Cmp, Items >::find ( const std::vector< T > &  key) const
inline

Searches the Trie for an element with 'key' as key

Parameters
keyKey to be searched for, should be terminated by 'end' symbol
Returns
const_iterator to the element with key 'key' if found, otherwise an const_iterator to Trie::end

Definition at line 1547 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
const V* Mezzanine::Trie< T, V, Cmp, Items >::get ( const T *  key) const
inline

Retrieves the value for the given key

Parameters
keyKey to be searched for, should be terminated by 'end' symbol
Returns
Constant pointer to value for the given key, 0 on failure

Definition at line 1354 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
const V* Mezzanine::Trie< T, V, Cmp, Items >::get ( const std::vector< T > &  key) const
inline

Retrieves the value for the given key

Parameters
keyKey to be searched for, should be terminated by 'end' symbol
Returns
Constant pointer to value for the given key, 0 on failure
Todo:
Currently this uses a hack that isn't safe for all types and circumstances. This should be changed to use "data()" function member new to C++11 when we make that dive.

Definition at line 1363 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
V* Mezzanine::Trie< T, V, Cmp, Items >::get ( const T *  key)
inline

Retrieves the value for the given key

Parameters
keyKey to be searched for, should be terminated by 'end' symbol
Returns
Pointer to value for the given key, 0 on failure

Definition at line 1373 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
V* Mezzanine::Trie< T, V, Cmp, Items >::get ( const std::vector< T > &  key)
inline

Retrieves the value for the given key

Parameters
keyKey to be searched for, should be terminated by 'end' symbol
Returns
Pointer to value for the given key, 0 on failure
Todo:
Currently this uses a hack that isn't safe for all types and circumstances. This should be changed to use "data()" function member new to C++11 when we make that dive.

Definition at line 1382 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
bool Mezzanine::Trie< T, V, Cmp, Items >::hasKey ( const T *  key) const
inline

Checks whether the given key is present in the Trie

Parameters
keyKey to be searched for, should be terminated by 'end' symol
Returns
true if the key is present

Definition at line 1415 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
bool Mezzanine::Trie< T, V, Cmp, Items >::hasKey ( const std::vector< T > &  key) const
inline

Checks whether the given key is present in the Trie

Parameters
keyKey to be searched for, should be terminated by 'end' symol
Returns
true if the key is present
Todo:
Currently this uses a hack that isn't safe for all types and circumstances. This should be changed to use "data()" function member new to C++11 when we make that dive.

Definition at line 1424 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
std::pair<iterator, bool> Mezzanine::Trie< T, V, Cmp, Items >::insert ( const T *  key,
V const &  value 
)
inline

Add a key with value in to the Trie

Parameters
keyKey which should be inserted, should be terminated by 'end' symbol
valueThe value that is to be set with the key
Returns
An std::pair with pair::first set to the iterator points to the element and pair::second to true is key is newly inserted, false otherwise

Definition at line 1306 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
std::pair<iterator, bool> Mezzanine::Trie< T, V, Cmp, Items >::insert ( const std::vector< T > &  key,
V const &  value 
)
inline

Add a key with value in to the Trie

Parameters
keyKey which should be inserted, should be terminated by 'end' symbol
valueThe value that is to be set with the key
Returns
An std::pair with pair::first set to the iterator points to the element and pair::second to true is key is newly inserted, false otherwise
Todo:
Currently this uses a hack that isn't safe for all types and circumstances. This should be changed to use "data()" function member new to C++11 when we make that dive.

Definition at line 1316 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
V& Mezzanine::Trie< T, V, Cmp, Items >::operator[] ( const T *  key)
inline

Retrieves the value for the given key, If key does not match the key of any element in the Trie, the function inserts a new element with that key and returns a reference to its mapped value

Parameters
keyKey to be searched for, should be terminated by 'end' symbol
Returns
Reference to value for the given key

Definition at line 1394 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
V& Mezzanine::Trie< T, V, Cmp, Items >::operator[] ( const std::vector< T > &  key)
inline

Retrieves the value for the given key, If key does not match the key of any element in the Trie, the function inserts a new element with that key and returns a reference to its mapped value

Parameters
keyKey to be searched for, should be terminated by 'end' symbol
Returns
Reference to value for the given key
Todo:
Currently this uses a hack that isn't safe for all types and circumstances. This should be changed to use "data()" function member new to C++11 when we make that dive.

Definition at line 1405 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
unsigned int Mezzanine::Trie< T, V, Cmp, Items >::size ( ) const
inline

Returns the number of elements in the Trie

Returns
Number of key value pair in the Trie

Definition at line 1441 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
iterator Mezzanine::Trie< T, V, Cmp, Items >::startsWith ( const T *  prefix)
inline

Retrieves iterator to the elements with common prefix

Parameters
prefixPart of the key which should be searched, should be terminated by 'end' symbol
Returns
Iterator to the elements with prefix specified in 'prefix'

Definition at line 1457 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
iterator Mezzanine::Trie< T, V, Cmp, Items >::startsWith ( const std::vector< T > &  prefix)
inline

Retrieves iterator to the elements with common prefix

Parameters
prefixPart of the key which should be searched, should be terminated by 'end' symbol
Returns
Iterator to the elements with prefix specified in 'prefix'
Todo:
Currently this uses a hack that isn't safe for all types and circumstances. This should be changed to use "data()" function member new to C++11 when we make that dive.

Definition at line 1466 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
const_iterator Mezzanine::Trie< T, V, Cmp, Items >::startsWith ( const T *  prefix) const
inline

Retrieves const_iterator to the elements with common prefix

Parameters
prefixPart of the key which should be searched, should be terminated by 'end' symbol
Returns
const_iterator to the elements with prefix specified in 'prefix'

Definition at line 1476 of file trie.h.

template<typename T, typename V, typename Cmp = std::less<T>, typename Items = SetItems<T, V, Cmp>>
const_iterator Mezzanine::Trie< T, V, Cmp, Items >::startsWith ( const std::vector< T > &  prefix) const
inline

Retrieves const_iterator to the elements with common prefix

Parameters
prefixPart of the key which should be searched, should be terminated by 'end' symbol
Returns
const_iterator to the elements with prefix specified in 'prefix'
Todo:
Currently this uses a hack that isn't safe for all types and circumstances. This should be changed to use "data()" function member new to C++11 when we make that dive.

Definition at line 1485 of file trie.h.


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