Subversion Repositories gelsvn

Rev

Rev 513 | Rev 518 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 513 Rev 515
Line 10... Line 10...
10
 * ----------------------------------------------------------------------- */
10
 * ----------------------------------------------------------------------- */
11
 
11
 
12
#ifndef __HMESH_ITEMVECTOR_H__
12
#ifndef __HMESH_ITEMVECTOR_H__
13
#define __HMESH_ITEMVECTOR_H__
13
#define __HMESH_ITEMVECTOR_H__
14
 
14
 
-
 
15
//#include <cassert>
15
#include <vector>
16
#include <vector>
16
 
17
 
-
 
18
 
17
#include "ItemID.h"
19
#include "ItemID.h"
18
 
20
 
19
namespace HMesh
21
namespace HMesh
20
{
22
{
21
    template<typename ITEM>
23
    template<typename ITEM>
22
    class ItemVector
24
    class ItemVector
23
    {
25
    {
24
    public:
26
    public:
25
        /// default constructor
27
        /// default constructor
26
        ItemVector();
-
 
27
        ItemVector(IndexType _size, ITEM i = ITEM()); 
28
        ItemVector(IndexType _size = 0, ITEM i = ITEM()); 
28
 
29
 
29
        /// Get a reference to entity i from kernel
30
        /// Get a reference to entity i from kernel
30
        ITEM& get(IndexType i);
31
        ITEM& get(IndexType i);
31
        /// Get a const reference to entity i from kernel
32
        /// Get a const reference to entity i from kernel
32
        const ITEM& get(IndexType i) const;
33
        const ITEM& get(IndexType i) const;
Line 77... Line 78...
77
        /// Memory consideration - objects flagged as unused should be remembered for future use (unless purged)
78
        /// Memory consideration - objects flagged as unused should be remembered for future use (unless purged)
78
        std::vector<bool> active_items;
79
        std::vector<bool> active_items;
79
    };
80
    };
80
 
81
 
81
    template<typename ITEM>
82
    template<typename ITEM>
82
    inline ItemVector<ITEM>::ItemVector() : size_active(0) {}
83
    inline ItemVector<ITEM>::ItemVector(IndexType _size, ITEM i) 
83
 
-
 
-
 
84
        :   size_active(_size), 
84
    template<typename ITEM>
85
            items(_size, i), 
85
    inline ItemVector<ITEM>::ItemVector(IndexType _size, ITEM i = ITEM()) : size_active(_size), items(_size, i), active_items(_size, true){}
86
            active_items(_size, true){}
86
 
87
 
87
    template<typename ITEM>
88
    template<typename ITEM>
88
    ITEM& ItemVector<ITEM>::get(IndexType i)
89
    inline ITEM& ItemVector<ITEM>::get(IndexType i)
89
    {
90
    {
90
        assert(i < items.size());
91
        //std::assert(i < items.size());
91
        return items[i];
92
        return items[i];
92
    }
93
    }
93
 
94
 
94
    template<typename ITEM>
95
    template<typename ITEM>
95
    const ITEM& ItemVector<ITEM>::get(IndexType i) const
96
    inline const ITEM& ItemVector<ITEM>::get(IndexType i) const
96
    {
97
    {
97
        assert(i < items.size());
98
        //std::assert(i < items.size());
98
        return items[i];
99
        return items[i];
99
    }
100
    }
100
 
101
 
101
    template<typename ITEM>
102
    template<typename ITEM>
102
    ITEM& ItemVector<ITEM>::operator [](IndexType i)
103
    inline ITEM& ItemVector<ITEM>::operator [](IndexType i)
103
    {
104
    {
104
        assert(i < items.size());
105
        //std::assert(i < items.size());
105
        return items[i];
106
        return items[i];
106
    } 
107
    } 
107
 
108
 
108
    template<typename ITEM>
109
    template<typename ITEM>
109
    const ITEM& ItemVector<ITEM>::operator [](IndexType i) const
110
    inline const ITEM& ItemVector<ITEM>::operator [](IndexType i) const
110
    {
111
    {
111
        assert(i < items.size());
112
        //std::assert(i < items.size());
112
        return items[i];
113
        return items[i];
113
    }
114
    }
114
 
115
 
115
    template<typename ITEM>
116
    template<typename ITEM>
116
    IndexType ItemVector<ITEM>::add(const ITEM& i)
117
    inline IndexType ItemVector<ITEM>::add(const ITEM& i)
117
    {
118
    {
118
        items.push_back(i);
119
        items.push_back(i);
119
        active_items.push_back(true);
120
        active_items.push_back(true);
120
        ++size_active;
121
        ++size_active;
121
        return items.size() - 1;
122
        return items.size() - 1;
122
    }
123
    }
123
 
124
 
124
    template<typename ITEM>
125
    template<typename ITEM>
125
    void ItemVector<ITEM>::remove(IndexType i)
126
    inline void ItemVector<ITEM>::remove(IndexType i)
126
    {
127
    {
127
        if(active_items[i]){ 
128
        if(active_items[i]){ 
128
            --size_active;
129
            --size_active;
129
            active_items[i] = false;
130
            active_items[i] = false;
130
        }
131
        }
131
    }
132
    }
132
 
133
 
133
    template<typename ITEM>
134
    template<typename ITEM>
134
    void ItemVector<ITEM>::cleanup()
135
    inline void ItemVector<ITEM>::cleanup()
135
    {
136
    {
136
        std::vector<ITEM> new_items;
137
        std::vector<ITEM> new_items;
137
        for(IndexType i = 0; i < items.size(); ++i){
138
        for(IndexType i = 0; i < items.size(); ++i){
138
            if(active_items[i]) 
139
            if(active_items[i]) 
139
                new_items.push_back(items[i]);          
140
                new_items.push_back(items[i]);          
140
        }
141
        }
141
        items = new_items;
142
        std::swap(items, new_items);
142
        active_items = std::vector<bool>(items.size(), true);
143
        active_items = std::vector<bool>(items.size(), true);
143
        size_active = items.size();
144
        size_active = items.size();
144
    }
145
    }
145
 
146
 
146
    template<typename ITEM>
147
    template<typename ITEM>
147
    IndexType ItemVector<ITEM>::size(bool active = true) const
148
    inline IndexType ItemVector<ITEM>::size(bool active = true) const
148
    { return active ? size_active : items.size(); }
149
    { return active ? size_active : items.size(); }
149
 
150
 
150
    template<typename ITEM>
151
    template<typename ITEM>
151
    void ItemVector<ITEM>::resize(IndexType _size, ITEM i = ITEM())
152
    inline void ItemVector<ITEM>::resize(IndexType _size, ITEM i)
152
    {
153
    {
153
        items.resize(_size, i);
154
        items.resize(_size, i);
154
        active_items.resize(_size, true);
155
        active_items.resize(_size, true);
155
        size_active = _size;
156
        size_active = _size;
156
    }
157
    }
157
 
158
 
158
    template<typename ITEM>
159
    template<typename ITEM>
159
    void ItemVector<ITEM>::reserve(IndexType i)
160
    inline void ItemVector<ITEM>::reserve(IndexType i)
160
    {
161
    {
161
        items.reserve(i);
162
        items.reserve(i);
162
        active_items.reserve(i);
163
        active_items.reserve(i);
163
    }
164
    }
164
 
165
 
165
    template<typename ITEM>
166
    template<typename ITEM>
166
    void ItemVector<ITEM>::clear()
167
    inline void ItemVector<ITEM>::clear()
167
    {
168
    {
168
        items.clear();
169
        items.clear();
169
        active_items.clear();
170
        active_items.clear();
170
        size_active = 0;
171
        size_active = 0;
171
    }
172
    }
172
 
173
 
173
    template<typename ITEM>
174
    template<typename ITEM>
174
    bool ItemVector<ITEM>::in_use(IndexType i) const
175
    inline bool ItemVector<ITEM>::in_use(IndexType i) const
175
    {
176
    {
176
        assert(i < active_items.size());
177
        //td::assert(i < active_items.size());
177
        return active_items[i];
178
        return active_items[i];
178
    }
179
    }
179
 
180
 
180
    template<typename ITEM>
181
    template<typename ITEM>
181
    IndexType ItemVector<ITEM>::index_begin(bool skip = true) const
182
    inline IndexType ItemVector<ITEM>::index_begin(bool skip = true) const
182
    {
183
    {
183
        IndexType i = 0;
184
        IndexType i = 0;
184
 
185
 
185
        if(!skip)
186
        if(!skip)
186
            return i;
187
            return i;
Line 190... Line 191...
190
 
191
 
191
        return i;
192
        return i;
192
    }
193
    }
193
 
194
 
194
    template<typename ITEM>
195
    template<typename ITEM>
195
    IndexType ItemVector<ITEM>::index_end() const
196
    inline IndexType ItemVector<ITEM>::index_end() const
196
    { return items.size(); }
197
    { return items.size(); }
197
 
198
 
198
    template<typename ITEM>
199
    template<typename ITEM>
199
    IndexType ItemVector<ITEM>::index_next(IndexType index, bool skip = true) const
200
    inline IndexType ItemVector<ITEM>::index_next(IndexType index, bool skip = true) const
200
    {
201
    {
201
        if(index < items.size())
202
        if(index < items.size())
202
            ++index;
203
            ++index;
203
 
204
 
204
        if(!skip)
205
        if(!skip)
Line 209... Line 210...
209
 
210
 
210
        return index;
211
        return index;
211
    }
212
    }
212
 
213
 
213
    template<typename ITEM>
214
    template<typename ITEM>
214
    IndexType ItemVector<ITEM>::index_prev(IndexType index, bool skip = true) const
215
    inline IndexType ItemVector<ITEM>::index_prev(IndexType index, bool skip = true) const
215
    {
216
    {
216
        if(index > 0)
217
        if(index > 0)
217
            --index;
218
            --index;
218
 
219
 
219
        if(!skip)
220
        if(!skip)