[Aptitude-svn-commit] r3971 - in branches/aptitude-0.3/aptitude: . src/generic tests

Daniel Burrows dburrows at costa.debian.org
Mon Aug 29 03:09:40 UTC 2005


Author: dburrows
Date: Mon Aug 29 03:09:36 2005
New Revision: 3971

Modified:
   branches/aptitude-0.3/aptitude/ChangeLog
   branches/aptitude-0.3/aptitude/src/generic/immset.h
   branches/aptitude-0.3/aptitude/tests/test_wtree.cc
Log:
Add a mapping wrapper to the wtree code.

Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog	(original)
+++ branches/aptitude-0.3/aptitude/ChangeLog	Mon Aug 29 03:09:36 2005
@@ -1,3 +1,10 @@
+2005-08-28  Daniel Burrows  <dburrows at debian.org>
+
+	* src/generic/immset.h, tests/test_wtree.cc:
+
+	  Rename wtree to set, and add a wrapper class to make it work as
+	  a mapping.
+
 2005-08-27  Daniel Burrows  <dburrows at debian.org>
 
 	* src/generic/immset.h, tests/test_wtree.cc:

Modified: branches/aptitude-0.3/aptitude/src/generic/immset.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/immset.h	(original)
+++ branches/aptitude-0.3/aptitude/src/generic/immset.h	Mon Aug 29 03:09:36 2005
@@ -320,7 +320,7 @@
   /** An entire weighted tree.
    */
   template<typename Val, typename Compare = std::less<Val>, int w = 4 >
-  class wtree
+  class set
   {
   public:
     typedef Val value_type;
@@ -503,13 +503,14 @@
         return splice_trees(n.getLeft(), n.getRight());
     }
 
-    wtree(const node &n)
-      :root(n)
+    set(const node &n, const Compare &_value_compare)
+      :root(n), value_compare(_value_compare)
     {
     }
   public:
     /** Construct an empty tree. */
-    wtree()
+    set(const Compare &_value_compare = Compare())
+      : value_compare(_value_compare)
     {
     }
 
@@ -518,24 +519,24 @@
      *  old tree; instead, it returns a new tree containing the
      *  element in addition to the elements of the old tree.
      */
-    static wtree add(const wtree &old, const Val &x)
+    static set add(const set &old, const Val &x)
     {
-      return old.insert(old.root, x);
+      return set(old.add(old.root, x), old.value_compare);
     }
 
     /** Like insert, but updates existing equivalent elements. */
-    static wtree addUpdate(const wtree &old, const Val &x)
+    static set addUpdate(const set &old, const Val &x)
     {
-      return old.addUpdate(old.root, x);
+      return set(old.addUpdate(old.root, x), old.value_compare);
     }
 
     /** Remove x from the tree. */
-    static wtree remove(const wtree &old, const Val &x)
+    static set remove(const set &old, const Val &x)
     {
-      return old.remove(old.root, x);
+      return set(old.remove(old.root, x), old.value_compare);
     }
 
-    /** Do an "in-place" update of this wtree, by replacing the root
+    /** Do an "in-place" update of this set, by replacing the root
      *  with a new root.
      */
     void insert(const Val &x)
@@ -558,7 +559,7 @@
     /** Find a tree node by value.  \return the node, or an invalid
      *	node if none exists.
      */
-    node find_node(const Val &x)
+    node find_node(const Val &x) const
     {
       node rval = root;
 
@@ -575,6 +576,12 @@
       return rval;
     }
 
+    /** \return \b true if this set contains the given value. */
+    bool contains(const Val &x) const
+    {
+      return find_node(x).isValid();
+    }
+
     const_iterator begin() const
     {
       return const_iterator(root);
@@ -595,6 +602,119 @@
       return root.empty();
     }
   };
+
+  template<typename Key, typename Val, typename Compare = std::less<Val> >
+  class map
+  {
+  public:
+    struct key_compare
+    {
+      Compare real_cmp;
+    public:
+      key_compare(const Compare &_real_cmp)
+	:real_cmp(_real_cmp)
+      {
+      }
+
+      bool operator()(const std::pair<Key, Val> &p1,
+		      const std::pair<Key, Val> &p2) const
+      {
+	return real_cmp(p1.first, p2.first);
+      }
+    };
+    typedef std::pair<Key, Val> binding_type;
+    typedef set<binding_type, key_compare> mapping_type;
+    typedef typename mapping_type::const_iterator const_iterator;
+    typedef typename mapping_type::size_type size_type;
+
+  private:
+    mapping_type contents;
+
+  public:
+    /** Construct a map directly from a set of bindings. */
+    map(const mapping_type &_contents)
+      :contents(_contents)
+    {
+    }
+
+    /** Construct an empty map */
+    map(const Compare &value_compare = Compare())
+      :contents(mapping_type(key_compare(value_compare)))
+    {
+    }
+
+    mapping_type get_bindings() const
+    {
+      return contents;
+    }
+
+    const_iterator begin() const
+    {
+      return contents.begin();
+    }
+
+    const_iterator end() const
+    {
+      return contents.end();
+    }
+
+    bool empty() const
+    {
+      return contents.empty();
+    }
+
+    size_type size() const
+    {
+      return contents.size();
+    }
+
+    /** \return either the node corresponding to the given key,
+     *  or an empty tree.
+     */
+    typename mapping_type::node lookup(const Key &k)
+    {
+      return contents.find_node(binding_type(k, Val()));
+    }
+
+    /** \return either the value of the mapping at k, or dflt if k is
+     *  unbound.
+     */
+    Val get(const Key &k, const Val &dflt)
+    {
+      typename mapping_type::node found = contents.find_node(binding_type(k, Val()));
+
+      if(found.isValid())
+	return found.getVal().second;
+      else
+	return dflt;
+    }
+
+    /** \return a new map that binds k to v, overwriting any existing
+     *  binding.
+     */
+    static map bind(const map &m, const Key &k, const Val &v)
+    {
+      return map(mapping_type::add(m.contents, binding_type(k, v)));
+    }
+
+    /** \return a new map based on m in which k is unbound. */
+    static map unbind(const map &m, const Key &k)
+    {
+      return map(mapping_type::erase(m.contents, binding_type(k, Val())));
+    } 
+
+    /** Add a binding to this map. */
+    void put(const Key &k, const Val &v)
+    {
+      contents.insert(binding_type(k, v));
+    }
+
+    /** Delete a binding from this map by key. */
+    void erase(const Key &k)
+    {
+      contents.erase(binding_type(k, Val()));
+    }
+  };
 };
 
 #endif

Modified: branches/aptitude-0.3/aptitude/tests/test_wtree.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/tests/test_wtree.cc	(original)
+++ branches/aptitude-0.3/aptitude/tests/test_wtree.cc	Mon Aug 29 03:09:36 2005
@@ -21,7 +21,8 @@
 
 #include <src/generic/immset.h>
 
-using imm::wtree;
+using imm::map;
+using imm::set;
 using imm::wtree_node;
 
 class WTreeTest : public CppUnit::TestFixture
@@ -30,6 +31,7 @@
 
   CPPUNIT_TEST(testNodeRotate);
   CPPUNIT_TEST(generalWTreeTest);
+  CPPUNIT_TEST(mapTest);
 
   CPPUNIT_TEST_SUITE_END();
 public:
@@ -102,14 +104,14 @@
     CPPUNIT_ASSERT(b_right.getVal() == 8);
   }
 
-  static bool WTreeValuesMatch(const wtree<int> &t,
+  static bool WTreeValuesMatch(const set<int> &t,
 			       const int *values, int count)
   {
     if(count == 0)
       return t.empty();
     else
       {
-	wtree<int>::const_iterator i = t.begin();
+	set<int>::const_iterator i = t.begin();
 
 	while(i != t.end() && count > 0)
 	  {
@@ -126,7 +128,7 @@
   }
 
   static void dumpMatchFailure(std::ostream &out,
-			       const wtree<int> &t,
+			       const set<int> &t,
 			       const int *values,
 			       int count)
   {
@@ -143,7 +145,7 @@
 
     out << "}; got {";
 
-    for(wtree<int>::const_iterator i = t.begin(); i != t.end(); ++i)
+    for(set<int>::const_iterator i = t.begin(); i != t.end(); ++i)
       {
 	if(i != t.begin())
 	  out << ", ";
@@ -169,7 +171,7 @@
 
   void generalWTreeTest()
   {
-    wtree<int> t;
+    set<int> t;
 
     assertWTreeValues(t, NULL, 0);
 
@@ -307,6 +309,51 @@
       assertWTreeValues(t, vals, 0);
     }
   }
+
+  void mapTest()
+  {
+    map<int, int> m;
+
+    CPPUNIT_ASSERT(m.empty());
+    CPPUNIT_ASSERT_EQUAL(m.size(), 0U);
+
+    m.put(5, 2);
+    CPPUNIT_ASSERT(!m.empty());
+    CPPUNIT_ASSERT_EQUAL(m.size(), 1U);
+
+    m.put(3, 10);
+    CPPUNIT_ASSERT(!m.empty());
+    CPPUNIT_ASSERT_EQUAL(m.size(), 2U);
+
+    m.put(4, 10);
+    CPPUNIT_ASSERT(!m.empty());
+    CPPUNIT_ASSERT_EQUAL(m.size(), 3U);
+
+    m.put(1, 3);
+    CPPUNIT_ASSERT(!m.empty());
+    CPPUNIT_ASSERT_EQUAL(m.size(), 4U);
+
+    m.put(2, 8);
+    CPPUNIT_ASSERT(!m.empty());
+    CPPUNIT_ASSERT_EQUAL(m.size(), 5U);
+
+    m.put(6, 11);
+    CPPUNIT_ASSERT(!m.empty());
+    CPPUNIT_ASSERT_EQUAL(m.size(), 6U);
+
+    m.put(1, 0);
+    CPPUNIT_ASSERT(!m.empty());
+    CPPUNIT_ASSERT_EQUAL(m.size(), 7U);
+
+
+    CPPUNIT_ASSERT_EQUAL(m.get(0, -1), -1);
+    CPPUNIT_ASSERT_EQUAL(m.get(1, -1), 3);
+    CPPUNIT_ASSERT_EQUAL(m.get(2, -1), 8);
+    CPPUNIT_ASSERT_EQUAL(m.get(3, -1), 10);
+    CPPUNIT_ASSERT_EQUAL(m.get(4, -1), 10);
+    CPPUNIT_ASSERT_EQUAL(m.get(5, -1), 2);
+    CPPUNIT_ASSERT_EQUAL(m.get(6, -1), 11);
+  }
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(WTreeTest);



More information about the Aptitude-svn-commit mailing list