tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
indices.h
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program 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
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef TENSOR_INDICES_H
21 #define TENSOR_INDICES_H
22 
23 #include <list>
24 #include <vector>
25 #include <tensor/vector.h>
26 #include <tensor/gen.h>
27 
30 namespace tensor {
31 
32  extern template class Vector<index>;
33 
35  class Indices : public Vector<index> {
36  public:
37  Indices() : Vector<index>() {}
38  Indices(const Vector<index> &v) : Vector<index>(v) {}
39  template<size_t n> Indices(StaticVector<index,n> v) : Vector<index>(v) {}
40  explicit Indices(index size) : Vector<index>(size) {}
41 
42  static const Indices range(index min, index max, index step = 1);
43 
44  index total_size() const;
45  };
46 
47  void surrounding_dimensions(const Indices &d, index ndx, index *d1, index *d2, index *d3);
48 
49  const Indices operator<<(const Indices &a, const Indices &b);
50 
51  extern template class Vector<bool>;
52 
53 
55  class Booleans : public Vector<bool> {
56  public:
57  Booleans() : Vector<bool>() {}
58  Booleans(const Booleans &b) : Vector<bool>(b) {}
59  explicit Booleans(index size) : Vector<bool>(size) {}
60  };
61 
62  const Booleans operator!(const Booleans &b);
63  const Booleans operator&&(const Booleans &a, const Booleans &b);
64  const Booleans operator||(const Booleans &a, const Booleans &b);
65  const Indices which(const Booleans &b);
66 
67  bool all_of(const Booleans& b);
68  bool any_of(const Booleans& b);
69  inline bool none_of(const Booleans& b) {return !any_of(b);}
70 
71  bool all_equal(const Indices &a, const Indices &b);
72  inline bool some_unequal(const Indices &a, const Indices &b) { return !all_equal(a,b); }
73  const Booleans operator==(const Indices &a, const Indices &b);
74  const Booleans operator<(const Indices &a, const Indices &b);
75  const Booleans operator>(const Indices &a, const Indices &b);
76  const Booleans operator<=(const Indices &a, const Indices &b);
77  const Booleans operator>=(const Indices &a, const Indices &b);
78  const Booleans operator!=(const Indices &a, const Indices &b);
79 
80  const Booleans operator==(const Indices &a, index b);
81  const Booleans operator<(const Indices &a, index b);
82  const Booleans operator>(const Indices &a, index b);
83  const Booleans operator<=(const Indices &a, index b);
84  const Booleans operator>=(const Indices &a, index b);
85  const Booleans operator!=(const Indices &a, index b);
86 
87  inline const Booleans operator==(index a, const Indices &b) { return b == a; }
88  inline const Booleans operator<(index a, const Indices &b) { return b >= a; }
89  inline const Booleans operator>(index a, const Indices &b) { return b <= a; }
90  inline const Booleans operator<=(index a, const Indices &b) { return b > a; }
91  inline const Booleans operator>=(index a, const Indices &b) { return b < a; }
92  inline const Booleans operator!=(index a, const Indices &b) { return b != a; }
93 
95  // RANGE OF INTEGERS
96  //
97 
105  class Range {
106  public:
107  Range();
108  virtual ~Range();
109  virtual index pop();
110  virtual void set_factor(index new_factor);
111  virtual void set_limit(index new_limit);
112  virtual index size() const;
113  virtual void reset();
114  index nomore() const { return ~(index)0; }
115  index get_offset() const { return base_; }
116  index get_limit() const { return limit_; }
117  index get_factor() const { return factor_; }
118  void set_offset(index new_base) { base_ = new_base; }
119  private:
120  index base_, limit_, factor_;
121  };
122 
123  class FullRange : public Range {
124  public:
125  FullRange();
126  virtual index pop();
127  virtual void set_factor(index new_factor);
128  virtual void set_limit(index new_limit);
129  virtual index size() const;
130  virtual void reset();
131  private:
132  index counter_, counter_end_;
133  };
134 
135  class StepRange : public Range {
136  public:
137  StepRange(index start, index end, index step = 1);
138  virtual index pop();
139  virtual void set_factor(index new_factor);
140  virtual void set_limit(index new_limit);
141  virtual index size() const;
142  virtual void reset();
143  private:
144  index ndx_, start_, end_, step_;
145  };
146 
147  class SingleRange : public Range {
148  public:
149  SingleRange(index ndx);
150  virtual index pop();
151  virtual void set_factor(index new_factor);
152  virtual void set_limit(index new_limit);
153  virtual index size() const;
154  virtual void reset();
155  private:
156  index ndx_, counter_;
157  };
158 
159  class IndexRange : public Range {
160  public:
161  IndexRange(const Indices &i);
162  virtual index pop();
163  virtual void set_factor(index new_factor);
164  virtual void set_limit(index new_limit);
165  virtual index size() const;
166  virtual void reset();
167  private:
168  Indices indices_;
169  index counter_;
170  };
171 
172  class ProductRange : public Range {
173  public:
174  ProductRange(Range *r1, Range *r2);
175  ~ProductRange();
176  virtual index pop();
177  virtual void set_factor(index new_factor);
178  virtual void set_limit(index new_limit);
179  virtual index size() const;
180  virtual void reset();
181  private:
182  Range *r1_, *r2_;
183  index base_;
184  };
185 
186  class PRange {
187  public:
188  PRange(Range *r) : ptr_(r) {};
189  operator Range*() const { return ptr_; }
190  Range &operator*() { return *ptr_; }
191  Range *operator->() { return ptr_; }
192  private:
193  PRange();
194  Range * ptr_;
195  };
196 
198  inline PRange range(index ndx) { return new SingleRange(ndx); }
200  inline PRange range(index start, index end) { return new StepRange(start, end); }
202  inline PRange range(index start, index end, index step) { return new StepRange(start, end, step); }
204  inline PRange range(Indices i) { return new IndexRange(i); }
206  inline PRange range() { return new FullRange(); }
207 
209  inline Indices iota(index start, index end, index step = 1) {
210  return Indices::range(start, end, step);
211  }
212 
213  template<size_t n>
214  bool operator==(const tensor::Indices &v2,
215  const tensor::StaticVector<tensor::index,n> &v1)
216  {
217  tensor::Indices v0(v1);
218  if (v0.size() != v2.size()) return false;
219  return std::equal(v0.begin_const(), v0.end_const(), v2.begin_const());
220  }
221 
222  template<size_t n>
223  bool operator==(const tensor::StaticVector<tensor::index,n> &v1,
224  const tensor::Indices &v2)
225  {
226  tensor::Indices v0(v1);
227  if (v0.size() != v2.size()) return false;
228  return std::equal(v0.begin_const(), v0.end_const(), v2.begin_const());
229  }
230 
231 
232 }; // namespace
233 
235 #endif // !TENSOR_H
Vector of boolean values.
Definition: indices.h:55
Vector of 'index' type, where 'index' fits the indices of a tensor.
Definition: indices.h:35
Range of indices.
Definition: indices.h:105