瀏覽代碼

Extracting find_impl into a friend function.

Samuel Jaffe 8 年之前
父節點
當前提交
fed6a55440
共有 3 個文件被更改,包括 22 次插入17 次删除
  1. 6 4
      trie.hpp
  2. 14 13
      trie_impl.hpp
  3. 2 0
      trie_iterator.hpp

+ 6 - 4
trie.hpp

@@ -18,6 +18,8 @@ class trie;
 namespace detail {
   template <typename Trie, typename Iter>
   class trie_iterator_base;
+  template <typename Iter, typename Trie, typename KS>
+  Iter find_impl(Trie * tr, KS const & keys);
 }
 
 template <typename Trie, typename Iter>
@@ -130,11 +132,11 @@ public:
   
   
   template <typename KS>
-  iterator find(KS const & keys) { return find_impl<impl_iterator>(this, keys); }
+  iterator find(KS const & keys) { return detail::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(KS const & keys) const { return detail::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); }
   
@@ -147,10 +149,10 @@ public:
 private:
   void drop(iterator it);
   template <typename Iter, typename Trie, typename KS>
-  static Iter find_impl(Trie * tr, KS const & keys);
+  friend Iter detail::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);
+    return detail::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);

+ 14 - 13
trie_impl.hpp

@@ -74,19 +74,6 @@ auto trie<K, V, C>::emplace_impl(KS && keys, Args &&... args) -> std::pair<impl_
   return { std::move(it) , create };
 }
 
-template <typename K, typename V, typename C>
-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();
-    if ( it == end ) { return {}; }
-    rval.push({it, end});
-  }
-  return rval;
-}
-
 // n := total elements
 // d := average depth
 // Stack: O(1)
@@ -111,3 +98,17 @@ void trie<K, V, C>::drop(iterator it) {
     it.stk.top()->impl_.erase(to_erase);
   }
 }
+
+namespace detail {
+  template <typename Iter, typename Trie, typename KS>
+  Iter find_impl(Trie * tr, KS const & keys) {
+    Iter rval{tr};
+    for (auto & key : keys) {
+      auto & top = rval.stk.top()->impl_;
+      auto it = top.find(key), end = top.end();
+      if ( it == end ) { return {}; }
+      rval.push({it, end});
+    }
+    return rval;
+  }
+}

+ 2 - 0
trie_iterator.hpp

@@ -76,6 +76,8 @@ namespace detail {
       else return *lhs == *rhs;
     }
     friend Trie;
+    template <typename Self, typename _Trie, typename KS>
+    friend Self find_impl(_Trie * tr, KS const & keys);
   };
 }