fast_vector_property_map.hh 7.26 KB
Newer Older
1
2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2017 Tiago de Paula Peixoto <tiago@skewed.de>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

// Copyright (C) Vladimir Prus 2003.
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/graph/vector_property_map.html for
// documentation.
//

//
// This is a modification of boost's vector property map which optionally
// disables bound checking for better performance.
//

#ifndef FAST_VECTOR_PROPERTY_MAP_HH
#define FAST_VECTOR_PROPERTY_MAP_HH

35
36
37
38
39
40
#include <boost/version.hpp>
#if (BOOST_VERSION >= 104000)
#   include <boost/property_map/property_map.hpp>
#else
#   include <boost/property_map.hpp>
#endif
41
#include <memory>
42
43
44
45
46
#include <vector>

namespace boost {

template<typename T, typename IndexMap>
47
class unchecked_vector_property_map;
48
49

template<typename T, typename IndexMap = identity_property_map>
50
class checked_vector_property_map
51
52
53
    : public boost::put_get_helper<
              typename std::iterator_traits<
                  typename std::vector<T>::iterator >::reference,
54
              checked_vector_property_map<T, IndexMap> >
55
56
57
58
59
60
61
62
63
{
public:
    typedef typename property_traits<IndexMap>::key_type  key_type;
    typedef T value_type;
    typedef typename std::iterator_traits<
        typename std::vector<T>::iterator >::reference reference;
    typedef boost::lvalue_property_map_tag category;

    template<typename Type, typename Index>
64
    friend class unchecked_vector_property_map;
65

66
    typedef unchecked_vector_property_map<T, IndexMap> unchecked_t;
67
    typedef IndexMap index_map_t;
68
    typedef checked_vector_property_map<T,IndexMap> self_t;
69

70
    checked_vector_property_map(const IndexMap& idx = IndexMap())
71
        : store(std::make_shared<std::vector<T>>()), index(idx) {}
72

73
    checked_vector_property_map(unsigned initial_size,
74
                                const IndexMap& idx = IndexMap())
75
        : store(std::make_shared<std::vector<T>>(initial_size)), index(idx) {}
76

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    typename std::vector<T>::iterator storage_begin()
    {
        return store->begin();
    }

    typename std::vector<T>::iterator storage_end()
    {
        return store->end();
    }

    typename std::vector<T>::const_iterator storage_begin() const
    {
        return store->begin();
    }

    typename std::vector<T>::const_iterator storage_end() const
    {
        return store->end();
    }

    void reserve(size_t size) const
    {
99
        if (size > store->size())
100
101
102
            store->resize(size);
    }

103
104
105
106
107
108
109
110
111
112
    void resize(size_t size) const
    {
        store->resize(size);
    }

    void shrink_to_fit() const
    {
        store->shrink_to_fit();
    }

113
114
115
116
117
118
119
120
    std::vector<T>& get_storage() const { return (*store); }

    unchecked_t get_unchecked(size_t size = 0) const
    {
        reserve(size);
        return unchecked_t(*this, size);
    }

121
122
123
124
125
126
127
128
    // deep copy
    checked_vector_property_map copy() const
    {
        checked_vector_property_map pmap(index);
        *(pmap.store) = *store;
        return pmap;
    }

129
130
131
132
133
134
public:
    // Copy ctor absent, default semantics is OK.
    // Assignment operator absent, default semantics is OK.
    // CONSIDER: not sure that assignment to 'index' is correct.

    reference operator[](const key_type& v) const {
135
136
        auto i = get(index, v);
        if (static_cast<size_t>(i) >= store->size()) {
137
138
139
140
141
142
143
144
145
146
147
            store->resize(i + 1, T());
        }
        return (*store)[i];
    }
protected:
    // Conceptually, we have a vector of infinite size. For practical
    // purposes, we start with an empty vector and grow it as needed.
    // Note that we cannot store pointer to vector here -- we cannot
    // store pointer to data, because if copy of property map resizes
    // the vector, the pointer to data will be invalidated.
    // I wonder if class 'pmap_ref' is simply needed.
148
    std::shared_ptr< std::vector<T> > store;
149
150
151
152
    IndexMap index;
};

template<typename T, typename IndexMap = identity_property_map>
153
class unchecked_vector_property_map
154
155
156
    : public boost::put_get_helper<
                typename std::iterator_traits<
                    typename std::vector<T>::iterator >::reference,
157
                unchecked_vector_property_map<T, IndexMap> >
158
159
160
161
162
163
164
165
{
public:
    typedef typename property_traits<IndexMap>::key_type  key_type;
    typedef T value_type;
    typedef typename std::iterator_traits<
        typename std::vector<T>::iterator >::reference reference;
    typedef boost::lvalue_property_map_tag category;

166
167
168
169
170
171
172
173
174
    typedef checked_vector_property_map<T, IndexMap> checked_t;

    unchecked_vector_property_map(const checked_t& checked = checked_t(),
                                  size_t size = 0)
        : _checked(checked)
    {
        if (size > 0 && _checked.store->size() < size)
            _checked.store->resize(size);
    }
175

176
177
    unchecked_vector_property_map(const IndexMap& index_map,
                                  size_t size = 0)
178
    {
179
        *this = unchecked_vector_property_map(checked_t(index_map), size);
180
181
    }

182
    void reserve(size_t size) const { _checked.reserve(size); }
183
184
    void resize(size_t size) const { _checked.resize(size); }
    void shrink_to_fit() const { _checked.shrink_to_fit(); }
185

186
    reference operator[](const key_type& v) const
187
    {
188
        auto i = get(_checked.index, v);
189
        return (*_checked.store)[i];
190
191
    }

192
193
194
    std::vector<T>& get_storage() const { return _checked.get_storage(); }

    checked_t get_checked() {return _checked;}
195

196
197
198
199
200
201
202
203
204
    // deep copy
    unchecked_vector_property_map copy() const
    {
        unchecked_vector_property_map pmap(_checked.index,
                                           _checked.store->size());
        *(pmap._checked.store) = *(_checked.store);
        return pmap;
    }

205
206
private:
    checked_t _checked;
207
208
209
};

template<typename T, typename IndexMap>
210
211
212
213
214
215
216
217
218
checked_vector_property_map<T, IndexMap>
make_checked_vector_property_map(IndexMap index)
{
    return checked_vector_property_map<T, IndexMap>(index);
}

template<typename T, typename IndexMap>
unchecked_vector_property_map<T, IndexMap>
make_unchecked_vector_property_map(IndexMap index)
219
{
220
    return unchecked_vector_property_map<T, IndexMap>(index);
221
222
}

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

template <class Type, class Index>
unchecked_vector_property_map<Type, Index>
get_unchecked(checked_vector_property_map<Type, Index> prop)
{
    return prop.get_unchecked();
}

template <class Prop>
Prop
get_unchecked(Prop prop)
{
    return prop;
}

template <class Type, class Index>
checked_vector_property_map<Type, Index>
get_checked(unchecked_vector_property_map<Type, Index> prop)
{
    return prop.get_checked();
}

template <class Prop>
Prop
get_checked(Prop prop)
{
    return prop;
}


253
254
255
}

#endif