File: identifiers.hpp

package info (click to toggle)
pgrouting 4.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,332 kB
  • sloc: cpp: 21,315; sql: 10,419; ansic: 9,795; perl: 1,142; sh: 919; javascript: 314; xml: 182; makefile: 29
file content (259 lines) | stat: -rw-r--r-- 6,701 bytes parent folder | download
1
2
3
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
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
253
254
255
256
257
258
259
/*PGR-GNU*****************************************************************
File: identifiers.hpp

Generated with Template by:
Copyright (c) 2016-2026 pgRouting developers
Mail: project@pgrouting.org

Function's developer:
Copyright (c) 2016 Rohith Reddy
Mail:

------

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 2 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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 ********************************************************************PGR-GNU*/

/*! @file */

#ifndef INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_
#define INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_
#pragma once

#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <iostream>
#include <stdexcept>

/* TODO(vicky)
 * compiler check that type T is a integral type
 */
namespace pgrouting {

template <typename T>
class Identifiers {
 public:
    typedef typename std::set<T>::iterator iterator;
    typedef typename std::set<T>::const_iterator const_iterator;

    /*!
    \brief define ids
     */
    void set_ids(const std::set<T> &ids) {
        m_ids = ids;
    }

    //! @name constructors
    //@{
    Identifiers() = default;
    explicit Identifiers(const std::set<T>& data) : m_ids(data) {
    }

    /* @brief initializes with {1 ~ number}
     *
     * @params [in] number
     */
    explicit Identifiers(const size_t number) {
        size_t i(0);
        std::generate_n(std::inserter(m_ids, m_ids.begin()),
                number,
                [&i](){ return i++; });
    }

    //@}

    //! @name set like operators
    //@{
    size_t size() const {return m_ids.size(); }
    inline bool empty() const {return m_ids.empty(); }
    inline T front() const {return *m_ids.begin();}
    const_iterator begin() const {return m_ids.begin();}
    const_iterator end() const {return m_ids.end();}
    inline void pop_front() {m_ids.erase(m_ids.begin());}
    inline void clear() {m_ids.clear();}
    iterator begin() {return m_ids.begin();}
    iterator end() {return m_ids.end();}
    //@}


 private:
    std::set<T> m_ids;

 public:
    //! \brief true ids() has element
    /*!
     * @param [in] other Identifier of type *T*
     */
    bool has(const T other) const {
        return (m_ids.find(other) != m_ids.end());
    }


    //! \brief true when both sets are equal
    /*!
     * @param [in] rhs set of identifiers to be compared
     */
    bool operator==(const Identifiers<T> &rhs) const {
        return std::equal(m_ids.begin(), m_ids.end(), rhs.m_ids.begin());
    }

    //! @name  set UNION
    /// @{

    /*! \brief set UNION set
     *
     * @param[in] lhs Identifiers
     * @param[in] rhs Identifiers
     */
    friend Identifiers<T> operator +(
            const Identifiers<T> &lhs,
            const Identifiers<T> &rhs) {
        Identifiers<T> union_ids(lhs);
        union_ids += rhs;
        return union_ids;
    }

    //! \brief compound set UNION set
    /*!
     * @param [in] other set of identifiers
     */
    Identifiers<T>& operator +=(
            const Identifiers<T> &other) {
        m_ids.insert(other.m_ids.begin(), other.m_ids.end());
        return *this;
    }
    //! \brief compound set UNION element
    /*!
     * @param [in] element of type *T*
     */
    Identifiers<T>& operator +=(const T &element) {
        m_ids.insert(element);
        return *this;
    }

    /// @}



    //! @name  set INTERSECTION
    /// @{

    /*! \brief set INTERSECTION
     *
     *
     * @param[in] lhs  Identifiers
     * @param[in] rhs  Identifiers
     */

    friend Identifiers<T> operator *(
                const Identifiers<T> &lhs,
                const Identifiers<T> &rhs) {
            std::set<T> result;
            std::set_intersection(
                    lhs.m_ids.begin(), lhs.m_ids.end(),
                    rhs.m_ids.begin(), rhs.m_ids.end(),
                    std::inserter(result, result.begin()));
            return Identifiers<T>(result);
        }

    //! \brief compound set INTERSECTION set
    /*!
     * @param [in] other is a set of identifiers of type *Identifiers<T>*
     */
    Identifiers<T>& operator *=(
            const Identifiers<T> &other) {
        *this = *this * other;
        return *this;
    }

    //! \brief compound set INTERSECTION element
    /*!
     * @param[in] element is an identifiers of type *T*
     */
    Identifiers<T>& operator *=(const T &element) {
        if (has(element)) {
            m_ids.clear();
            m_ids.insert(element);
        } else {
            m_ids.clear();
        }
        return *this;
    }

    /// @}


    //! @name  set DIFFERENCE
    /// @{

    /* \brief set DIFFERENCE set
     *
     * @param[in] lhs Identifiers
     * @param[in] rhs Identifiers
     */
    friend
        Identifiers<T> operator -(
                const Identifiers<T> &lhs,
                const Identifiers<T> &rhs) {
            std::set<T> result;
            std::set_difference(
                    lhs.m_ids.begin(), lhs.m_ids.end(),
                    rhs.m_ids.begin(), rhs.m_ids.end(),
                    std::inserter(result, result.begin()));
            return Identifiers<T>(result);
        }



    //! \brief compound set DIFFERENCE set
    /*!
      @param [in] other is a set of identifiers of type *Identifiers<T>*
      Replaces this set with the set difference between this set and other
      */
    Identifiers<T>& operator -=(const Identifiers<T> &other) {
        *this = *this - other;
        return *this;
    }

    //! \brief compound set DIFFERENCE element
    /*!
      @param[in] element to be removed
      */
    Identifiers<T>& operator -=(const T &element) {
            m_ids.erase(element);
        return *this;
    }

    /// @}

    //! \brief Prints the set of identifiers
    friend
        std::ostream&
        operator<<(std::ostream& os, const Identifiers<T>& identifiers) {
            os << "{";
            for (auto identifier : identifiers.m_ids) {
                os << identifier << ", ";
            }
            os << "}";
            return os;
        }
};

}  // namespace pgrouting

#endif  // INCLUDE_CPP_COMMON_IDENTIFIERS_HPP_