Subversion Repositories gelsvn

Rev

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

Rev 515 Rev 518
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 <cassert>
16
#include <vector>
16
#include <vector>
17
 
17
 
18
 
-
 
19
#include "ItemID.h"
-
 
20
 
-
 
21
namespace HMesh
18
namespace HMesh
22
{
19
{
23
    template<typename ITEM>
20
    template<typename ITEM>
24
    class ItemVector
21
    class ItemVector
25
    {
22
    {
26
    public:
23
    public:
27
        /// default constructor
24
        /// default constructor
28
        ItemVector(IndexType _size = 0, ITEM i = ITEM()); 
25
        ItemVector(size_t _size = 0, ITEM i = ITEM()); 
29
 
26
        
30
        /// Get a reference to entity i from kernel
27
        /// Get a reference to entity i from kernel
31
        ITEM& get(IndexType i);
28
        ITEM& get(size_t i);
32
        /// Get a const reference to entity i from kernel
29
        /// Get a const reference to entity i from kernel
33
        const ITEM& get(IndexType i) const;
30
        const ITEM& get(size_t i) const;
34
 
31
 
35
        ITEM& operator[](IndexType i);
32
        ITEM& operator[](size_t i);
36
        const ITEM& operator[](IndexType i) const;
33
        const ITEM& operator[](size_t i) const;
37
 
34
 
38
        /// Add an entity to the kernel
35
        /// Add an entity to the kernel
39
        IndexType add(const ITEM& i);
36
        size_t add(const ITEM& i);
40
 
37
 
41
        /// remove an entity from kernel - entity is NOT erased!
38
        /// remove an entity from kernel - entity is NOT erased!
42
        void remove(IndexType i);
39
        void remove(size_t i);
43
 
40
 
44
        /// erase unused entities from the kernel
41
        /// erase unused entities from the kernel
45
        void cleanup();
42
        void cleanup();
46
 
43
 
47
        /// size of vector (default: only active items)
44
        /// size of vector (default: only active items)
48
        IndexType size(bool active = true) const;
45
        size_t size(bool active = true) const;
49
 
46
 
50
        /// Resize the kernel NOTE: Sets all active flags to true
47
        /// Resize the kernel NOTE: Sets all active flags to true
51
        void resize(IndexType _size, ITEM i = ITEM());
48
        void resize(size_t _size, ITEM i = ITEM());
52
        /// Request size change in kernel
49
        /// Request size change in kernel
53
        void reserve(IndexType i);
50
        void reserve(size_t i);
54
 
51
 
55
        /// Clear the kernel
52
        /// Clear the kernel
56
        void clear();
53
        void clear();
57
 
54
 
58
        /// Check if entity i is used
55
        /// Check if entity i is used
59
        bool in_use(IndexType i) const;
56
        bool in_use(size_t i) const;
60
 
57
 
61
        /// get starting index (default: skip to first active index)
58
        /// get starting index (default: skip to first active index)
62
        IndexType index_begin(bool skip = true) const;
59
        size_t index_begin(bool skip = true) const;
63
 
60
 
64
        /// get ending index
61
        /// get ending index
65
        IndexType index_end() const;
62
        size_t index_end() const;
66
 
63
 
67
        /// get the next index (default: skip to first active index)
64
        /// get the next index (default: skip to first active index)
68
        IndexType index_next(IndexType index, bool skip = true) const;
65
        size_t index_next(size_t index, bool skip = true) const;
69
 
66
 
70
        /// get the previous index (default: skip to first active index)
67
        /// get the previous index (default: skip to first active index)
71
        IndexType index_prev(IndexType index, bool skip = true) const;
68
        size_t index_prev(size_t index, bool skip = true) const;
72
 
69
 
73
    private:
70
    private:
74
 
71
 
75
        IndexType size_active;
72
        size_t size_active;
76
        std::vector<ITEM> items;
73
        std::vector<ITEM> items;
77
 
74
 
78
        /// Memory consideration - objects flagged as unused should be remembered for future use (unless purged)
75
        /// Memory consideration - objects flagged as unused should be remembered for future use (unless purged)
79
        std::vector<bool> active_items;
76
        std::vector<bool> active_items;
80
    };
77
    };
81
 
78
 
82
    template<typename ITEM>
79
    template<typename ITEM>
83
    inline ItemVector<ITEM>::ItemVector(IndexType _size, ITEM i) 
80
    inline ItemVector<ITEM>::ItemVector(size_t _size, ITEM i) 
84
        :   size_active(_size), 
81
        :   size_active(_size), 
85
            items(_size, i), 
82
            items(_size, i), 
86
            active_items(_size, true){}
83
            active_items(_size, true){}
87
 
84
 
88
    template<typename ITEM>
85
    template<typename ITEM>
89
    inline ITEM& ItemVector<ITEM>::get(IndexType i)
86
    inline ITEM& ItemVector<ITEM>::get(size_t i)
90
    {
87
    {
91
        //std::assert(i < items.size());
88
        assert(i < items.size());
92
        return items[i];
89
        return items[i];
93
    }
90
    }
94
 
91
 
95
    template<typename ITEM>
92
    template<typename ITEM>
96
    inline const ITEM& ItemVector<ITEM>::get(IndexType i) const
93
    inline const ITEM& ItemVector<ITEM>::get(size_t i) const
97
    {
94
    {
98
        //std::assert(i < items.size());
95
        assert(i < items.size());
99
        return items[i];
96
        return items[i];
100
    }
97
    }
101
 
98
 
102
    template<typename ITEM>
99
    template<typename ITEM>
103
    inline ITEM& ItemVector<ITEM>::operator [](IndexType i)
100
    inline ITEM& ItemVector<ITEM>::operator [](size_t i)
104
    {
101
    {
105
        //std::assert(i < items.size());
102
        assert(i < items.size());
106
        return items[i];
103
        return items[i];
107
    } 
104
    } 
108
 
105
 
109
    template<typename ITEM>
106
    template<typename ITEM>
110
    inline const ITEM& ItemVector<ITEM>::operator [](IndexType i) const
107
    inline const ITEM& ItemVector<ITEM>::operator [](size_t i) const
111
    {
108
    {
112
        //std::assert(i < items.size());
109
        assert(i < items.size());
113
        return items[i];
110
        return items[i];
114
    }
111
    }
115
 
112
 
116
    template<typename ITEM>
113
    template<typename ITEM>
117
    inline IndexType ItemVector<ITEM>::add(const ITEM& i)
114
    inline size_t ItemVector<ITEM>::add(const ITEM& i)
118
    {
115
    {
119
        items.push_back(i);
116
        items.push_back(i);
120
        active_items.push_back(true);
117
        active_items.push_back(true);
121
        ++size_active;
118
        ++size_active;
122
        return items.size() - 1;
119
        return items.size() - 1;
123
    }
120
    }
124
 
121
 
125
    template<typename ITEM>
122
    template<typename ITEM>
126
    inline void ItemVector<ITEM>::remove(IndexType i)
123
    inline void ItemVector<ITEM>::remove(size_t i)
127
    {
124
    {
128
        if(active_items[i]){ 
125
        if(active_items[i]){ 
129
            --size_active;
126
            --size_active;
130
            active_items[i] = false;
127
            active_items[i] = false;
131
        }
128
        }
Line 133... Line 130...
133
 
130
 
134
    template<typename ITEM>
131
    template<typename ITEM>
135
    inline void ItemVector<ITEM>::cleanup()
132
    inline void ItemVector<ITEM>::cleanup()
136
    {
133
    {
137
        std::vector<ITEM> new_items;
134
        std::vector<ITEM> new_items;
138
        for(IndexType i = 0; i < items.size(); ++i){
135
        for(size_t i = 0; i < items.size(); ++i){
139
            if(active_items[i]) 
136
            if(active_items[i]) 
140
                new_items.push_back(items[i]);          
137
                new_items.push_back(items[i]);          
141
        }
138
        }
142
        std::swap(items, new_items);
139
        std::swap(items, new_items);
143
        active_items = std::vector<bool>(items.size(), true);
140
        active_items = std::vector<bool>(items.size(), true);
144
        size_active = items.size();
141
        size_active = items.size();
145
    }
142
    }
146
 
143
 
147
    template<typename ITEM>
144
    template<typename ITEM>
148
    inline IndexType ItemVector<ITEM>::size(bool active = true) const
145
    inline size_t ItemVector<ITEM>::size(bool active = true) const
149
    { return active ? size_active : items.size(); }
146
    { return active ? size_active : items.size(); }
150
 
147
 
151
    template<typename ITEM>
148
    template<typename ITEM>
152
    inline void ItemVector<ITEM>::resize(IndexType _size, ITEM i)
149
    inline void ItemVector<ITEM>::resize(size_t _size, ITEM i)
153
    {
150
    {
154
        items.resize(_size, i);
151
        items.resize(_size, i);
155
        active_items.resize(_size, true);
152
        active_items.resize(_size, true);
156
        size_active = _size;
153
        size_active = _size;
157
    }
154
    }
158
 
155
 
159
    template<typename ITEM>
156
    template<typename ITEM>
160
    inline void ItemVector<ITEM>::reserve(IndexType i)
157
    inline void ItemVector<ITEM>::reserve(size_t i)
161
    {
158
    {
162
        items.reserve(i);
159
        items.reserve(i);
163
        active_items.reserve(i);
160
        active_items.reserve(i);
164
    }
161
    }
165
 
162
 
Line 170... Line 167...
170
        active_items.clear();
167
        active_items.clear();
171
        size_active = 0;
168
        size_active = 0;
172
    }
169
    }
173
 
170
 
174
    template<typename ITEM>
171
    template<typename ITEM>
175
    inline bool ItemVector<ITEM>::in_use(IndexType i) const
172
    inline bool ItemVector<ITEM>::in_use(size_t i) const
176
    {
173
    {
177
        //td::assert(i < active_items.size());
174
        assert(i < active_items.size());
178
        return active_items[i];
175
        return active_items[i];
179
    }
176
    }
180
 
177
 
181
    template<typename ITEM>
178
    template<typename ITEM>
182
    inline IndexType ItemVector<ITEM>::index_begin(bool skip = true) const
179
    inline size_t ItemVector<ITEM>::index_begin(bool skip = true) const
183
    {
180
    {
184
        IndexType i = 0;
181
        size_t i = 0;
185
 
182
 
186
        if(!skip)
183
        if(!skip)
187
            return i;
184
            return i;
188
        
185
        
189
        while(i < active_items.size() && !active_items[i])
186
        while(i < active_items.size() && !active_items[i])
Line 191... Line 188...
191
 
188
 
192
        return i;
189
        return i;
193
    }
190
    }
194
 
191
 
195
    template<typename ITEM>
192
    template<typename ITEM>
196
    inline IndexType ItemVector<ITEM>::index_end() const
193
    inline size_t ItemVector<ITEM>::index_end() const
197
    { return items.size(); }
194
    { return items.size(); }
198
 
195
 
199
    template<typename ITEM>
196
    template<typename ITEM>
200
    inline IndexType ItemVector<ITEM>::index_next(IndexType index, bool skip = true) const
197
    inline size_t ItemVector<ITEM>::index_next(size_t index, bool skip = true) const
201
    {
198
    {
202
        if(index < items.size())
199
        if(index < items.size())
203
            ++index;
200
            ++index;
204
 
201
 
205
        if(!skip)
202
        if(!skip)
Line 210... Line 207...
210
 
207
 
211
        return index;
208
        return index;
212
    }
209
    }
213
 
210
 
214
    template<typename ITEM>
211
    template<typename ITEM>
215
    inline IndexType ItemVector<ITEM>::index_prev(IndexType index, bool skip = true) const
212
    inline size_t ItemVector<ITEM>::index_prev(size_t index, bool skip = true) const
216
    {
213
    {
217
        if(index > 0)
214
        if(index > 0)
218
            --index;
215
            --index;
219
 
216
 
220
        if(!skip)
217
        if(!skip)