CMR  1.3.0
binary_linear_space.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 
5 #include "permutations.hpp"
6 
7 namespace tu
8 {
9 
16  {
17  public:
18 
19  typedef std::vector <bool> vector_type;
20 
28  explicit binary_linear_space(size_t length)
29  {
30  _length = length;
31  _dimension = 0;
32  _vectors.push_back(vector_type(length, false));
33  }
34 
42  {
43  _length = other._length;
44  _dimension = other._dimension;
45  _vectors.clear();
46  _vectors.reserve(other.vectors());
47  std::copy(other._vectors.begin(), other._vectors.end(), std::back_inserter(_vectors));
48  }
49 
56  const vector_type& operator[](size_t index) const
57  {
58  return _vectors[index];
59  }
60 
65  inline size_t length() const
66  {
67  return _length;
68  }
69 
74  inline size_t vectors() const
75  {
76  return _vectors.size();
77  }
78 
83  inline size_t dimension() const
84  {
85  return _dimension;
86  }
87 
96  inline bool insert_checked(const vector_type& vector)
97  {
98  assert (vector.size() == length());
99 
100  if (is_spanned(vector))
101  return false;
102 
103  insert(vector);
104  return true;
105  }
106 
114  void insert(const vector_type& vector)
115  {
116  assert (vector.size() == length());
117 
118  size_t begin = 1 << _dimension;
119  ++_dimension;
120  size_t end = 1 << _dimension;
121  _vectors.resize(end, vector);
122  for (size_t current = begin + 1; current < end; current++)
123  vector_sum(vector, _vectors[current - begin], _vectors[current]);
124  }
125 
133  int find(const vector_type& vector) const
134  {
135  assert (vector.size() == length());
136 
137  for (size_t j = 0; j < _vectors.size(); ++j)
138  {
139  const vector_type& current_vector = _vectors[j];
140  bool same = true;
141  for (size_t i = 0; i < _length; ++i)
142  {
143  if (current_vector[i] ? !vector[i] : vector[i])
144  {
145  same = false;
146  break;
147  }
148  }
149  if (same)
150  return j;
151  }
152  return -1;
153  }
154 
162  inline bool is_spanned(const vector_type& vector) const
163  {
164  assert (vector.size() == length());
165 
166  return find(vector) >= 0;
167  }
168 
169  protected:
170 
180  static void vector_sum(const vector_type& first, const vector_type& second, vector_type& result)
181  {
182  assert (first.size() == second.size());
183  assert (first.size() == result.size());
184 
185  for (size_t i = 0; i < first.size(); ++i)
186  {
187  result[i] = first[i] ? !second[i] : second[i];
188  }
189  }
190 
191  private:
192  size_t _length;
193  size_t _dimension;
194  std::vector <vector_type> _vectors;
195  };
196 
205  inline std::ostream& operator<<(std::ostream& stream, const binary_linear_space& space)
206  {
207  for (size_t i = 0; i < space.vectors(); ++i)
208  {
209  for (size_t j = 0; j < space.length(); ++j)
210  {
211  stream << ' ' << (space[i][j] ? '1' : '0');
212  }
213  stream << '\n';
214  }
215  return stream;
216  }
217 
218 } /* namespace tu */
Definition: binary_linear_space.hpp:16
const vector_type & operator[](size_t index) const
Definition: binary_linear_space.hpp:56
std::vector< bool > vector_type
Definition: binary_linear_space.hpp:19
size_t length() const
Definition: binary_linear_space.hpp:65
size_t vectors() const
Definition: binary_linear_space.hpp:74
static void vector_sum(const vector_type &first, const vector_type &second, vector_type &result)
Definition: binary_linear_space.hpp:180
bool insert_checked(const vector_type &vector)
Definition: binary_linear_space.hpp:96
void insert(const vector_type &vector)
Definition: binary_linear_space.hpp:114
binary_linear_space(size_t length)
Definition: binary_linear_space.hpp:28
int find(const vector_type &vector) const
Definition: binary_linear_space.hpp:133
bool is_spanned(const vector_type &vector) const
Definition: binary_linear_space.hpp:162
size_t dimension() const
Definition: binary_linear_space.hpp:83
binary_linear_space(const binary_linear_space &other)
Definition: binary_linear_space.hpp:41
Definition: algorithm.hpp:14
std::ostream & operator<<(std::ostream &stream, const binary_linear_space &space)
Definition: binary_linear_space.hpp:205