Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "ns3/core-module.h" |
| 22 | #include "ns3/ndnSIM-module.h" |
| 23 | #include "../utils/trie.h" |
| 24 | |
| 25 | using namespace ns3; |
| 26 | |
| 27 | NS_LOG_COMPONENT_DEFINE ("Trie"); |
| 28 | |
| 29 | class Integer : public ns3::SimpleRefCount<Integer> |
| 30 | { |
| 31 | public: |
| 32 | Integer (int value) : value_ (value) {} |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 33 | |
| 34 | operator int () const { return value_; } |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 35 | private: |
| 36 | int value_; |
| 37 | }; |
| 38 | |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 39 | std::ostream & |
| 40 | operator << (std::ostream &os, const Integer &i) |
| 41 | { |
| 42 | os << (int)i; |
| 43 | return os; |
| 44 | } |
| 45 | |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 46 | int |
| 47 | main (int argc, char *argv[]) |
| 48 | { |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 49 | typedef indexed_trie<ns3::CcnxNameComponents, Integer, smart_pointer_payload_traits<Integer> > trie; |
| 50 | trie x; |
| 51 | x.getPolicy ().set_max_size (2); |
| 52 | |
| 53 | // x.getTrie ().PrintStat (std::cout); |
| 54 | |
| 55 | ns3::CcnxNameComponents n1,n2,n3,n4; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 56 | n1("a")("b")("c"); |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 57 | n2("a")("b")("d"); |
| 58 | n3("a")("b")("f"); |
| 59 | n4("a")("b"); |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 60 | |
| 61 | ns3::Ptr<Integer> i = ns3::Create<Integer> (1); |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 62 | x.insert (n4, ns3::Create<Integer> (4)); |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 63 | |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 64 | |
| 65 | |
| 66 | x.insert (n3, ns3::Create<Integer> (3)); |
| 67 | |
| 68 | std::pair< trie::iterator, bool > item = |
| 69 | x.insert (n2, ns3::Create<Integer> (2)); |
| 70 | x.erase (item.first); |
| 71 | |
| 72 | x.insert (n1, ns3::Create<Integer> (1)); |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 73 | x.insert (n4, ns3::Create<Integer> (4)); |
| 74 | |
| 75 | // std::cout << x.getTrie (); |
| 76 | BOOST_FOREACH (const trie::parent_trie &item, x.getPolicy ()) |
| 77 | { |
| 78 | std::cout << *item.payload () << " " << std::endl; |
| 79 | } |
| 80 | |
| 81 | // ns3::CcnxNameComponents n4; |
| 82 | // n4("a")("c"); |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 83 | |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 84 | // // std::cout << *x->find (n4).get<0> (); |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 85 | |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 86 | // x->prune (); |
| 87 | // // x->find (n5).get<0> ()->erase (); |
| 88 | // x->find (n1).get<0> ()->erase (); |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 89 | |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 90 | // std::cout << "digraph trie {\n"; |
| 91 | // std::cout << *x; |
| 92 | // std::cout << "}\n"; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 89fb535 | 2012-06-12 22:43:16 -0700 | [diff] [blame] | 94 | // x->PrintStat (std::cout); |
| 95 | |
| 96 | // delete x; |
Alexander Afanasyev | fd0c41c | 2012-06-11 22:15:49 -0700 | [diff] [blame] | 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |