Browse Source

Adding 'find(...) const -> const_iterator' functions. This adds a const_iml_iterator typedef, and alters find_impl into a pseudo-static function.
TODO: see if I can extract this from the class entirely

Samuel Jaffe 8 years ago
parent
commit
1d27d8fb4e
2 changed files with 16 additions and 10 deletions
  1. 13 7
      trie.hpp
  2. 3 3
      trie_impl.hpp

+ 13 - 7
trie.hpp

@@ -52,6 +52,7 @@ public:
   using const_reverse_iterator = trie_reverse_iterator<self_t const,  local_const_reverse_iterator>;
 private:
   using impl_iterator = detail::trie_iterator_base<self_t, local_iterator>;
+  using impl_const_iterator = detail::trie_iterator_base<self_t, local_const_iterator>;
 public:
   trie() {}
   trie(trie const & other);
@@ -129,9 +130,13 @@ public:
   
   
   template <typename KS>
-  iterator find(KS const & keys) { return find_impl(keys); }
-  iterator find(key_type const & key) { return find_impl({key}); }
-  iterator find(std::initializer_list<key_type> keys) { return find_impl(keys); }
+  iterator find(KS const & keys) { return find_impl<impl_iterator>(this, keys); }
+  iterator find(key_type const & key) { return find_impl<impl_iterator>(this, {key}); }
+  iterator find(std::initializer_list<key_type> keys) { return find_impl<impl_iterator>(this, keys); }
+  template <typename KS>
+  const_iterator find(KS const & keys) const { return find_impl<impl_const_iterator>(this, keys); }
+  const_iterator find(key_type const & key) const { return find_impl<impl_const_iterator>(this, {key}); }
+  const_iterator find(std::initializer_list<key_type> keys) const { return find_impl<impl_const_iterator>(this, keys); }
   
   template <typename KS>
   void erase(KS const & keys) { drop(find(keys)); }
@@ -141,10 +146,11 @@ public:
   void clear();
 private:
   void drop(iterator it);
-  template <typename KS>
-  impl_iterator find_impl(KS const & keys);
-  impl_iterator find_impl(std::initializer_list<key_type> keys) {
-    return find_impl<std::initializer_list<key_type>>(keys);
+  template <typename Iter, typename Trie, typename KS>
+  static Iter find_impl(Trie * tr, KS const & keys);
+  template <typename Iter, typename Trie>
+  static Iter find_impl(Trie * tr, std::initializer_list<key_type> keys) {
+    return find_impl<Iter, Trie, std::initializer_list<key_type>>(tr, keys);
   }
   template <typename KS, typename... Args>
   std::pair<impl_iterator, bool> emplace_impl(KS && keys, Args &&... args);

+ 3 - 3
trie_impl.hpp

@@ -75,9 +75,9 @@ auto trie<K, V, C>::emplace_impl(KS && keys, Args &&... args) -> std::pair<impl_
 }
 
 template <typename K, typename V, typename C>
-template <typename KS>
-auto trie<K, V, C>::find_impl(KS const & keys) -> impl_iterator {
-  impl_iterator rval{this};
+template <typename Iter, typename Trie, typename KS>
+auto trie<K, V, C>::find_impl(Trie * tr, KS const & keys) -> Iter {
+  Iter rval{tr};
   for (auto & key : keys) {
     auto & top = rval.stk.top()->impl_;
     auto it = top.find(key), end = top.end();