1 #ifndef DataFormats_Common_Trie_h
2 #define DataFormats_Common_Trie_h
132 template <
typename Node>
134 unsigned char val)
const;
169 template <
typename T>
171 template <
typename T>
178 template <
typename T>
199 void insert(
const char *str,
unsigned strLen,
const T &value);
203 void setEntry(
const char *str,
unsigned strLen,
const T &value);
206 const T&
find(
const char *str,
unsigned strLen)
const;
213 void display(std::ostream &os);
232 #include <boost/iterator/iterator_facade.hpp>
244 :
public boost::iterator_facade<TrieNodeIter<T>,
246 boost::forward_traversal_tag >
257 :
m_node(p ? p->subNode() : 0),
258 m_label(p ? p->subNodeLabel() : 0)
272 return this->
m_node == other.m_node;
283 template<
typename V,
typename T>
288 for (node_iterator
p(&n);
p!=
e; ++
p) {
289 v((*p).value(),
label+(char)
p.label());
297 template<
typename V,
typename T>
303 if (p==e)
return true;
306 v((*p).value(),
label+(char)p.label());
320 template <
typename T>
322 _paquetSize(paquetSize), _lastNodes(0x0), _nbUsedInLastNodes(0)
327 template <
typename T>
330 typename std::list<TrieNode<T>*>::const_iterator it;
332 for (it = _allocatedNodes.begin(); it != _allocatedNodes.end(); ++it)
338 template <
typename T>
341 if (_nbUsedInLastNodes == _paquetSize)
343 _allocatedNodes.push_back(_lastNodes);
344 _nbUsedInLastNodes = 0;
347 TrieNode<T> *res = &_lastNodes[_nbUsedInLastNodes];
348 ++_nbUsedInLastNodes;
354 template <
typename T>
357 typename std::list<TrieNode<T>*>::const_iterator it;
358 for (it = _allocatedNodes.begin(); it != _allocatedNodes.end(); ++it)
360 _allocatedNodes.clear();
361 _nbUsedInLastNodes = 0;
365 template <
typename T>
367 _brother(0), _brotherLabel(0), _firstSubNode(0), _firstSubNodeLabel(0),_value()
373 template <
typename T>
381 template <
typename T>
386 template <
typename T>
392 template <
typename T>
398 template <
typename T>
404 template <
typename T>
410 template <
typename T>
416 template <
typename T>
420 return _sequentialSearch(brother, _brotherLabel, chr);
423 template <
typename T>
426 return _sequentialSearch(_brother, _brotherLabel, chr);
429 template <
typename T>
432 if (!_brother || _brotherLabel > chr)
442 template <
typename T>
445 return _brotherLabel;
448 template <
typename T>
451 return _firstSubNode;
454 template <
typename T>
457 return _firstSubNode;
460 template <
typename T>
463 return _firstSubNodeLabel;
466 template <
typename T>
470 return _sequentialSearch(first, _firstSubNodeLabel, chr);
473 template <
typename T>
476 return _sequentialSearch(_firstSubNode, _firstSubNodeLabel, chr);
479 template <
typename T>
482 if (!_firstSubNode || _firstSubNodeLabel > chr)
484 node->
_setBrother(_firstSubNode, _firstSubNodeLabel);
485 _firstSubNode =
node;
486 _firstSubNodeLabel = chr;
489 _firstSubNode->_addBrother(chr, node);
492 template <
typename T>
493 template <
typename Node>
496 if (first && label <= val)
500 return first->_getBrother(val);
505 template <
typename T>
509 _brotherLabel = brotherLabel;
512 template <
typename T>
519 os <<
"label[" << label <<
"] ";
520 os <<
"value[" << _value <<
"]" << std::endl;
522 _firstSubNode->display(os, offset + 2, _firstSubNodeLabel);
524 _brother->display(os, offset, _brotherLabel);
527 template <
typename T>
533 _firstSubNodeLabel = 0;
546 namespace detailsTrie {
549 "Trie::insert called with a key already in collection;\n"
556 template <
typename T>
558 _empty(empty), _factory(0x0), _initialNode(0x0)
565 template <
typename T>
571 template <
typename T>
574 setEntry(str.c_str(),str.size(),
value);
576 template <
typename T>
583 template <
typename T>
591 while (found && pos < strLen)
604 if (!node || pos != strLen)
607 for (
unsigned i = pos;
i < strLen; ++
i)
620 template <
typename T>
626 template <
typename T>
632 if (node && node->
value() != _empty)
637 template <
typename T>
639 return find(str.c_str(),str.size());
642 template <
typename T>
649 while (found && pos < strLen)
659 if (node && pos == strLen)
660 return node->
value();
664 template <
typename T>
667 return node(str.c_str(),str.size());
670 template <
typename T>
677 while (found && pos < strLen)
691 template <
typename T>
697 template <
typename T>
701 _initialNode = _factory->newNode(_empty);
704 template <
typename T>
708 _initialNode->display(os, 0, 0);
711 #endif // DataFormats_Common_Trie_h
void clear()
clear content of TrieNode
void display(std::ostream &os, unsigned offset, unsigned char label) const
display content of node in output stream
TrieFactory & operator=(const TrieFactory &e)
avoid affectation operator
edm::TrieNodeIter< PDet > node_iterator
this class represent the node of a trie, it contains a link to a sub node and a link to a brother (no...
void _setBrother(TrieNode< T > *brother, unsigned char brotherLabel)
set brother (used by sort)
void addSubNode(unsigned char chr, TrieNode< T > *node)
const TrieNode< T > * _getBrother(unsigned char chr) const
TrieNode< T > * _lastNodes
void setValue(const T &val)
set value associed to node
const TrieNode< T > * brother() const
get brother (return 0x0 this node has no brother)
const TrieNode< T > * subNode() const
unsigned char _firstSubNodeLabel
character to go to first subnode
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
TrieNode< T > * _firstSubNode
pointer to first sub node
TrieNodeIter< T > end() const
mark end of iteration (std conforming)
TrieNode< T > * newNode(const T &value)
static void throwThis(Code category, char const *message0="", char const *message1="", char const *message2="", char const *message3="", char const *message4="")
bool iterateTrieLeaves(V &v, TrieNode< T > const &n, std::string const &label="")
visits only leaf nodes
void display(std::ostream &os)
display content of trie in output stream
void insert(std::string const &str, const T &value)
TrieNodeIter< T > begin() const
initialize subnode iterator (std conforming)
void setEntry(std::string const &str, const T &value)
TrieNode< T > const node_base
const T & value() const
get value associed to node
Node _sequentialSearch(Node first, unsigned char label, unsigned char val) const
const T & find(std::string const &str) const
get an entry in the Trie
TrieFactory()
avoid default constructor
void _addBrother(unsigned char chr, TrieNode< T > *brother)
add a new brother
const TrieNode< T > * initialNode() const
get initial TrieNode
T _empty
value returned when no match is found in trie
std::list< TrieNode< T > * > _allocatedNodes
bool insert(Storage &iStorage, ItemType *iItem, const IdTag &iIdTag)
unsigned int offset(bool)
friend class boost::iterator_core_access
TrieNodeIter< T > const_iterator
void errorInsert(std::string const &key)
void walkTrie(V &v, TrieNode< T > const &n, std::string const &label="")
visit each node of the trie
unsigned _nbUsedInLastNodes
void clear()
clear the content of trie
bool equal(self const &other) const
TrieNode & operator=(const TrieNode &e)
avoid affectation operator
TrieNodeIter(node_base *p)
TrieFactory< T > * _factory
factory
Trie & operator=(const Trie &e)
avoid affectation operator
Trie()
avoid default constructor
TrieNode< T > * _initialNode
first node of trie
unsigned char label() const
const TrieNode< T > * subNodeByLabel(unsigned char chr) const
T _value
value associed to this node
unsigned char brotherLabel() const
get brother label
edm::TrieNode< PDet > Node
T first(std::pair< T, U > const &p)
unsigned char subNodeLabel() const
unsigned char _brotherLabel
character to go to brother (node with same father as this one)
const TrieNode< T > * node(std::string const &str) const
get node matching a string
TrieNode< T > * _addEntry(const char *str, unsigned strLen)
node_base & dereference() const
TrieNode< T > * _brother
pointer to brother (node with same father as this one)