Subversion Repositories gelsvn

Rev

Rev 511 | Rev 513 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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