diff options
author | Erkin Bahceci <erkinbah@gmail.com> | 2009-12-15 21:15:07 -0600 |
---|---|---|
committer | Erkin Bahceci <erkinbah@gmail.com> | 2009-12-16 06:14:32 -0600 |
commit | f842469d7ed8cd4a8a0a7f6c81ef148d1597460d (patch) | |
tree | fbd9325ebfaec1053dbd96ff4122986d9c3198a5 /include/core/countedlist.h | |
parent | 6b9eb15b09fa68ad35b176ef2a9ad1d211b180dd (diff) | |
download | mobileperf-f842469d7ed8cd4a8a0a7f6c81ef148d1597460d.tar.gz mobileperf-f842469d7ed8cd4a8a0a7f6c81ef148d1597460d.tar.bz2 |
Add CountedList class, a wrapper for std::list.
This std::list wrapper is to be used for cases where size() needs to be called,
since std::list.size() has linear complexity in gcc, whereas CountedList::size()
has constant complexity, as it keeps a counter.
Diffstat (limited to 'include/core/countedlist.h')
-rw-r--r-- | include/core/countedlist.h | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/include/core/countedlist.h b/include/core/countedlist.h new file mode 100644 index 0000000..304338c --- /dev/null +++ b/include/core/countedlist.h @@ -0,0 +1,114 @@ +/* + * countedlist.h + * + * Created on: Dec 15, 2009 + * Author: erkin + */ + +#ifndef COUNTEDLIST_H_ +#define COUNTEDLIST_H_ + + +template<typename value_type, + typename allocator_type = std::allocator<value_type> > +class CountedList : protected std::list<value_type, allocator_type> +{ +public: + typedef std::list<value_type, allocator_type> Base; + typedef typename Base::size_type size_type; + typedef typename Base::iterator iterator; + typedef typename Base::const_iterator const_iterator; + typedef typename Base::reverse_iterator reverse_iterator; + typedef typename Base::const_reverse_iterator const_reverse_iterator; + typedef typename Base::reference reference; + typedef typename Base::const_reference const_reference; + + CountedList () : Base (), mCount (0) { } + + // Overriden std::list methods + + bool empty () const { return Base::empty (); } + size_type size () const { return mCount; } + size_type max_size() const { return Base::max_size(); } + + iterator begin () { return Base::begin (); } + const_iterator begin () const { return Base::begin (); } + iterator end () { return Base::end (); } + const_iterator end () const { return Base::end (); } + + reverse_iterator rbegin () { return Base::rbegin (); } + const_reverse_iterator rbegin () const { return Base::rbegin (); } + reverse_iterator rend () { return Base::rend (); } + const_reverse_iterator rend () const { return Base::rend (); } + + reference front () { return Base::front (); } + const_reference front () const { return Base::front (); } + reference back () { return Base::back (); } + const_reference back () const { return Base::back (); } + + void reverse () { Base::reverse (); } + void sort () { Base::sort (); } + + template<typename _StrictWeakOrdering> + void sort(_StrictWeakOrdering __comp) { Base::sort (__comp); } + + void resize (size_type __new_size, value_type __x = value_type ()) + { + mCount = __new_size; + Base::resize (__new_size, __x); + } + void clear () + { + mCount = 0; + Base::clear (); + } + void push_front (const value_type& __x) + { + mCount++; + Base::push_front (__x); + } + void push_back (const value_type& __x) + { + mCount++; + Base::push_back (__x); + } + void pop_front () + { + if (mCount > 0) + mCount--; + Base::pop_front (); + } + void pop_back () + { + if (mCount > 0) + mCount--; + Base::pop_back (); + } + iterator insert (iterator __position, const value_type& __x) + { + mCount++; + return Base::insert (__position, __x); + } + void insert (iterator __position, size_type __n, const value_type& __x) + { + mCount += __n; + Base::insert (__position, __n, __x); + } + iterator erase (iterator __position) + { + if (mCount > 0) + mCount--; + return Base::erase (__position); + } + void remove (const value_type& __value) + { + if (mCount > 0) + mCount--; + Base::remove (__value); + } + +protected: + size_type mCount; +}; + +#endif /* COUNTEDLIST_H_ */ |