summaryrefslogtreecommitdiffstats
path: root/kicker/applets/launcher/easyvector.h
blob: f2bd827e52fb4a2ed9bbe22709f20400c70680c6 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*  Copyright 2004, Daniel Woods Bullok <[email protected]>
    distributed under the terms of the
    GNU GENERAL PUBLIC LICENSE Version 2 -
    See the file tdebase/COPYING for details
*/

#ifndef __easyvector_h__
#define __easyvector_h__
#include <vector>
#include <algorithm>
#include <assert.h>

template < class VALUE >
class __Valtype {
public:
    typedef const VALUE& CVALUE;
};


template < class VALUE >
class __Valtype< VALUE* > {
public:
    typedef const VALUE* CVALUE;
};


template <class VALUE, bool CHECKINDEX=true>
class EasyVector: public std::vector< VALUE > {
public:
    typedef int Index;
    typedef std::vector< Index > Indices;
    typedef typename __Valtype< VALUE >::CVALUE CVALUE;

    static const Index NotFound=-2;
    static const Index Append=-1;

    template < class PTYPE, class PROP_FUNC >
    Index findProperty(const PTYPE &property,
                       PROP_FUNC prop_func) const;
    Index findValue(CVALUE value) const;

    Index lastIndex() const {return this->size()-1;}

    void eraseAt(Index index);

    VALUE takeFrom(Index index);

    void insertAt(Index index,const VALUE &value);
    void insertAt(Index index,const EasyVector &values);

    bool isValidIndex(Index index) const;
    bool isValidInsertIndex(Index index) const;
    virtual ~EasyVector(){};


protected:
    void _checkInsertIndex(Index index) const;
    void _checkIndex(Index index) const;
    Index _convertInsertIndex(Index index) const;
};


template < class VALUE, bool CHECKINDEX >
template < class PTYPE, class PROP_FUNC >
typename EasyVector< VALUE, CHECKINDEX >::Index
    EasyVector< VALUE, CHECKINDEX >::findProperty(const PTYPE &property,
                                                  PROP_FUNC prop_func) const
{   typename EasyVector< VALUE, CHECKINDEX >::const_iterator i;
    for (i=this->begin();i!=this->end();++i) {
        if (prop_func(*i)==property)
            return i-this->begin();
    }
    return NotFound;
}


template < class VALUE, bool CHECKINDEX >
typename EasyVector< VALUE, CHECKINDEX >::Index
    EasyVector< VALUE, CHECKINDEX >::findValue(CVALUE value) const
{   typename EasyVector< VALUE, CHECKINDEX >::const_iterator i;
    i=std::find(this->begin(),this->end(),value);
    if (i==this->end()) return NotFound;
    return i-this->begin();
}


template < class VALUE, bool CHECKINDEX >
void EasyVector< VALUE, CHECKINDEX >::eraseAt(Index index)
{   _checkIndex(index);
    this->erase(this->begin()+index);
}


template < class VALUE, bool CHECKINDEX >
VALUE EasyVector< VALUE, CHECKINDEX >::takeFrom(Index index)
{   _checkIndex(index);
    VALUE result=(*this)[index];
    eraseAt(index);
    return result;
}


template < class VALUE, bool CHECKINDEX >
void EasyVector< VALUE, CHECKINDEX >::insertAt(EasyVector< VALUE, CHECKINDEX >::Index index,const VALUE &value)
{   index=_convertInsertIndex(index);
    _checkInsertIndex(index);
    if (index==int(this->size())) {
        this->push_back(value);
        return;
    }
    this->insert(this->begin()+index,value);
}


template < class VALUE, bool CHECKINDEX >
void EasyVector< VALUE, CHECKINDEX >::insertAt(EasyVector< VALUE, CHECKINDEX >::Index index,const EasyVector< VALUE, CHECKINDEX > &v)
{   index=_convertInsertIndex(index);
    _checkInsertIndex(index);
    this->insert(this->begin()+index,v.begin(),v.end());
}


template < class VALUE, bool CHECKINDEX >
bool EasyVector< VALUE, CHECKINDEX >::isValidIndex(EasyVector< VALUE, CHECKINDEX >::Index index) const
{   return(0<=index && index<int(this->size()));}

template < class VALUE, bool CHECKINDEX >
bool EasyVector< VALUE, CHECKINDEX >::isValidInsertIndex(EasyVector< VALUE, CHECKINDEX >::Index index) const
{   return(index==Append)||(0<=index && index<=int(this->size()));}

template < class VALUE, bool CHECKINDEX >
inline typename EasyVector< VALUE, CHECKINDEX >::Index EasyVector< VALUE, CHECKINDEX >::_convertInsertIndex(Index index) const
{   if (index==Append) return this->size();
    return index;
}

template < class VALUE, bool CHECKINDEX >
void EasyVector< VALUE, CHECKINDEX >::_checkInsertIndex(Index index) const
{   if (CHECKINDEX) assert (isValidInsertIndex(index));}

template < class VALUE, bool CHECKINDEX >
void EasyVector< VALUE, CHECKINDEX >::_checkIndex(Index index) const
{   if (CHECKINDEX) assert (isValidIndex(index));}


#endif