glucat  0.8.2
index_set.h
Go to the documentation of this file.
1 #ifndef _GLUCAT_INDEX_SET_H
2 #define _GLUCAT_INDEX_SET_H
3 /***************************************************************************
4  GluCat : Generic library of universal Clifford algebra templates
5  index_set.h : Declare a class for a set of non-zero integer indices
6  -------------------
7  begin : Sun 2001-12-09
8  copyright : (C) 2001-2012 by Paul C. Leopardi
9  ***************************************************************************
10 
11  This library is free software: you can redistribute it and/or modify
12  it under the terms of the GNU Lesser General Public License as published
13  by the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public License
22  along with this library. If not, see <http://www.gnu.org/licenses/>.
23 
24  ***************************************************************************
25  This library is based on a prototype written by Arvind Raja and was
26  licensed under the LGPL with permission of the author. See Arvind Raja,
27  "Object-oriented implementations of Clifford algebras in C++: a prototype",
28  in Ablamowicz, Lounesto and Parra (eds.)
29  "Clifford algebras with numeric and symbolic computations", Birkhauser, 1996.
30  ***************************************************************************
31  See also Arvind Raja's original header comments in glucat.h
32  ***************************************************************************/
33 
34 #include "glucat/global.h"
35 #include "glucat/errors.h"
36 
37 #include <boost/static_assert.hpp>
38 
39 #include <bitset>
40 #include <utility>
41 
42 namespace glucat
43 {
44  template<const index_t LO, const index_t HI>
45  class index_set; // forward
46 
48  template<const index_t LO, const index_t HI>
49  const index_set<LO,HI>
50  operator^ (const index_set<LO,HI>& lhs,
51  const index_set<LO,HI>& rhs);
52 
54  template<const index_t LO, const index_t HI>
55  const index_set<LO,HI>
56  operator& (const index_set<LO,HI>& lhs,
57  const index_set<LO,HI>& rhs);
58 
60  template<const index_t LO, const index_t HI>
61  const index_set<LO,HI>
62  operator| (const index_set<LO,HI>& lhs,
63  const index_set<LO,HI>& rhs);
64 
66  // -1 if a<b, +1 if a>b, 0 if a==b
67  template<const index_t LO, const index_t HI>
68  int
69  compare(const index_set<LO,HI>& a, const index_set<LO,HI>& b);
70 
72  template<const index_t LO, const index_t HI>
73  class index_set :
74  private std::bitset<HI-LO>
75  {
76  private:
77  BOOST_STATIC_ASSERT((LO <= 0) && (0 <= HI) && (LO < HI) && \
78  (-LO < _GLUCAT_BITS_PER_ULONG) && \
79  ( HI < _GLUCAT_BITS_PER_ULONG) && \
80  ( HI-LO <= _GLUCAT_BITS_PER_ULONG));
81  typedef std::bitset<HI-LO> bitset_t;
83  public:
85  typedef std::pair<index_t,index_t> index_pair_t;
86 
87  static const index_t v_lo = LO;
88  static const index_t v_hi = HI;
89 
90  static const std::string classname();
92  index_set () { }
94  index_set (const bitset_t bst);
96  index_set (const index_t idx);
98  index_set (const set_value_t folded_val, const index_set_t frm, const bool prechecked = false);
100  index_set (const index_pair_t& range, const bool prechecked = false);
102  index_set (const std::string& str);
103 
105  bool operator== (const index_set_t rhs) const;
107  bool operator!= (const index_set_t rhs) const;
109  index_set_t operator~ () const;
111  index_set_t& operator^= (const index_set_t rhs);
113  index_set_t& operator&= (const index_set_t rhs);
115  index_set_t& operator|= (const index_set_t rhs);
117  bool operator[] (const index_t idx) const;
119  bool test(const index_t idx) const;
121  index_set_t& set();
123  index_set_t& set(const index_t idx);
125  index_set_t& set(const index_t idx, const int val);
127  index_set_t& reset();
129  index_set_t& reset(const index_t idx);
131  index_set_t& flip();
133  index_set_t& flip(const index_t idx);
135  index_t count() const;
137  index_t count_neg() const;
139  index_t count_pos() const;
141  index_t min() const;
143  index_t max() const;
144 
145  // Functions which support Clifford algebra operations
147  bool operator< (const index_set_t rhs) const;
149  bool is_contiguous () const;
151  const index_set_t fold () const;
153  const index_set_t fold (const index_set_t frm, const bool prechecked = false) const;
155  const index_set_t unfold (const index_set_t frm, const bool prechecked = false) const;
157  set_value_t value_of_fold (const index_set_t frm) const;
159  int sign_of_mult (const index_set_t ist) const;
161  int sign_of_square() const;
162 
164  size_t hash_fn () const;
165 
166  // Friends
167  friend const index_set_t operator^<> (const index_set_t& lhs, const index_set_t& rhs);
168  friend const index_set_t operator&<> (const index_set_t& lhs, const index_set_t& rhs);
169  friend const index_set_t operator|<> (const index_set_t& lhs, const index_set_t& rhs);
170  friend int compare<> (const index_set_t& lhs, const index_set_t& rhs);
171 
172  // Member reference:
173  class reference;
174  friend class reference;
175 
177  class reference {
178  friend class index_set;
179 
181  reference();
182  public:
186  reference& operator= (const bool x);
188  reference& operator= (const reference& j);
190  bool operator~ () const;
192  operator bool () const;
194  reference& flip();
195 
196  private:
199  };
202  private:
204  bool lex_less_than (const index_set_t rhs) const;
205  };
206 
208  _GLUCAT_CTAssert(sizeof(set_value_t) >= sizeof(std::bitset<DEFAULT_HI-DEFAULT_LO>),
209  Default_index_set_too_big_for_value)
210 
211  // non-members
212 
213 
214  template<const index_t LO, const index_t HI>
215  std::ostream&
216  operator<< (std::ostream& os, const index_set<LO,HI>& ist);
217 
219  template<const index_t LO, const index_t HI>
220  std::istream&
221  operator>> (std::istream& s, index_set<LO,HI>& ist);
222 
223  // Functions which support Clifford algebra operations
225  int sign_of_square(index_t j);
226 
228  template<const index_t LO, const index_t HI>
229  index_t
230  min_neg(const index_set<LO,HI>& ist);
231 
233  template<const index_t LO, const index_t HI>
234  index_t
235  max_pos(const index_set<LO,HI>& ist);
236 }
237 #endif // _GLUCAT_INDEX_SET_H
reference & flip()
for b[i].flip();
index_t count_pos() const
Number of positive indices included in set.
int compare(const index_set< LO, HI > &a, const index_set< LO, HI > &b)
"lexicographic compare" eg. {3,4,5} is less than {3,7,8}
const index_set_t unfold(const index_set_t frm, const bool prechecked=false) const
Unfold this index set within the given frame.
static const index_t v_hi
Definition: index_set.h:88
int sign_of_square(index_t j)
Square of generator {j}.
bool operator[](const index_t idx) const
Subscripting: Test idx for membership: test value of bit idx.
bool operator~() const
Flips a bit.
index_set index_set_t
Definition: index_set.h:84
bool lex_less_than(const index_set_t rhs) const
Lexicographic ordering of two sets: *this < rhs.
bool operator==(const index_set_t rhs) const
Equality.
size_t hash_fn() const
Hash function.
error< index_set > error_t
Definition: index_set.h:82
_GLUCAT_CTAssert(std::numeric_limits< unsigned char >::radix==2, CannotDetermineBitsPerChar) const index_t BITS_PER_CHAR
If radix of unsigned char is not 2, we can&#39;t easily determine number of bits from sizeof...
index_t count() const
Cardinality: Number of indices included in set.
BOOST_STATIC_ASSERT((LO<=0) &&(0<=HI) &&(LO< HI) &&(-LO< _GLUCAT_BITS_PER_ULONG) &&(HI< _GLUCAT_BITS_PER_ULONG) &&(HI-LO<=_GLUCAT_BITS_PER_ULONG))
static const std::string classname()
Definition: index_set_imp.h:49
index_set_t & operator^=(const index_set_t rhs)
Symmetric set difference: exclusive or.
bool test(const index_t idx) const
Test idx for membership: test value of bit idx.
static const index_t v_lo
Definition: index_set.h:87
index_set_t & operator &=(const index_set_t rhs)
Set intersection: and.
reference & operator=(const bool x)
for b[i] = x;
const Multivector< Scalar_T, LO, HI > operator^(const Multivector< Scalar_T, LO, HI > &lhs, const RHS< Scalar_T, LO, HI > &rhs)
Outer product.
int sign_of_square() const
Sign of geometric square of a Clifford basis element.
int sign_of_mult(const index_set_t ist) const
Sign of geometric product of two Clifford basis elements.
index_t min() const
Minimum member.
index_t min_neg(const index_set< LO, HI > &ist)
Minimum negative index, or 0 if none.
reference()
Private default constructor is left undefined.
bool operator!=(const index_set_t rhs) const
Inequality.
index_set_t & operator|=(const index_set_t rhs)
Set union: or.
std::pair< index_t, index_t > index_pair_t
Definition: index_set.h:85
Specific exception class.
Definition: errors.h:57
bool is_contiguous() const
Determine if the index set is contiguous, ie. has no gaps.
index_set_t & flip()
Set complement, except 0: flip all bits, except 0.
std::bitset< HI-LO > bitset_t
Definition: index_set.h:81
bool operator<(const index_set_t rhs) const
Less than operator used for comparisons, map, etc.
index_set()
Default constructor creates an empty set.
Definition: index_set.h:92
Index set class based on std::bitset<> in Gnu standard C++ library.
Definition: index_set.h:45
Index set member reference.
Definition: index_set.h:177
const Multivector< Scalar_T, LO, HI > operator|(const Multivector< Scalar_T, LO, HI > &lhs, const RHS< Scalar_T, LO, HI > &rhs)
Transformation via twisted adjoint action.
index_t max() const
Maximum member.
index_t max_pos(const index_set< LO, HI > &ist)
Maximum positive index, or 0 if none.
index_set_t & reset()
Make set empty: Set all bits to 0.
set_value_t value_of_fold(const index_set_t frm) const
The set value of the fold of this index set within the given frame.
index_t count_neg() const
Number of negative indices included in set.
int index_t
Size of index_t should be enough to represent LO, HI.
Definition: global.h:77
unsigned long set_value_t
Size of set_value_t should be enough to contain index_set<LO,HI>
Definition: global.h:79
const Multivector< Scalar_T, LO, HI > operator &(const Multivector< Scalar_T, LO, HI > &lhs, const RHS< Scalar_T, LO, HI > &rhs)
Inner product.
std::istream & operator>>(std::istream &s, framed_multi< Scalar_T, LO, HI > &val)
Read multivector from input.
index_set_t operator~() const
Set complement: not.
const index_set_t fold() const
Fold this index set within itself as a frame.