[Aptitude-svn-commit] r3993 - in branches/aptitude-0.3/aptitude: .
src/generic tests
Daniel Burrows
dburrows at costa.debian.org
Tue Aug 30 16:38:20 UTC 2005
Author: dburrows
Date: Tue Aug 30 16:38:17 2005
New Revision: 3993
Modified:
branches/aptitude-0.3/aptitude/ChangeLog
branches/aptitude-0.3/aptitude/src/generic/setset.h
branches/aptitude-0.3/aptitude/tests/test_setset.cc
Log:
Add a setset wrapper for sets of maps.
Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog (original)
+++ branches/aptitude-0.3/aptitude/ChangeLog Tue Aug 30 16:38:17 2005
@@ -1,5 +1,9 @@
2005-08-30 Daniel Burrows <dburrows at debian.org>
+ * src/generic/setset.h, tests/test_setset.cc:
+
+ Add a setset wrapper for sets of maps.
+
* src/generic/immset.h, tests/test_wtree.cc:
Make imm::map::put update existing entries.
Modified: branches/aptitude-0.3/aptitude/src/generic/setset.h
==============================================================================
--- branches/aptitude-0.3/aptitude/src/generic/setset.h (original)
+++ branches/aptitude-0.3/aptitude/src/generic/setset.h Tue Aug 30 16:38:17 2005
@@ -41,9 +41,6 @@
template<typename Val, typename Compare = std::less<Val> >
class setset
{
-public:
- typedef typename std::vector<imm::set<Val, Compare> >::const_iterator const_iterator;
-
private:
std::vector<imm::set<Val, Compare> > entries;
@@ -56,7 +53,12 @@
index_type sets_by_key;
+public:
+ typedef typename entries_list::const_iterator const_iterator;
+
+ typedef typename index_type::size_type size_type;
+private:
// Used to construct a set traversal that populates the sets_by_key
// structure.
struct populate_sets_by_key
@@ -184,5 +186,136 @@
}
};
+template<typename Key, typename Val, typename Compare = std::less<Key> >
+class mapset
+{
+ typedef setset<std::pair<Key, Val>, imm::key_compare<Key, Val, Compare> > mapset_type;
+
+ mapset_type S;
+
+public:
+ typedef typename mapset_type::size_type size_type;
+
+ class const_iterator
+ {
+ typename mapset_type::const_iterator realiter;
+ public:
+ const_iterator(const typename mapset_type::const_iterator &_realiter)
+ :realiter(_realiter)
+ {
+ }
+
+ const_iterator(const const_iterator &other)
+ :realiter(other.realiter)
+ {
+ }
+
+ const_iterator &operator=(const const_iterator &other)
+ {
+ realiter = other.realiter;
+ return *this;
+ }
+
+ imm::map<Key, Val> operator*() const
+ {
+ return *realiter;
+ }
+
+ bool operator==(const const_iterator &other) const
+ {
+ return realiter == other.realiter;
+ }
+
+ bool operator!=(const const_iterator &other) const
+ {
+ return realiter != other.realiter;
+ }
+
+ bool operator<(const const_iterator &other) const
+ {
+ return realiter < other.realiter;
+ }
+
+ bool operator>(const const_iterator &other) const
+ {
+ return realiter > other.realiter;
+ }
+
+ bool operator<=(const const_iterator &other) const
+ {
+ return realiter <= other.realiter;
+ }
+
+ bool operator>=(const const_iterator &other) const
+ {
+ return realiter >= other.realiter;
+ }
+
+ const_iterator &operator++()
+ {
+ ++realiter;
+ return *this;
+ }
+
+ const_iterator &operator--()
+ {
+ --realiter;
+ return *this;
+ }
+
+ const_iterator operator+(size_type i) const
+ {
+ return realiter + i;
+ }
+
+ const_iterator operator+=(size_type i) const
+ {
+ return realiter + i;
+ }
+
+ const_iterator operator-(size_type i) const
+ {
+ return realiter - i;
+ }
+
+ const_iterator operator-=(size_type i) const
+ {
+ return realiter - i;
+ }
+ };
+
+ mapset(const Compare &c = Compare())
+ :S(imm::key_compare<Key, Val, Compare>(c))
+ {
+ }
+
+ const_iterator begin() const
+ {
+ return S.begin();
+ }
+
+ const_iterator end() const
+ {
+ return S.end();
+ }
+
+ void insert(const imm::map<Key, Val, Compare> &m)
+ {
+ S.insert(m.get_bindings());
+ }
+
+ const_iterator find_submap(const imm::map<Key, Val, Compare> &m) const
+ {
+ return const_iterator(S.find_subset(m.get_bindings()));
+ }
+
+ template<typename R>
+ const_iterator find_submap(const imm::map<Key, Val, Compare> &m,
+ const R &r) const
+ {
+ return const_iterator(S.find_subset(m.get_bindings(), r));
+ }
+};
+
#endif // SETSET_H
Modified: branches/aptitude-0.3/aptitude/tests/test_setset.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/tests/test_setset.cc (original)
+++ branches/aptitude-0.3/aptitude/tests/test_setset.cc Tue Aug 30 16:38:17 2005
@@ -21,6 +21,26 @@
#include <src/generic/setset.h>
+#include <iostream>
+
+template<typename Key, typename Val, typename Compare>
+inline
+std::ostream &operator<<(std::ostream &out, const imm::map<Key, Val, Compare> &s)
+{
+ out << "{";
+
+ for(typename imm::map<Key, Val, Compare>::const_iterator i = s.begin(); i != s.end(); ++i)
+ {
+ if(i != s.begin())
+ out << ", ";
+ out << i->first << " => " << i->second;
+ }
+
+ out << "}";
+
+ return out;
+}
+
template<typename T, typename Compare>
inline
std::ostream &operator<<(std::ostream &out, const imm::set<T, Compare> &s)
@@ -46,6 +66,8 @@
CPPUNIT_TEST(testSubsetSearch);
CPPUNIT_TEST(testSubsetPredicateSearch);
+ CPPUNIT_TEST(testSubmapSearch);
+
CPPUNIT_TEST_SUITE_END();
public:
@@ -80,7 +102,7 @@
found = S.find_subset(t);
CPPUNIT_ASSERT(found != S.end());
- CPPUNIT_ASSERT_EQUAL(*found, s2);
+ CPPUNIT_ASSERT_EQUAL(s2, *found);
t.insert(6);
t.insert(4);
@@ -107,17 +129,17 @@
s1.insert(5);
CPPUNIT_ASSERT(s1.contains(5));
- CPPUNIT_ASSERT_EQUAL(s1.size(), 1U);
+ CPPUNIT_ASSERT_EQUAL(1U, s1.size());
S.insert(s1);
s2.insert(6);
s2.insert(8);
- CPPUNIT_ASSERT_EQUAL(s2.size(), 2U);
+ CPPUNIT_ASSERT_EQUAL(2U, s2.size());
S.insert(s2);
s3.insert(9);
s3.insert(11);
- CPPUNIT_ASSERT_EQUAL(s3.size(), 2U);
+ CPPUNIT_ASSERT_EQUAL(2U, s3.size());
S.insert(s3);
imm::set<int, HalfCmp> t;
@@ -126,7 +148,7 @@
setset<int, HalfCmp>::const_iterator found = S.find_subset(t);
CPPUNIT_ASSERT(found != S.end());
- CPPUNIT_ASSERT_EQUAL(*found, s1);
+ CPPUNIT_ASSERT_EQUAL(s1, *found);
t.insert(10);
t.insert(11);
@@ -135,14 +157,14 @@
found = S.find_subset(t);
CPPUNIT_ASSERT(found != S.end());
- CPPUNIT_ASSERT_EQUAL(*found, s1);
+ CPPUNIT_ASSERT_EQUAL(s1, *found);
t.erase(5);
t.insert(4);
found = S.find_subset(t);
CPPUNIT_ASSERT(found != S.end());
- CPPUNIT_ASSERT_EQUAL(*found, s1);
+ CPPUNIT_ASSERT_EQUAL(s1, *found);
t.erase(4);
t.insert(7);
@@ -151,7 +173,7 @@
found = S.find_subset(t);
CPPUNIT_ASSERT(found != S.end());
- CPPUNIT_ASSERT_EQUAL(*found, s2);
+ CPPUNIT_ASSERT_EQUAL(s2, *found);
found = S.find_subset(t, std::greater<int>());
@@ -165,7 +187,55 @@
found = S.find_subset(t, std::greater<int>());
CPPUNIT_ASSERT(found != S.end());
- CPPUNIT_ASSERT_EQUAL(*found, s3);
+ CPPUNIT_ASSERT_EQUAL(s3, *found);
+ }
+
+ void testSubmapSearch()
+ {
+ imm::map<int, int> m1, m2, m3;
+
+ m1.put(1, 2);
+ m1.put(5, 2);
+
+ m2.put(1, 5);
+
+ m3.put(5, 2);
+ m3.put(6, 2);
+
+ mapset<int, int> S;
+
+ S.insert(m1);
+ S.insert(m2);
+ S.insert(m3);
+
+ imm::map<int, int> t;
+
+ t.put(1, 2);
+ t.put(3, 2);
+
+ mapset<int, int>::const_iterator found = S.find_submap(t);
+
+ CPPUNIT_ASSERT(found != S.end());
+ CPPUNIT_ASSERT_EQUAL(m2, *found);
+
+ found = S.find_submap(t, std::equal_to<std::pair<int, int> >());
+ CPPUNIT_ASSERT(found == S.end());
+
+ t.put(5, 2);
+ found = S.find_submap(t, std::equal_to<std::pair<int, int> >());
+ CPPUNIT_ASSERT(found != S.end());
+ CPPUNIT_ASSERT_EQUAL(m1, *found);
+
+ t.put(1, 5);
+ found = S.find_submap(t, std::equal_to<std::pair<int, int> >());
+ CPPUNIT_ASSERT(found != S.end());
+ CPPUNIT_ASSERT_EQUAL(m2, *found);
+
+ t.erase(1);
+ t.put(6, 2);
+ found = S.find_submap(t);
+ CPPUNIT_ASSERT(found != S.end());
+ CPPUNIT_ASSERT_EQUAL(m3, *found);
}
};
More information about the Aptitude-svn-commit
mailing list