Separating interface and implementation of FIB. Right now everything is
kind of broken.
diff --git a/utils/detail/multi-policy-container.h b/utils/detail/multi-policy-container.h
index 9fcc061..0fa07b3 100644
--- a/utils/detail/multi-policy-container.h
+++ b/utils/detail/multi-policy-container.h
@@ -77,6 +77,13 @@
Value::value_.erase (item);
Super::erase (item);
}
+
+ void
+ clear ()
+ {
+ Value::value_.clear ();
+ Super::clear ();
+ }
};
template< class Base >
@@ -88,6 +95,7 @@
bool insert (typename Base::iterator item) { return true; }
void lookup (typename Base::iterator item) {}
void erase (typename Base::iterator item) {}
+ void clear () {}
};
template< class Base, class Vector >
diff --git a/utils/empty-policy.h b/utils/empty-policy.h
index 0fe43f8..c5f324c 100644
--- a/utils/empty-policy.h
+++ b/utils/empty-policy.h
@@ -26,21 +26,24 @@
struct empty_policy_traits
{
- typedef void policy_hook_type;
+ typedef void* policy_hook_type;
- template<class Container> struct container_hook { typedef void type; }
+ template<class Container> struct container_hook { typedef void* type; };
template<class Base,
class Container,
class Hook>
struct policy
{
- class type
+ struct type
{
- inline void update (typename parent_trie::iterator) { }
- inline bool insert (typename parent_trie::iterator) { return true; }
- inline void lookup (typename parent_trie::iterator item) { }
- inline void erase (typename parent_trie::iterator item) { }
+ inline type (Base &base) {}
+
+ inline void update (typename Container::iterator) { }
+ inline bool insert (typename Container::iterator) { return true; }
+ inline void lookup (typename Container::iterator item) { }
+ inline void erase (typename Container::iterator item) { }
+ inline void clear () { }
};
};
};
diff --git a/utils/fifo-policy.h b/utils/fifo-policy.h
index 9f215fc..2dccbe4 100644
--- a/utils/fifo-policy.h
+++ b/utils/fifo-policy.h
@@ -90,6 +90,12 @@
}
inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ inline void
set_max_size (size_t max_size)
{
max_size_ = max_size;
diff --git a/utils/lru-policy.h b/utils/lru-policy.h
index bf03fa8..077d602 100644
--- a/utils/lru-policy.h
+++ b/utils/lru-policy.h
@@ -95,6 +95,12 @@
}
inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ inline void
set_max_size (size_t max_size)
{
max_size_ = max_size;
diff --git a/utils/multi-policy.h b/utils/multi-policy.h
index fd126ae..f5cf682 100644
--- a/utils/multi-policy.h
+++ b/utils/multi-policy.h
@@ -115,6 +115,12 @@
{
policy_container::erase (item);
}
+
+ inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
};
};
};
diff --git a/utils/random-policy.h b/utils/random-policy.h
index 705bcf3..7a96712 100644
--- a/utils/random-policy.h
+++ b/utils/random-policy.h
@@ -127,6 +127,12 @@
}
inline void
+ clear ()
+ {
+ policy_container::clear ();
+ }
+
+ inline void
set_max_size (size_t max_size)
{
max_size_ = max_size;
diff --git a/utils/trie-with-policy.h b/utils/trie-with-policy.h
index c674ef2..79480da 100644
--- a/utils/trie-with-policy.h
+++ b/utils/trie-with-policy.h
@@ -53,7 +53,7 @@
}
inline std::pair< iterator, bool >
- insert (const FullKey &key, typename PayloadTraits::const_pointer_type payload)
+ insert (const FullKey &key, typename PayloadTraits::pointer_type payload)
{
std::pair<iterator, bool> item =
trie_.insert (key, payload);
@@ -69,14 +69,26 @@
}
else
{
- item.first->set_payload (payload);
- policy_.update (s_iterator_to (item.first));
+ return std::make_pair (s_iterator_to (item.first), false);
}
return item;
}
inline void
+ erase (const FullKey &key)
+ {
+ iterator foundItem, lastItem;
+ bool reachLast;
+ boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+
+ if (!reachLast || lastItem->payload () == PayloadTraits::empty_payload)
+ return; // nothing to invalidate
+
+ erase (lastItem);
+ }
+
+ inline void
erase (iterator node)
{
if (node == end ()) return;
@@ -85,18 +97,39 @@
node->erase (); // will do cleanup here
}
+ inline void
+ clear ()
+ {
+ policy_.clear ();
+ trie_.clear ();
+ }
+
+ template<typename Modifier>
+ bool
+ modify (iterator position, Modifier mod)
+ {
+ if (position == end ()) return false;
+ if (position->payload () == PayloadTraits::empty_payload) return false;
+
+ mod (*position->payload ());
+ policy_.update (position);
+ return true;
+ }
+
/**
* @brief Find a node that has the longest common prefix with key (FIB/PIT lookup)
*/
inline iterator
longest_prefix_match (const FullKey &key)
{
- boost::tuple< iterator, bool, iterator > item = trie_.find (key);
- if (item.template get<0> () != trie_.end ())
+ iterator foundItem, lastItem;
+ bool reachLast;
+ boost::tie (foundItem, reachLast, lastItem) = trie_.find (key);
+ if (foundItem != trie_.end ())
{
- policy_.lookup (s_iterator_to (item.template get<0> ()));
+ policy_.lookup (s_iterator_to (foundItem));
}
- return item.template get<0> ();
+ return foundItem;
}
/**
@@ -159,7 +192,7 @@
}
}
- iterator end ()
+ iterator end () const
{
return 0;
}
@@ -190,4 +223,3 @@
} // ndnSIM
#endif // TRIE_WITH_POLICY_H_
-
diff --git a/utils/trie.h b/utils/trie.h
index 47e62e5..86ca01f 100644
--- a/utils/trie.h
+++ b/utils/trie.h
@@ -29,6 +29,7 @@
#include <boost/functional/hash.hpp>
#include <boost/interprocess/smart_ptr/unique_ptr.hpp>
#include <boost/tuple/tuple.hpp>
+#include <boost/foreach.hpp>
namespace ndnSIM
{
@@ -43,11 +44,11 @@
typedef Payload* pointer_type;
typedef const Payload* const_pointer_type;
- static const Payload* empty_payload;
+ static Payload* empty_payload;
};
template<typename Payload>
-const Payload*
+Payload*
pointer_payload_traits<Payload>::empty_payload = 0;
template<typename Payload>
@@ -57,11 +58,11 @@
typedef ns3::Ptr<Payload> pointer_type;
typedef ns3::Ptr<const Payload> const_pointer_type;
- static const ns3::Ptr<const Payload> empty_payload;
+ static ns3::Ptr<Payload> empty_payload;
};
template<typename Payload>
-const ns3::Ptr<const Payload>
+ns3::Ptr<Payload>
smart_pointer_payload_traits<Payload>::empty_payload = 0;
@@ -109,6 +110,12 @@
inline
~trie ();
+
+ void
+ clear ()
+ {
+ children_.clear_and_dispose (trie_delete_disposer ());
+ }
// actual entry
friend bool
@@ -120,7 +127,7 @@
inline std::pair<iterator, bool>
insert (const FullKey &key,
- typename PayloadTraits::const_pointer_type payload);
+ typename PayloadTraits::pointer_type payload);
/**
* @brief Removes payload (if it exists) and if there are no children, prunes parents trie
@@ -134,6 +141,12 @@
inline void
prune ();
+ inline boost::tuple<const iterator, bool, const iterator>
+ find (const FullKey &key) const
+ {
+ return const_cast<trie*> (this)->find (key);
+ }
+
/**
* @brief Perform the longest prefix match
* @param key the key for which to perform the longest prefix match
@@ -175,8 +188,14 @@
return payload_;
}
+ typename PayloadTraits::pointer_type
+ payload ()
+ {
+ return payload_;
+ }
+
void
- set_payload (typename PayloadTraits::const_pointer_type payload)
+ set_payload (typename PayloadTraits::pointer_type payload)
{
payload_ = payload;
}
@@ -237,7 +256,7 @@
buckets_array buckets_;
unordered_set children_;
- typename PayloadTraits::const_pointer_type payload_;
+ typename PayloadTraits::pointer_type payload_;
trie *parent_; // to make cleaning effective
};
@@ -267,7 +286,7 @@
inline
std::pair<typename trie<FullKey, Payload, PayloadTraits, PolicyHook>::iterator, bool>
trie<FullKey, Payload, PayloadTraits, PolicyHook>
-::insert (const FullKey &key, typename PayloadTraits::const_pointer_type payload)
+::insert (const FullKey &key, typename PayloadTraits::pointer_type payload)
{
trie *trieNode = this;