Subversion Repositories gelsvn

Rev

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

Rev 511 Rev 512
Line -... Line 1...
-
 
1
/* ----------------------------------------------------------------------- *
-
 
2
 * This file is part of GEL, www.imm.dtu.dk/GEL
-
 
3
 * Copyright (C) the authors (see AUTHORS.txt) and DTU Informatics
1
/*
4
 *
2
* Written by Christian Thode Larsen 2009-2010
5
 * Principal authors:
3
* Contact: thode2d@gmail.com
6
 *  Christian Thode Larsen (thode2d@gmail.com)
4
* Based on original work by J. Andreas Baerentzen
-
 
5
* Inspired by OpenMesh (www.openmesh.org)
7
 *  J. Andreas Baerentzen (jab@imm.dtu.dk)
6
*/
8
 *
-
 
9
 * See LICENSE.txt for licensing information
-
 
10
 * ----------------------------------------------------------------------- */
7
 
11
 
8
#ifndef __HMESH_ITEMVECTOR_H__
12
#ifndef __HMESH_ITEMVECTOR_H__
9
#define __HMESH_ITEMVECTOR_H__
13
#define __HMESH_ITEMVECTOR_H__
10
 
14
 
11
#include <vector>
15
#include <vector>
12
 
16
 
13
#include "ItemID.h"
17
#include "ItemID.h"
14
 
18
 
15
namespace HMesh
19
namespace HMesh
16
{
20
{
17
	template<typename ITEM>
21
    template<typename ITEM>
18
	class ItemVector
22
    class ItemVector
19
	{
23
    {
20
	public:
24
    public:
21
		/// default constructor
25
        /// default constructor
22
		ItemVector();
26
        ItemVector();
23
		ItemVector(IndexType _size, ITEM i = ITEM()); 
27
        ItemVector(IndexType _size, ITEM i = ITEM()); 
24
 
28
 
25
		/// Get a reference to entity i from kernel
29
        /// Get a reference to entity i from kernel
26
		ITEM& get(IndexType i);
30
        ITEM& get(IndexType i);
27
		/// Get a const reference to entity i from kernel
31
        /// Get a const reference to entity i from kernel
28
		const ITEM& get(IndexType i) const;
32
        const ITEM& get(IndexType i) const;
29
 
33
 
30
		ITEM& operator[](IndexType i);
34
        ITEM& operator[](IndexType i);
31
		const ITEM& operator[](IndexType i) const;
35
        const ITEM& operator[](IndexType i) const;
32
 
36
 
33
		/// Add an entity to the kernel
37
        /// Add an entity to the kernel
34
		IndexType add(const ITEM& i);
38
        IndexType add(const ITEM& i);
35
 
39
 
36
		/// remove an entity from kernel - entity is NOT erased!
40
        /// remove an entity from kernel - entity is NOT erased!
37
		void remove(IndexType i);
41
        void remove(IndexType i);
38
 
42
 
39
		/// erase unused entities from the kernel
43
        /// erase unused entities from the kernel
40
		void cleanup();
44
        void cleanup();
41
 
45
 
42
		/// size of vector (default: only active items)
46
        /// size of vector (default: only active items)
43
		IndexType size(bool active = true) const;
47
        IndexType size(bool active = true) const;
44
 
48
 
45
		/// Resize the kernel NOTE: Sets all active flags to true
49
        /// Resize the kernel NOTE: Sets all active flags to true
46
		void resize(IndexType _size, ITEM i = ITEM());
50
        void resize(IndexType _size, ITEM i = ITEM());
47
		/// Request size change in kernel
51
        /// Request size change in kernel
48
		void reserve(IndexType i);
52
        void reserve(IndexType i);
49
 
53
 
50
		/// Clear the kernel
54
        /// Clear the kernel
51
		void clear();
55
        void clear();
52
 
56
 
53
		/// Check if entity i is used
57
        /// Check if entity i is used
54
		bool in_use(IndexType i) const;
58
        bool in_use(IndexType i) const;
55
 
59
 
56
		/// get starting index (default: skip to first active index)
60
        /// get starting index (default: skip to first active index)
57
		IndexType index_begin(bool skip = true) const;
61
        IndexType index_begin(bool skip = true) const;
58
 
62
 
59
		/// get ending index
63
        /// get ending index
60
		IndexType index_end() const;
64
        IndexType index_end() const;
61
 
65
 
62
		/// get the next index (default: skip to first active index)
66
        /// get the next index (default: skip to first active index)
63
		IndexType index_next(IndexType index, bool skip = true) const;
67
        IndexType index_next(IndexType index, bool skip = true) const;
64
 
68
 
65
		/// get the previous index (default: skip to first active index)
69
        /// get the previous index (default: skip to first active index)
66
		IndexType index_prev(IndexType index, bool skip = true) const;
70
        IndexType index_prev(IndexType index, bool skip = true) const;
67
 
71
 
68
	private:
72
    private:
69
 
73
 
70
		IndexType size_active;
74
        IndexType size_active;
71
		std::vector<ITEM> items;
75
        std::vector<ITEM> items;
72
 
76
 
73
		/// Memory consideration - objects flagged as unused should be remembered for future use (unless purged)
77
        /// Memory consideration - objects flagged as unused should be remembered for future use (unless purged)
74
		std::vector<bool> active_items;
78
        std::vector<bool> active_items;
75
	};
79
    };
76
 
80
 
77
	template<typename ITEM>
81
    template<typename ITEM>
78
	inline ItemVector<ITEM>::ItemVector() : size_active(0) {}
82
    inline ItemVector<ITEM>::ItemVector() : size_active(0) {}
79
 
83
 
80
	template<typename ITEM>
84
    template<typename ITEM>
81
	inline ItemVector<ITEM>::ItemVector(IndexType _size, ITEM i = ITEM()) : size_active(_size), items(_size, i){}
85
    inline ItemVector<ITEM>::ItemVector(IndexType _size, ITEM i = ITEM()) : size_active(_size), items(_size, i){}
82
 
86
 
83
	template<typename ITEM>
87
    template<typename ITEM>
84
	ITEM& ItemVector<ITEM>::get(IndexType i)
88
    ITEM& ItemVector<ITEM>::get(IndexType i)
85
	{
89
    {
86
		assert(i < items.size());
90
        assert(i < items.size());
87
		return items[i];
91
        return items[i];
88
	}
92
    }
89
 
93
 
90
	template<typename ITEM>
94
    template<typename ITEM>
91
	const ITEM& ItemVector<ITEM>::get(IndexType i) const
95
    const ITEM& ItemVector<ITEM>::get(IndexType i) const
92
	{
96
    {
93
		assert(i < items.size());
97
        assert(i < items.size());
94
		return items[i];
98
        return items[i];
95
	}
99
    }
96
 
100
 
97
	template<typename ITEM>
101
    template<typename ITEM>
98
	ITEM& ItemVector<ITEM>::operator [](IndexType i)
102
    ITEM& ItemVector<ITEM>::operator [](IndexType i)
99
	{
103
    {
100
		assert(i < items.size());
104
        assert(i < items.size());
101
		return items[i];
105
        return items[i];
102
	} 
106
    } 
103
 
107
 
104
	template<typename ITEM>
108
    template<typename ITEM>
105
	const ITEM& ItemVector<ITEM>::operator [](IndexType i) const
109
    const ITEM& ItemVector<ITEM>::operator [](IndexType i) const
106
	{
110
    {
107
		assert(i < items.size());
111
        assert(i < items.size());
108
		return items[i];
112
        return items[i];
109
	}
113
    }
110
 
114
 
111
	template<typename ITEM>
115
    template<typename ITEM>
112
	IndexType ItemVector<ITEM>::add(const ITEM& i)
116
    IndexType ItemVector<ITEM>::add(const ITEM& i)
113
	{
117
    {
114
		items.push_back(i);
118
        items.push_back(i);
115
		active_items.push_back(true);
119
        active_items.push_back(true);
116
		++size_active;
120
        ++size_active;
117
		return items.size() - 1;
121
        return items.size() - 1;
118
	}
122
    }
119
 
123
 
120
	template<typename ITEM>
124
    template<typename ITEM>
121
	void ItemVector<ITEM>::remove(IndexType i)
125
    void ItemVector<ITEM>::remove(IndexType i)
122
	{
126
    {
123
		if(active_items[i]) 
127
        if(active_items[i]) 
124
			--size_active;
128
            --size_active;
125
		active_items[i] = false;
129
        active_items[i] = false;
126
	}
130
    }
127
 
131
 
128
	template<typename ITEM>
132
    template<typename ITEM>
129
	void ItemVector<ITEM>::cleanup()
133
    void ItemVector<ITEM>::cleanup()
130
	{
134
    {
131
		std::vector<ITEM> new_items;
135
        std::vector<ITEM> new_items;
132
		for(IndexType i = 0; i < items.size(); ++i){
136
        for(IndexType i = 0; i < items.size(); ++i){
133
			if(active_items[i]) 
137
            if(active_items[i]) 
134
				new_items.push_back(items[i]);          
138
                new_items.push_back(items[i]);          
135
		}
139
        }
136
		items = new_items;
140
        items = new_items;
137
		active_items = std::vector<bool>(items.size(), true);
141
        active_items = std::vector<bool>(items.size(), true);
138
		size_active = items.size();
142
        size_active = items.size();
139
	}
143
    }
140
 
144
 
141
	template<typename ITEM>
145
    template<typename ITEM>
142
	IndexType ItemVector<ITEM>::size(bool active = true) const
146
    IndexType ItemVector<ITEM>::size(bool active = true) const
143
	{ return active ? size_active : items.size(); }
147
    { return active ? size_active : items.size(); }
144
 
148
 
145
	template<typename ITEM>
149
    template<typename ITEM>
146
	void ItemVector<ITEM>::resize(IndexType _size, ITEM i = ITEM())
150
    void ItemVector<ITEM>::resize(IndexType _size, ITEM i = ITEM())
147
	{
151
    {
148
		items.resize(_size, i);
152
        items.resize(_size, i);
149
		active_items.resize(_size, true);
153
        active_items.resize(_size, true);
150
		size_active = _size;
154
        size_active = _size;
151
	}
155
    }
152
 
156
 
153
	template<typename ITEM>
157
    template<typename ITEM>
154
	void ItemVector<ITEM>::reserve(IndexType i)
158
    void ItemVector<ITEM>::reserve(IndexType i)
155
	{
159
    {
156
		items.reserve(i);
160
        items.reserve(i);
157
		active_items.reserve(i);
161
        active_items.reserve(i);
158
	}
162
    }
159
 
163
 
160
	template<typename ITEM>
164
    template<typename ITEM>
161
	void ItemVector<ITEM>::clear()
165
    void ItemVector<ITEM>::clear()
162
	{
166
    {
163
		items.clear();
167
        items.clear();
164
		active_items.clear();
168
        active_items.clear();
165
		size_active = 0;
169
        size_active = 0;
166
	}
170
    }
167
 
171
 
168
	template<typename ITEM>
172
    template<typename ITEM>
169
	bool ItemVector<ITEM>::in_use(IndexType i) const
173
    bool ItemVector<ITEM>::in_use(IndexType i) const
170
	{
174
    {
171
		assert(i < active_items.size());
175
        assert(i < active_items.size());
172
		return active_items[i];
176
        return active_items[i];
173
	}
177
    }
174
 
178
 
175
	template<typename ITEM>
179
    template<typename ITEM>
176
	IndexType ItemVector<ITEM>::index_begin(bool skip = true) const
180
    IndexType ItemVector<ITEM>::index_begin(bool skip = true) const
177
	{
181
    {
178
		if(!skip)
182
        if(!skip)
179
			return 0;
183
            return 0;
180
		IndexType i = 0;
184
        IndexType i = 0;
181
		while(!active_items[i])
185
        while(!active_items[i])
182
			++i;
186
            ++i;
183
		return i;
187
        return i;
184
	}
188
    }
185
 
189
 
186
	template<typename ITEM>
190
    template<typename ITEM>
187
	IndexType ItemVector<ITEM>::index_end() const
191
    IndexType ItemVector<ITEM>::index_end() const
188
	{ return items.size(); }
192
    { return items.size(); }
189
 
193
 
190
	template<typename ITEM>
194
    template<typename ITEM>
191
	IndexType ItemVector<ITEM>::index_next(IndexType index, bool skip = true) const
195
    IndexType ItemVector<ITEM>::index_next(IndexType index, bool skip = true) const
192
	{
196
    {
193
		if(index != items.size())
197
        if(index != items.size())
194
			++index;
198
            ++index;
195
 
199
 
196
		if(skip){
200
        if(skip){
197
			while(!in_use(index) && index < items.size()){
201
            while(!in_use(index) && index < items.size()){
198
				++index;
202
                ++index;
199
			}
203
            }
200
		}
204
        }
201
		return index;
205
        return index;
202
	}
206
    }
203
 
207
 
204
	template<typename ITEM>
208
    template<typename ITEM>
205
	IndexType ItemVector<ITEM>::index_prev(IndexType index, bool skip = true) const
209
    IndexType ItemVector<ITEM>::index_prev(IndexType index, bool skip = true) const
206
	{
210
    {
207
		if(index > 0)
211
        if(index > 0)
208
			--index;
212
            --index;
209
 
213
 
210
		if(skip){
214
        if(skip){
211
			while(!in_use(index) && index > 0){
215
            while(!in_use(index) && index > 0){
212
				--index;
216
                --index;
213
			}
217
            }
214
		}
218
        }
215
		return index;
219
        return index;
216
	}
220
    }
217
 
221
 
218
}
222
}
219
 
223
 
220
#endif 
224
#endif