Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:49:30

0001 /*
0002  */
0003 
0004 #include "DataFormats/Common/interface/Trie.h"
0005 #include "FWCore/Utilities/interface/Exception.h"
0006 
0007 #include <iostream>
0008 #include <string>
0009 
0010 struct Print {
0011   //  typedef edm::TrieNode<int> const node;
0012   //void operator()(node& n, std::string const& label) const {
0013   //  std::cout << label << " " << n.value() << std::endl;
0014   // }
0015   void operator()(int v, std::string const& label) const { std::cout << label << " " << v << std::endl; }
0016 };
0017 
0018 int main(int, char**) try {
0019   /// trie that associates a integer to strings
0020   /// 0 is the default value I want to receive when there is no match
0021   /// in trie
0022   edm::Trie<int> trie(0);
0023   typedef edm::TrieNode<int> Node;
0024   typedef Node const* pointer;  // sigh....
0025   typedef edm::TrieNodeIter<int> node_iterator;
0026 
0027   char tre[] = {'a', 'a', 'a'};
0028   char quattro[] = {'c', 'a', 'a', 'a'};
0029 
0030   for (int j = 0; j < 3; j++) {
0031     tre[2] = 'a';
0032     quattro[3] = 'a';
0033     for (int i = 0; i < 10; i++) {
0034       trie.insert(tre, 3, i);
0035       trie.insert(quattro, 4, i);
0036       tre[2]++;
0037       quattro[3]++;
0038     }
0039     tre[1]++;
0040     quattro[2]++;
0041   }
0042 
0043   std::cout << "get [aac] " << trie.find("aac", 3) << std::endl;
0044   std::cout << "get [caae] = " << trie.find("caae", 4) << std::endl;
0045 
0046   trie.setEntry("caag", 4, -2);
0047   std::cout << "get [caag] = " << trie.find("caag", 4) << std::endl;
0048 
0049   // no match
0050   std::cout << "get [abcd] = " << trie.find("abcd", 4) << std::endl;
0051   // no match
0052   std::cout << "get [ca] = " << trie.find("ca", 2) << std::endl;
0053 
0054   trie.display(std::cout);
0055   std::cout << std::endl;
0056 
0057   pointer pn = trie.node("ab", 2);
0058   if (pn)
0059     pn->display(std::cout, 0, ' ');
0060   std::cout << std::endl;
0061 
0062   node_iterator e;
0063   std::cout << "\n ab iteration" << std::endl;
0064   for (node_iterator p(trie.node("ab", 2)); p != e; p++) {
0065     std::cout << "ab" << p.label() << " = " << p->value() << std::endl;
0066   }
0067 
0068   std::cout << "\n ab iteration: string" << std::endl;
0069   pn = trie.node("ab");
0070   e = pn->end();
0071   for (Node::const_iterator p = pn->begin(); p != e; p++) {
0072     std::cout << "ab" << p.label() << " = " << p->value() << std::endl;
0073   }
0074   std::cout << "\ntop iteration" << std::endl;
0075   for (node_iterator p(trie.initialNode()); p != e; p++) {
0076     std::cout << p.label() << " = " << p->value() << std::endl;
0077   }
0078   std::cout << std::endl;
0079 
0080   std::cout << "\nfull walk" << std::endl;
0081   Print pr;
0082   edm::walkTrie(pr, *trie.initialNode());
0083   std::cout << std::endl;
0084 
0085   std::cout << "\nleaves iteration" << std::endl;
0086   edm::iterateTrieLeaves(pr, *trie.initialNode());
0087   std::cout << std::endl;
0088   return 0;
0089 } catch (cms::Exception const& e) {
0090   std::cerr << e.explainSelf() << std::endl;
0091   return 1;
0092 } catch (std::exception const& e) {
0093   std::cerr << e.what() << std::endl;
0094   return 1;
0095 }