Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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