IndexSet.h
1 /* Copyright (C) 2012-2020 IBM Corp.
2  * This program is Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software
7  * distributed under the License is distributed on an "AS IS" BASIS,
8  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9  * See the License for the specific language governing permissions and
10  * limitations under the License. See accompanying LICENSE file.
11  */
12 #ifndef HELIB_INDEXSET_H
13 #define HELIB_INDEXSET_H
19 #include <helib/NumbTh.h>
20 
21 #include <helib/JsonWrapper.h>
22 
23 namespace helib {
24 
32 class IndexSet
33 {
34  std::vector<bool> rep;
35  // NOTE: modern versions of C++ are supposed
36  // to implement this efficiently as a "specialized template class".
37 
38  long _first, _last, _card;
39 
40  // Invariant: if _card == 0, then _first = 0, _last = -1;
41  // otherwise, _first (resp. _last) is the lowest (resp. highest)
42  // index in the set.
43  // In any case, the std::vector rep always defines the characteristic
44  // function of the set.
45 
46  // private helper function
47  void intervalConstructor(long low, long high);
48 
49 public:
50  /*** constructors ***/
51 
52  // @brief No-argument constructor, creates empty set
53  IndexSet() : _first(0), _last(-1), _card(0) {}
54 
55  // @brief Constructs an interval, low to high
56  IndexSet(long low, long high) { intervalConstructor(low, high); }
57 
58  // @brief Constructs a singleton set
59  explicit IndexSet(long j) { intervalConstructor(j, j); }
60 
61  // copy constructor: use the built-in copy constructor
62 
63  /*** assignment ***/
64 
65  // assignment: use the built-in assignment operator
66 
68  long first() const { return _first; }
69 
71  long last() const { return _last; }
72 
74  long next(long j) const;
75 
76  // @brief Returns the previous element before j, if any; otherwise j-1
77  long prev(long j) const;
78 
80  long card() const { return _card; }
81 
83  bool contains(long j) const;
84 
86  bool contains(const IndexSet& s) const;
87 
89  bool disjointFrom(const IndexSet& s) const;
90 
91  /*** comparison ***/
92 
93  bool operator==(const IndexSet& s) const;
94 
95  bool operator!=(const IndexSet& s) const { return !(*this == s); }
96 
97  /*** update methods ***/
98 
100  void clear();
101 
103  void insert(long j);
104 
106  void remove(long j);
107 
109  void insert(const IndexSet& s);
110 
112  void remove(const IndexSet& s);
113 
115  void retain(const IndexSet& s);
116 
118  static const IndexSet& emptySet();
119 
121  bool isInterval() const { return (_card == (1 + _last - _first)); }
122 
123  /*** raw IO ***/
124 
129  void writeTo(std::ostream& str) const;
130 
137  static IndexSet readFrom(std::istream& str);
138 
144  void writeToJSON(std::ostream& str) const;
145 
150  JsonWrapper writeToJSON() const;
151 
158  static IndexSet readFromJSON(std::istream& str);
159 
165  static IndexSet readFromJSON(const JsonWrapper& j);
166 
167  /*** code to allow one to write "for (long i: set)" ***/
168 
169  class iterator
170  {
171  friend class IndexSet;
172 
173  public:
174  long operator*() const { return i_; }
175 
177  {
178  i_ = s_.next(i_);
179  return *this;
180  }
181 
182  bool operator==(const iterator& other) const
183  {
184  return &s_ == &other.s_ && i_ == other.i_;
185  }
186 
187  bool operator!=(const iterator& other) const { return !(*this == other); }
188 
189  protected:
190  iterator(const IndexSet& s, long i) : s_(s), i_(i) {}
191 
192  private:
193  const IndexSet& s_;
194  long i_;
195  };
196 
197  iterator begin() const { return iterator(*this, this->first()); }
198  iterator end() const { return iterator(*this, this->last() + 1); }
199 };
200 
201 // some high-level convenience methods.
202 
204 IndexSet operator|(const IndexSet& s, const IndexSet& t);
205 
207 IndexSet operator&(const IndexSet& s, const IndexSet& t);
208 
210 IndexSet operator^(const IndexSet& s, const IndexSet& t);
211 
213 IndexSet operator/(const IndexSet& s, const IndexSet& t);
214 
215 // I/O operator
216 std::ostream& operator<<(std::ostream& str, const IndexSet& set);
217 std::istream& operator>>(std::istream& str, IndexSet& set);
218 
220 long card(const IndexSet& s);
221 
222 inline bool empty(const IndexSet& s) { return s.card() == 0; }
223 
225 bool operator<=(const IndexSet& s1, const IndexSet& s2);
226 
228 bool operator<(const IndexSet& s1, const IndexSet& s2);
229 
231 bool operator>=(const IndexSet& s1, const IndexSet& s2);
232 
234 bool operator>(const IndexSet& s1, const IndexSet& s2);
235 
237 inline bool disjoint(const IndexSet& s1, const IndexSet& s2)
238 {
239  return s1.disjointFrom(s2);
240 }
241 
242 } // namespace helib
243 
244 #endif // ifndef HELIB_INDEXSET_H
Definition: IndexSet.h:170
bool operator==(const iterator &other) const
Definition: IndexSet.h:182
bool operator!=(const iterator &other) const
Definition: IndexSet.h:187
long operator*() const
Definition: IndexSet.h:174
iterator & operator++()
Definition: IndexSet.h:176
iterator(const IndexSet &s, long i)
Definition: IndexSet.h:190
A dynamic set of non-negative integers.
Definition: IndexSet.h:33
long card() const
The cardinality of the set.
Definition: IndexSet.h:80
void writeTo(std::ostream &str) const
Write out the IndexSet object in binary format.
Definition: IndexSet.cpp:288
iterator end() const
Definition: IndexSet.h:198
long first() const
Returns the first element, 0 if the set is empty.
Definition: IndexSet.h:68
IndexSet()
Definition: IndexSet.h:53
iterator begin() const
Definition: IndexSet.h:197
long prev(long j) const
Definition: IndexSet.cpp:63
bool isInterval() const
Is this set a contiguous interval?
Definition: IndexSet.h:121
long next(long j) const
Returns the next element after j, if any; otherwise j+1.
Definition: IndexSet.cpp:49
bool operator!=(const IndexSet &s) const
Definition: IndexSet.h:95
static IndexSet readFrom(std::istream &str)
Read from the stream the serialized IndexSet object in binary format.
Definition: IndexSet.cpp:299
void remove(long j)
Remove j from the set.
Definition: IndexSet.cpp:155
IndexSet(long j)
Definition: IndexSet.h:59
long last() const
Returns the last element, -1 if the set is empty.
Definition: IndexSet.h:71
void clear()
Set to the empty set.
Definition: IndexSet.cpp:121
static const IndexSet & emptySet()
Read-only access to an empty set.
Definition: IndexSet.cpp:19
void insert(long j)
Add j to the set.
Definition: IndexSet.cpp:129
bool operator==(const IndexSet &s) const
Definition: IndexSet.cpp:104
bool contains(long j) const
Returns true iff the set contains j.
Definition: IndexSet.cpp:77
bool disjointFrom(const IndexSet &s) const
Returns true iff the set is disjoint from s.
Definition: IndexSet.cpp:92
JsonWrapper writeToJSON() const
Write out the IndexSet object to a JsonWrapper.
Definition: IndexSet.cpp:319
IndexSet(long low, long high)
Definition: IndexSet.h:56
void retain(const IndexSet &s)
Retains only those elements that are also in s (intersection)
Definition: IndexSet.cpp:215
static IndexSet readFromJSON(std::istream &str)
Read from the stream the serialized IndexSet object using JSON format.
Definition: IndexSet.cpp:332
Definition: apiAttributes.h:21
IndexSet operator/(const IndexSet &s, const IndexSet &t)
set minus
Definition: IndexSet.cpp:257
bool operator>=(const IndexSet &s1, const IndexSet &s2)
Is s2 subset or equal to s2.
Definition: IndexSet.cpp:278
IndexSet operator|(const IndexSet &s, const IndexSet &t)
union
Definition: IndexSet.cpp:233
std::istream & operator>>(std::istream &s, CtxtPart &p)
Definition: Ctxt.cpp:2762
bool disjoint(const IndexSet &s1, const IndexSet &s2)
Functional disjoint.
Definition: IndexSet.h:237
bool operator<=(const IndexSet &s1, const IndexSet &s2)
Is s1 subset or equal to s2.
Definition: IndexSet.cpp:268
long card(const IndexSet &s)
Functional cardinality.
Definition: IndexSet.cpp:265
bool operator>(const IndexSet &s1, const IndexSet &s2)
Is s2 strict subset of s1.
Definition: IndexSet.cpp:283
bool operator<(const IndexSet &s1, const IndexSet &s2)
Is s1 strict subset of s2.
Definition: IndexSet.cpp:273
std::ostream & operator<<(std::ostream &os, const ContextBuilder< SCHEME > &cb)
ostream operator for serializing the ContextBuilder object.
IndexSet operator&(const IndexSet &s, const IndexSet &t)
intersection
Definition: IndexSet.cpp:241
bool empty(const IndexSet &s)
Definition: IndexSet.h:222
IndexSet operator^(const IndexSet &s, const IndexSet &t)
exclusive-or
Definition: IndexSet.cpp:249
Definition: JsonWrapper.h:9