summaryrefslogtreecommitdiff
path: root/include/core/countedlist.h
blob: 304338c1eb978c2490021d6cd166a705808aaa56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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_ */