akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 1 | #include <iostream> |
2 | #include <algorithm> | ||||
3 | |||||
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 4 | #include <ndn-cxx/common.hpp> |
5 | |||||
6 | #include "name-prefix-list.hpp" | ||||
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame^] | 7 | #include "logger.hpp" |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 8 | |
9 | namespace nlsr { | ||||
10 | |||||
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame^] | 11 | INIT_LOGGER("NamePrefixList"); |
12 | |||||
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 13 | using namespace std; |
14 | |||||
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 15 | NamePrefixList::NamePrefixList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 16 | { |
17 | } | ||||
18 | |||||
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 19 | NamePrefixList::~NamePrefixList() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 20 | { |
21 | } | ||||
22 | |||||
23 | static bool | ||||
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 24 | nameCompare(const ndn::Name& name1, const ndn::Name& name2) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 25 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 26 | return name1 == name2; |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 27 | } |
28 | |||||
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 29 | int32_t |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 30 | NamePrefixList::insert(const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 31 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 32 | std::list<ndn::Name>::iterator it = std::find_if(m_nameList.begin(), |
33 | m_nameList.end(), | ||||
34 | ndn::bind(&nameCompare, _1 , | ||||
35 | ndn::cref(name))); | ||||
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 36 | if (it != m_nameList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 37 | return -1; |
38 | } | ||||
39 | m_nameList.push_back(name); | ||||
40 | return 0; | ||||
41 | } | ||||
42 | |||||
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 43 | int32_t |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 44 | NamePrefixList::remove(const ndn::Name& name) |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 45 | { |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 46 | std::list<ndn::Name>::iterator it = std::find_if(m_nameList.begin(), |
47 | m_nameList.end(), | ||||
48 | ndn::bind(&nameCompare, _1 , | ||||
49 | ndn::cref(name))); | ||||
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 50 | if (it != m_nameList.end()) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 51 | m_nameList.erase(it); |
52 | } | ||||
53 | return -1; | ||||
54 | } | ||||
55 | |||||
56 | void | ||||
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 57 | NamePrefixList::sort() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 58 | { |
59 | m_nameList.sort(); | ||||
60 | } | ||||
61 | |||||
62 | void | ||||
akmhoque | 674b0b1 | 2014-05-20 14:33:28 -0500 | [diff] [blame^] | 63 | NamePrefixList::writeLog() |
64 | { | ||||
65 | _LOG_DEBUG("-------Name Prefix List--------"); | ||||
66 | int i = 1; | ||||
67 | for (std::list<ndn::Name>::iterator it = m_nameList.begin(); | ||||
68 | it != m_nameList.end(); it++) { | ||||
69 | _LOG_DEBUG("Name " << i << " : " << (*it)); | ||||
70 | i++; | ||||
71 | } | ||||
72 | } | ||||
73 | |||||
74 | void | ||||
akmhoque | c8a10f7 | 2014-04-25 18:42:55 -0500 | [diff] [blame] | 75 | NamePrefixList::print() |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 76 | { |
77 | int i = 1; | ||||
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 78 | for (std::list<ndn::Name>::iterator it = m_nameList.begin(); |
akmhoque | 157b0a4 | 2014-05-13 00:26:37 -0500 | [diff] [blame] | 79 | it != m_nameList.end(); it++) { |
akmhoque | 5335346 | 2014-04-22 08:43:45 -0500 | [diff] [blame] | 80 | cout << "Name " << i << " : " << (*it) << endl; |
81 | i++; | ||||
82 | } | ||||
83 | } | ||||
84 | |||||
85 | }//namespace nlsr |