Debian Codegraph @

Kernel/tFastSet.h

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2008-2015 Dmitry Tsarkov and The University of Manchester
3 Copyright (C) 2015-2017 Dmitry Tsarkov
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifndef TFASTSET_H
21 #define TFASTSET_H
22
23 #include <algorithm>
24 #include <vector>
25
26 /// the implementation of the set with constant-time "in" check
27 template <typename T>
28 class TFastSet
29 {
30 protected: // internal types
31 /// type for the array of values
32 typedef std::vector<T> ValArray;
33 /// type of the index
34 typedef size_t indexType;
35
36 public: // type interface
37 /// RW iterator on values
38 typedef typename ValArray::iterator iterator;
39 /// RO iterator on values
40 typedef typename ValArray::const_iterator const_iterator;
41
42 protected: // members
43 /// values to be kept in the set
44 ValArray Value;
45 /// indices of the used values
46 std::vector<indexType> Index;
47
48 protected: // methods
49 /// transform element into an integer; is tunable
50 static indexType toInt ( const T& t ) { return static_cast<indexType>(t); }
51
52 public: // interface
53 /// ensure that the index can cope with sets not larger that SIZE
54 void ensureMaxSetSize ( size_t size ) { Index.resize(size); }
55 /// empty c'tor
56 TFastSet ( void ) = default;
57 /// c'tor with a given max set SIZE
58 explicit TFastSet ( size_t size ) { ensureMaxSetSize(size); }
59 /// copy c'tor
60 TFastSet ( const TFastSet& ) = default;
61 /// move c'tor
62 TFastSet ( TFastSet&& ) = default;
63 /// assignment
64 TFastSet& operator = ( const TFastSet& ) = default;
65 /// move assignment
66 TFastSet& operator = ( TFastSet && ) = default;
67 /// reserve the set size to the SIZE elements
68 void reserve ( size_t size ) { Value.reserve(size); }
69
70 // set iterators
71
72 /// begin RW iterator
73 iterator begin ( void ) { return Value.begin(); }
74 /// end RW iterator
75 iterator end ( void ) { return Value.end(); }
76 /// begin RO iterator
77 const_iterator begin ( void ) const { return Value.begin(); }
78 /// end RO iterator
79 const_iterator end ( void ) const { return Value.end(); }
80
81 // set operations -- containment
82
83 /// check whether T is in set
84 bool in ( const T& t ) const
85 {
86 auto index = Index[toInt(t)];
87 return index < Value.size() && Value[index] == t;
88 }
89 /// check whether FS contains all the elements from the given set
90 bool operator <= ( const TFastSet& fs ) const
91 {
92 return std::all_of ( begin(), end(), [&] (const T& val) { return fs.in(val); } );
93 }
94 /// check whether given set contains all the elements from FS
95 bool operator >= ( const TFastSet& fs ) const { return fs <= *this; }
96 /// equality of the sets
97 bool operator == ( const TFastSet& fs ) const { return size() == fs.size() && (*this <= fs); }
98 /// strict containment
99 bool operator < ( const TFastSet& fs ) const { return size() < fs.size() && (*this <= fs); }
100 /// strict containment
101 bool operator > ( const TFastSet& fs ) const { return fs < *this; }
102
103 // set operations -- adding
104
105 /// add an element T to a set; assume T is not in the set (unique)
106 void addu ( const T& t )
107 {
108 Index[toInt(t)] = Value.size();
109 Value.emplace_back(t);
110 }
111 /// add element T to a set
112 void add ( const T& t ) { if ( !in(t) ) addu(t); }
113
114 // misc
115
116 /// check whether the set is empty
117 bool empty ( void ) const { return Value.empty(); }
118 /// get the size of the set
119 size_t size ( void ) const { return Value.size(); }
120 /// clear the set
121 void clear ( void ) { Value.clear(); }
122 /// set the size of the set to be a VALUE
123 void resize ( size_t value ) { Value.resize(value); }
124 }; // TFastSet
125
126 #endif