TBCI Numerical high perf. C++ Library 2.8.0
index.h
Go to the documentation of this file.
1
5/*
6 * AMB,
7 * KG, splitted from tensor.h, 99/05/31
8 * $Id: index.h,v 1.4.2.16 2019/05/28 11:13:02 garloff Exp $
9 */
10
11#ifndef TBCI_INDEX_H
12#define TBCI_INDEX_H
13
14#include "tbci/vector.h"
15
16// Avoid -fguiding-decls
17#if !defined(NO_GD) && !defined(AUTO_DECL)
18# include "index_gd.h"
19#endif
20
22
23#ifndef TBCI_DISABLE_EXCEPT
24class IdxErr : public NumErr
25{
26 public:
28 : NumErr("Error in Index class") {}
29 IdxErr(const char* t, const long i = 0)
30 : NumErr(t, i) {}
31 IdxErr(const IdxErr& ie)
32 : NumErr(ie) {}
33 virtual ~IdxErr() throw() {}
34};
35#endif
36
39
40#if defined(PRAGMA_I) && defined (PRAGMA_I_IDX) && ! defined(_NO_IDX_PRAGMA)
41# pragma interface "index.h"
42#endif
43
48class Index : public Vector <unsigned>
49{
50
51protected:
52
53public:
54 Index () : Vector <unsigned> () {}
55 Index (const unsigned dim) : Vector<unsigned> (dim) {}
56 Index (const unsigned value, const unsigned dim) : Vector<unsigned> (value, dim) {}
57 Index (const Index & idx) : Vector<unsigned> (idx) {}
58 Index (const Vector <unsigned> & vec) : Vector<unsigned> (vec) {}
59 Index (const TVector <unsigned> & tv) : Vector<unsigned> (tv) {}
60 // variable argument number
61 Index (vararg va, ...) : Vector <unsigned> ((unsigned)va)
62 {
63 va_list vl;
64 va_start (vl, va);
65 for (unsigned i = 0; i < (unsigned)va; ++i)
66 (*this) (i) = va_arg (vl, unsigned);
67 va_end (vl);
68 }
69
70 void lin_read (vararg va, ...)
71 {
72 //unsigned num=this->size();
73 va_list vl;
74 va_start (vl, va);
75 for (unsigned i = 0; i < (unsigned)va; ++i)
76 (*this) (i) = va_arg (vl, unsigned);
77 va_end (vl);
78 }
79
80 //Iterators (++,-- syntax can not be used, as we need to know max)
81 //work by incr/decr the last index, and wrap around ...
82 //caller has to check if Index(0) is within the valid range
83 //Really essential as it allows to linearize data access
84 // without circumventing the () operator in CTensor<>
85
87 {
88 BCHK (dim != max.dim, IdxErr, Indexes of iterators must have same dim, max.dim, *this);
89 (*this)(dim-1)++;
90 for (unsigned r = dim-1; r; r--)
91 if ((*this)(r) >= max(r)) { (*this)(r-1)++; (*this)(r) = 0; }
92 return *this;
93 }
95 {
96 BCHK (dim != max.dim, IdxErr, Indices of iterators must have same dim, max.dim, *this);
97 (*this)(dim-1)--;
98 for (unsigned r = dim-1; r; r--)
99 if ((signed)(*this)(r) < 0) { (*this)(r-1)--; (*this)(r) = max(r); }
100 return *this;
101 }
102
103 // Now a couple of functions to modify Index objects
104 //for friend TVector<unsigned> idx_fill_in1 FGD (const Index&, const unsigned, const unsigned);
105 TVector<unsigned> idx_fill_in1 (const unsigned, const unsigned) const;
106 //for friend TVector<unsigned> idx_fill_in2 FGD (const Index&, const unsigned,
107 // const unsigned, const unsigned, const unsigned);
108 TVector<unsigned> idx_fill_in2 (const unsigned, const unsigned,
109 const unsigned, const unsigned) const;
110
111 //for friend TVector<unsigned> idx_remove1 FGD (const Index&, const unsigned);
112 TVector<unsigned> idx_remove1 (const unsigned) const;
113 //for friend TVector<unsigned> idx_remove2 FGD (const Index&, const unsigned, const unsigned);
114 TVector<unsigned> idx_remove2 (const unsigned, const unsigned) const;
115 //for friend TVector<unsigned> idx_set1 FGD (const Index&, const unsigned, const unsigned);
116 TVector<unsigned> idx_set1 (const unsigned, const unsigned) const;
117};
118
119/* Implementation of Index modifying funcs */
120// Notes (KG, 98/12/09):
121// - We give back a TVector<unsigned> instead of Index (optimization)
122// - These functions should really be inlined. Otherwise the linker
123// will bite us because of multiple references, if we include this
124// header from more than one source file. (And it probably won't inline,
125// if we don't use optimization.)
126// Moving this class outside of a header would be an idea, as it's not
127// templated.
128
130Index::idx_fill_in1 (const unsigned where, const unsigned what) const
131{
132 TVector<unsigned> ix (size()+1);
133 BCHK (where >= ix.size(), IdxErr, Index nr. out of range, where, *this);
134 ix.set (what, where);
135 unsigned i = 0;
136 for (unsigned j = 0; j < size(); j++)
137 {
138 if (i == where) i++;
139 ix.set ((*this)(j), i++);
140 }
141 return ix;
142}
143
144INST(class Index friend TVector<unsigned> idx_fill_in1 (const Index&, const unsigned, const unsigned);)
146idx_fill_in1 (const Index& idx, const unsigned where, const unsigned what)
147{ return idx.idx_fill_in1 (where, what); }
148
150Index::idx_fill_in2 (const unsigned where1, const unsigned what1,
151 const unsigned where2, const unsigned what2) const
152{
153 TVector<unsigned> ix (size()+2);
154 BCHK (where1 >= ix.size(), IdxErr, Index nr. 1 out of range, where1, *this);
155 BCHK (where2 >= ix.size(), IdxErr, Index nr. 2 out of range, where2, *this);
156 ix.set (what1, where1); ix.set (what2, where2);
157 unsigned i = 0;
158 for (unsigned j = 0; j < size(); j++)
159 {
160 while (i == where1 || i == where2) i++;
161 ix.set ((*this)(j), i++);
162 }
163 return ix;
164}
165
166INST(class Index friend TVector<unsigned> idx_fill_in2 (const Index&, const unsigned, const unsigned, const unsigned, const unsigned);)
168idx_fill_in2 (const Index& idx, const unsigned where1, const unsigned what1,
169 const unsigned where2, const unsigned what2)
170{ return idx.idx_fill_in2 (where1, what1, where2, what2); }
171
172
174Index::idx_remove1 (const unsigned which) const
175{
176 BCHK (which >= size(), IdxErr, Tried to remove non-existing index, which, *this);
177 TVector<unsigned> ix (size()-1);
178 unsigned i = 0;
179 for (unsigned j = 0; j < size(); j++)
180 if (which != j) ix.set ((*this)(j), i++);
181 return ix;
182}
183
184INST(class Index friend TVector<unsigned> idx_remove1 (const Index&, const unsigned);)
186idx_remove1 (const Index& idx, const unsigned which)
187{ return idx.idx_remove1 (which); }
188
189
191Index::idx_remove2 (const unsigned which1, const unsigned which2) const
192{
193 BCHK (size() < 2, IdxErr, Cannot remove two indices from a Index with just one or zero, size(), *this);
194 BCHK (which1 >= size(), IdxErr, Tried to remove non-existing index1, which1, *this);
195 BCHK (which2 >= size(), IdxErr, Tried to remove non-existing index2, which2, *this);
196 TVector<unsigned> ix (size()-2);
197 unsigned i = 0;
198 for (unsigned j = 0; j < size(); j++)
199 if (which1 != j && which2 != j) ix.set ((*this)(j), i++);
200 return ix;
201}
202
203INST(class Index friend TVector<unsigned> idx_remove2 (const Index&, const unsigned, const unsigned);)
205idx_remove2 (const Index& idx, const unsigned which1, const unsigned which2)
206{ return idx.idx_remove2 (which1, which2); }
207
209Index::idx_set1 (const unsigned which, const unsigned what) const
210{
211 BCHK (which >= size(), IdxErr, Cannot set index (out of range), which, *this);
212 TVector<unsigned> ix (*this);
213 ix.set (what, which);
214 return ix;
215}
216
217INST(class Index friend TVector<unsigned> idx_set1 (const Index&, const unsigned, const unsigned);)
219idx_set1 (const Index& idx, const unsigned which, const unsigned what)
220{ return idx.idx_set1 (which, what); }
221
223
224#endif /* TBCI_INDEX_H */
long int Vector< T > & index
Definition LM_fit.h:69
int i
Definition LM_fit.h:71
#define BCHK(cond, exc, txt, ind, rtval)
Definition basics.h:575
#define NAMESPACE_END
Definition basics.h:323
#define INST(x)
Definition basics.h:238
#define NAMESPACE_TBCI
Definition basics.h:317
unsigned long dim
Definition bvector.h:74
unsigned * vec
Definition bvector.h:73
BVector< unsigned > & remove(const unsigned long)
Definition bvector.h:435
Definition index.h:25
virtual ~IdxErr()
Definition index.h:33
IdxErr()
Definition index.h:27
IdxErr(const IdxErr &ie)
Definition index.h:31
IdxErr(const char *t, const long i=0)
Definition index.h:29
Note that this pragma interface might create problems as the class index is not templated!
Definition index.h:49
Index(const Vector< unsigned > &vec)
Definition index.h:58
Index next_idx(const Index &max)
Definition index.h:86
Index(const unsigned value, const unsigned dim)
Definition index.h:56
Index(const TVector< unsigned > &tv)
Definition index.h:59
Index prev_idx(const Index &max)
Definition index.h:94
Index(const unsigned dim)
Definition index.h:55
Index(vararg va,...)
Definition index.h:61
TVector< unsigned > idx_set1(const unsigned, const unsigned) const
Definition index.h:209
TVector< unsigned > idx_fill_in1(const unsigned, const unsigned) const
Definition index.h:130
TVector< unsigned > idx_remove2(const unsigned, const unsigned) const
Definition index.h:191
Index(const Index &idx)
Definition index.h:57
TVector< unsigned > idx_remove1(const unsigned) const
Definition index.h:174
void lin_read(vararg va,...)
Definition index.h:70
Index()
Definition index.h:54
TVector< unsigned > idx_fill_in2(const unsigned, const unsigned, const unsigned, const unsigned) const
Definition index.h:150
NumErr()
Definition except.h:65
Temporary Base Class Idiom: Class TVector is used for temporary variables.
Definition vector.h:73
unsigned long size() const
Definition vector.h:104
T & set(const T &val, const unsigned long i) const
Definition vector.h:194
TVector(const unsigned long d=0)
Definition vector.h:84
unsigned max() const
Definition vector.h:2058
Vector(const unsigned long d=0)
Definition vector.h:1539
TVector< unsigned > idx_fill_in1(const Index &idx, const unsigned where, const unsigned what)
Definition index.h:146
TVector< unsigned > idx_fill_in2(const Index &idx, const unsigned where1, const unsigned what1, const unsigned where2, const unsigned what2)
Definition index.h:168
TVector< unsigned > idx_remove1(const Index &idx, const unsigned which)
Definition index.h:186
TVector< unsigned > idx_remove2(const Index &idx, const unsigned which1, const unsigned which2)
Definition index.h:205
TVector< unsigned > idx_set1(const Index &idx, const unsigned which, const unsigned what)
Definition index.h:219
const unsigned TMatrix< T > const Matrix< T > * a