Skip to content

Commit

Permalink
std::reverse_iterator used instead of trie_reverse_iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
cosminBoaca committed Feb 22, 2015
1 parent 8423ad3 commit 8ac0025
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 261 deletions.
109 changes: 7 additions & 102 deletions boost/trie/trie.hpp
Expand Up @@ -147,9 +147,9 @@ struct trie_iterator
{
typedef std::bidirectional_iterator_tag iterator_category;
typedef Key key_type;
typedef Value value_type;
typedef Reference reference;
typedef Pointer pointer;
typedef std::pair<std::vector<key_type>, Value> value_type;
typedef std::pair<std::vector<key_type>, Reference> reference;
typedef std::pair<std::vector<key_type>, Reference>* pointer;
typedef ptrdiff_t difference_type;
typedef trie_iterator<Key, Value, Value&, Value*> iterator;
typedef trie_iterator<Key, Value, Reference, Pointer> iter_type;
Expand Down Expand Up @@ -189,7 +189,7 @@ struct trie_iterator
* a function returns the key on the path should be invented
*
*/
std::vector<key_type> get_key()
std::vector<key_type> get_key() const
{
std::vector<key_type> key_path;
int path_length = 0;
Expand Down Expand Up @@ -230,7 +230,7 @@ struct trie_iterator

reference operator*() const
{
return vnode->value;
return std::make_pair(get_key(), std::ref(vnode->value));
}

pointer operator->() const
Expand Down Expand Up @@ -314,99 +314,6 @@ struct trie_iterator
}
};


template <typename TrieIterator>
struct trie_reverse_iterator {
typedef TrieIterator iter_type;
typedef trie_reverse_iterator<iter_type> self;
typedef typename std::iterator_traits<TrieIterator>::iterator_category iterator_category;
typedef typename std::iterator_traits<TrieIterator>::value_type value_type;
typedef typename std::iterator_traits<TrieIterator>::reference reference;
typedef typename std::iterator_traits<TrieIterator>::pointer pointer;
typedef typename std::iterator_traits<TrieIterator>::difference_type differcent_type;

typedef typename iter_type::key_type key_type;

private:
iter_type base_iter;

public:
trie_reverse_iterator()
{
}

explicit trie_reverse_iterator(iter_type it) : base_iter(it)
{

}
trie_reverse_iterator(const self &other) : base_iter(other.base())
{
}

template <typename Iter>
trie_reverse_iterator(const trie_reverse_iterator<Iter> &other) : base_iter(other.base())
{
}

iter_type base() const
{
return base_iter;
}

std::vector<key_type> get_key()
{
iter_type tmp = base_iter;
return (--tmp).get_key();
}

reference operator*() const
{
iter_type tmp = base_iter;
return *--tmp;
}

pointer operator->() const
{
return &(operator*());
}

bool operator==(const self& other) const
{
return base() == other.base();
}

bool operator!=(const self& other) const
{
return base() != other.base();
}


self& operator++()
{
--base_iter;
return *this;
}
self operator++(int)
{
self tmp = *this;
--base_iter;
return tmp;
}

self& operator--()
{
++base_iter;
return *this;
}
self operator--(int)
{
self tmp = *this;
++base_iter;
return tmp;
}
};


} // namespace detail


Expand Down Expand Up @@ -779,10 +686,8 @@ class trie {

typedef detail::trie_iterator<Key, Value, Value&, Value*> iterator;
typedef typename iterator::const_iterator const_iterator;
typedef detail::trie_reverse_iterator<iterator> reverse_iterator;
typedef detail::trie_reverse_iterator<const_iterator> const_reverse_iterator;
//typedef detail::trie_reverse_iterator<Key, Value, Value&, Value*> reverse_iterator;
//typedef typename reverse_iterator::const_reverse_iterator const_reverse_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
typedef std::pair<iterator, bool> pair_iterator_bool;
typedef std::pair<iterator, iterator> iterator_range;

Expand Down
2 changes: 1 addition & 1 deletion boost/trie/trie_map.hpp
Expand Up @@ -110,7 +110,7 @@ class trie_map
template<typename Container>
value_type& operator [] (const Container& container)
{
return *(t.insert_unique(container, value_type()).first);
return (*(t.insert_unique(container, value_type()).first)).second;
}

// insert
Expand Down
66 changes: 33 additions & 33 deletions libs/trie/test/test_custom_type.cpp
Expand Up @@ -27,7 +27,7 @@ class mytype {
{
return value < x.value;
}
bool operator==(const mytype& x) const
bool operator ==(const mytype& x) const
{
return value == x.value;
}
Expand All @@ -38,7 +38,7 @@ class mytype {
typedef boost::tries::trie_map<mytype, mytype> tmm;
typedef std::vector<mytype> vm;

std::ostream& operator<<(std::ostream& out, const mytype& m)
std::ostream& operator <<(std::ostream& out, const mytype& m)
{
out << m.value;
return out;
Expand Down Expand Up @@ -77,7 +77,7 @@ BOOST_AUTO_TEST_CASE(operator_test)
BOOST_CHECK(t.count_node() == 4);
BOOST_CHECK(t[s] == t[s2]);
BOOST_CHECK(t.count_node() == 4);
BOOST_CHECK(*t.begin() == mytype(5));
BOOST_CHECK((*t.begin()).second == mytype(5));
BOOST_CHECK(t.size() == 2);
BOOST_CHECK(t.count_node() == 4);
std::cout << t.count_node() << std::endl;
Expand All @@ -87,12 +87,12 @@ BOOST_AUTO_TEST_CASE(insert_and_find_test)
{
tmm t;
t[s] = 1;
BOOST_CHECK(*t.find(s) == 1);
BOOST_CHECK((*t.find(s)).second == 1);
BOOST_CHECK(t.find(s) != t.end());
BOOST_CHECK(t.find(s2) == t.end());
BOOST_CHECK(t.find(s2) == t.end());
t[s] = 2;
BOOST_CHECK(*t.find(s) == 2);
BOOST_CHECK((*t.find(s)).second == 2);
t[s2] = t[s];
BOOST_CHECK(t.find(s2) != t.end());
BOOST_CHECK(t.find(s1) == t.find(s3));
Expand All @@ -108,23 +108,23 @@ BOOST_AUTO_TEST_CASE(copy_test)
tmm t, t2;
t[s] = 1; t[s1] = 2; t[s2] = 3; t[s3] = 4;
t2 = t;
std::cout <<t[s]<<std::endl;
std::cout <<t[s] <<std::endl;
size_t node_cnt = 4 + 2 + 4;
BOOST_CHECK(t2.size() == 4);
BOOST_CHECK(t2.count_node() == node_cnt);
std::cout <<t[s]<<std::endl;
std::cout<< t2[s] << std::endl;
std::cout <<t[s] <<std::endl;
std::cout << t2[s] << std::endl;
BOOST_CHECK(t2[s] == 1);
BOOST_CHECK(t2[s1] == 2);
BOOST_CHECK(t2[s2] == 3);
BOOST_CHECK(t2[s3] == 4);
tmm t3(t2);
BOOST_CHECK(t3.size() == 4);
BOOST_CHECK(t3.count_node() == node_cnt);
BOOST_CHECK(*t3.find(s) == 1);
BOOST_CHECK(*t3.find(s1) == 2);
BOOST_CHECK(*t3.find(s2) == 3);
BOOST_CHECK(*t3.find(s3) == 4);
BOOST_CHECK((*t3.find(s)).second == 1);
BOOST_CHECK((*t3.find(s1)).second == 2);
BOOST_CHECK((*t3.find(s2)).second == 3);
BOOST_CHECK((*t3.find(s3)).second == 4);
BOOST_CHECK(t3.find(ms1, ms1 + 3) == t3.end());
BOOST_CHECK(t3.find(ms1, ms1 + 4) != t3.end());
}
Expand All @@ -134,22 +134,22 @@ BOOST_AUTO_TEST_CASE(iterator_operator_plus)
tmm t;
BOOST_CHECK(t.empty() == true);
BOOST_CHECK(t.size() == 0);
std::cout <<"ddd"<<std::endl;
std::cout <<"ddd" <<std::endl;
BOOST_CHECK(t.begin() == t.end());
std::cout <<"ddd"<<std::endl;
std::cout <<"ddd" <<std::endl;
t[s] = 1;
t[s1] = 2;
t[s2] = 3;
BOOST_CHECK(t.begin() != t.end());
std::cout <<"ddd"<<std::endl;
std::cout <<"ddd" <<std::endl;
tmm::iterator ti;
ti = t.begin();
BOOST_CHECK(*ti == 1);
BOOST_CHECK((*ti).second == 1);
++ti;
BOOST_CHECK(*ti == 3);
BOOST_CHECK((*ti).second == 3);
BOOST_CHECK(t[s2] == 3);
++ti;
BOOST_CHECK(*ti == 2);
BOOST_CHECK((*ti).second == 2);
++ti;
BOOST_CHECK(ti == t.end());
// test ++end()
Expand All @@ -170,26 +170,26 @@ BOOST_AUTO_TEST_CASE(iterator_operator_minus)
tmm::iterator ti;
tmm::const_iterator cti(t.end());
ti = t.begin();
BOOST_CHECK(*ti == 1);
*ti = 10;
BOOST_CHECK(*ti == 10);
BOOST_CHECK((*ti).second == 1);
(*ti).second = 10;
BOOST_CHECK((*ti).second == 10);
cti = t.begin();
BOOST_CHECK(*cti == 10);
BOOST_CHECK((*cti).second == 10);
--ti;
BOOST_CHECK(ti == t.begin());
BOOST_CHECK(t[s2] == 3);
ti = t.end();
--ti;
BOOST_CHECK(*ti == 2);
BOOST_CHECK((*ti).second == 2);
t[s3] = 4;
++ti;
BOOST_CHECK(*ti == 4);
BOOST_CHECK((*ti).second == 4);
++ti;
BOOST_CHECK(ti == t.end());
++ti;
BOOST_CHECK(ti == t.end());
--ti;
BOOST_CHECK(*ti == 4);
BOOST_CHECK((*ti).second == 4);
int node_cnt = 4 + 2 + 4 + 2;
t[s4] = 5;
t[s5] = 6;
Expand Down Expand Up @@ -229,16 +229,16 @@ BOOST_AUTO_TEST_CASE(erase_iterator)
BOOST_CHECK(t.size() == 3);
//std::cout << t.count_node() << std::endl;
BOOST_CHECK(t.count_node() == node_cnt);
BOOST_CHECK(*ti == 3);
BOOST_CHECK((*ti).second == 3);
t.erase(ti);
BOOST_CHECK(t.size() == 2);
ti = t.begin();
BOOST_CHECK(*ti == 2);
BOOST_CHECK((*ti).second == 2);
BOOST_CHECK(t.count_node() == 8);
BOOST_CHECK(t[s] == 0);
ti = t.begin();
BOOST_CHECK(t.size() == 3);
BOOST_CHECK(*ti == 0);
BOOST_CHECK((*ti).second == 0);
BOOST_CHECK(t.count_node() == 8);
}

Expand All @@ -258,22 +258,22 @@ BOOST_AUTO_TEST_CASE(erase_key)
ti = t.begin();
// erasion failure
BOOST_CHECK(t.size() == 4);
BOOST_CHECK(*ti == 1);
BOOST_CHECK((*ti).second == 1);
t.erase(ms1, ms1 + 2);
ti = t.begin();
BOOST_CHECK(t.size() == 3);
BOOST_CHECK(*ti == 3);
BOOST_CHECK((*ti).second == 3);
t.erase(s1);
BOOST_CHECK(t.size() == 2);
ti = t.begin();
BOOST_CHECK(*ti == 3);
BOOST_CHECK((*ti).second == 3);
BOOST_CHECK(t[s] == 0);
ti = t.begin();
BOOST_CHECK(t.size() == 3);
BOOST_CHECK(*ti == 0);
BOOST_CHECK((*ti).second == 0);
ti = t.begin();
BOOST_CHECK(t.size() == 3);
BOOST_CHECK(*ti == 0);
BOOST_CHECK((*ti).second == 0);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 8ac0025

Please sign in to comment.