blob: 22254462c1649740a6cbfc6cb4bc03e41425340a [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque157b0a42014-05-13 00:26:37 -050023#ifndef NLSR_FACE_MAP_HPP
24#define NLSR_FACE_MAP_HPP
25
26namespace nlsr {
27
28class FaceMapEntry {
29
30public:
31 FaceMapEntry(const std::string& faceUri, uint32_t faceId)
32 : m_faceUri(faceUri)
33 , m_faceId(faceId)
34 {
35 }
36
37 void
38 setFaceUri(const std::string& faceUri)
39 {
40 m_faceUri = faceUri;
41 }
42
43 const std::string&
44 getFaceUri() const
45 {
46 return m_faceUri;
47 }
48
49 void
50 setFaceId(uint32_t faceId)
51 {
52 m_faceId = faceId;
53 }
54
55 uint32_t
56 getFaceId() const
57 {
58 return m_faceId;
59 }
60
61 bool
62 compare(const FaceMapEntry& fme)
63 {
64 return m_faceUri == fme.getFaceUri();
65 }
66
67private:
68 std::string m_faceUri;
69 uint32_t m_faceId;
70};
71
72inline std::ostream&
73operator<<(std::ostream& os, const FaceMapEntry& fme)
74{
75 os << "Face Map Entry (FaceUri: " << fme.getFaceUri() << " Face Id: ";
76 os << fme.getFaceId() << ")" << std::endl;
77 return os;
78}
79
80class FaceMap {
81
82public:
83 FaceMap()
84 {
85 }
86
87 ~FaceMap()
88 {
89 }
90
91 inline void
92 update(const std::string& faceUri, uint32_t faceId)
93 {
94 FaceMapEntry fme(faceUri, faceId);
95 std::list<FaceMapEntry>::iterator it = std::find_if(m_table.begin(),
96 m_table.end(),
97 bind(&FaceMapEntry::compare,
98 &fme, _1));
99 if (it == m_table.end()) {
100 m_table.push_back(fme);
101 }
102 else {
103 (*it).setFaceId(fme.getFaceId());
104 }
105 }
106
107 inline uint32_t
108 getFaceId(const std::string& faceUri)
109 {
110 FaceMapEntry fme(faceUri, 0);
111 std::list<FaceMapEntry>::iterator it = std::find_if(m_table.begin(),
112 m_table.end(),
113 bind(&FaceMapEntry::compare,
114 &fme, _1));
115 if (it != m_table.end()) {
116 return (*it).getFaceId();
117 }
118 return 0;
119 }
120
121 inline void
122 print()
123 {
124 std::cout << "------- Face Map-----------" << std::endl;
125 for(std::list<FaceMapEntry>::iterator it = m_table.begin();
126 it != m_table.end(); ++it) {
127 std::cout << (*it);
128 }
129 }
130
131private:
132 std::list<FaceMapEntry> m_table;
133};
134
135} //namespace nlsr
136
137#endif //NLSR_FACE_MAP_HPP