COMBINATORIAL_BLAS  1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VecIterator.h
Go to the documentation of this file.
1 #ifndef VEC_ITERATOR_H
2 #define VEC_ITERATOR_H
3 
4 #include "FullyDistVec.h"
5 #include "FullyDistSpVec.h"
6 
7 template <class IT, class NT>
9 {
10  public:
11  virtual ~VectorLocalIterator() {}
12 
13  virtual IT LocalToGlobal(IT loc_idx) const = 0;
14  virtual IT GlobalToLocal(IT gbl_idx) const = 0;
15 
16  virtual bool Next() = 0;
17  virtual bool NextTo(IT loc_idx) = 0;
18  virtual bool HasNext() = 0;
19  virtual IT GetLocIndex() const = 0;
20  virtual NT& GetValue() const = 0;
21 
22  virtual void Del() = 0;
23 
24  virtual void Set(const IT loc_idx, const NT& val) = 0;
25 };
26 
27 template <class IT, class NT>
29 {
30  protected:
33 
34  public:
36 
37  IT LocalToGlobal(IT loc_idx) const
38  {
39  return v.LengthUntil() + loc_idx;
40  }
41 
42  IT GlobalToLocal(IT gbl_idx) const
43  {
44  IT ret;
45  v.Owner(gbl_idx, ret);
46  return ret;
47  }
48 
49 
50  bool Next()
51  {
52  iter_idx++;
53  bool exists = ((unsigned)iter_idx < v.arr.size());
54  if (!exists)
55  iter_idx = -1;
56  return exists;
57  }
58 
59  bool NextTo(IT loc_idx)
60  {
61  iter_idx = loc_idx;
62  return iter_idx > 0 && (unsigned)iter_idx < v.arr.size();
63  }
64 
65  bool HasNext()
66  {
67  return iter_idx >= 0 && (unsigned)iter_idx < v.arr.size();
68  }
69 
70  IT GetLocIndex() const
71  {
72  if ((unsigned)iter_idx < v.arr.size())
73  return iter_idx;
74  else
75  return -1;
76  }
77 
78  NT& GetValue() const
79  {
80  return v.arr[iter_idx];
81  }
82 
83  void Del()
84  {
85  assert(false);
86  }
87 
88  void Set(const IT loc_idx, const NT& val)
89  {
90  v.arr[loc_idx] = val;
91  }
92 };
93 
94 template <class IT, class NT>
96 {
97  protected:
100 
101  public:
103  {
104  if (v.ind.size() == 0)
105  iter_idx = -1;
106  }
107 
108  IT LocalToGlobal(IT loc_idx) const
109  {
110  return v.LengthUntil() + loc_idx;
111  }
112 
113  IT GlobalToLocal(IT gbl_idx) const
114  {
115  IT ret;
116  v.Owner(gbl_idx, ret);
117  return ret;
118  }
119 
120  bool Next()
121  {
122  iter_idx++;
123  bool exists = ((unsigned)iter_idx < v.ind.size());
124  if (!exists)
125  iter_idx = -1;
126  return exists;
127  }
128 
129  bool NextTo(IT loc_idx)
130  {
131  typename vector<IT>::iterator iter = lower_bound(v.ind.begin()+iter_idx, v.ind.end(), loc_idx);
132  if(iter == v.ind.end()) // beyond limits, insert from back
133  {
134  iter_idx = -1;
135  return false;
136  }
137  else if (loc_idx < *iter) // not found, but almost
138  {
139  iter_idx = iter - v.ind.begin();
140  return false;
141  }
142  else // found
143  {
144  iter_idx = iter - v.ind.begin();
145  return true;
146  }
147  }
148 
149  bool HasNext()
150  {
151  return iter_idx >= 0 && (unsigned)iter_idx < v.ind.size();
152  }
153 
154  IT GetLocIndex() const
155  {
156  if (iter_idx < 0)
157  return -1;
158  else
159  return v.ind[iter_idx];
160  }
161 
162  NT& GetValue() const
163  {
164  return v.num[iter_idx];
165  }
166 
167  void Del()
168  {
169  v.ind.erase(v.ind.begin()+iter_idx);
170  v.num.erase(v.num.begin()+iter_idx);
171  if ((unsigned)iter_idx >= v.ind.size())
172  iter_idx = -1;
173  }
174 
175  void Set(const IT loc_idx, const NT& val)
176  {
177  // see if we're just replacing the current value
178  /*if (loc_idx >= 0 && loc_idx == v.ind[iter_idx])
179  {
180  v.num[iter_idx] = val;
181  return;
182  }*/
183 
184  // inserted elsewhere
185  // This is from FullyDistSpVec::SetElement():
186  typename vector<IT>::iterator iter = lower_bound(v.ind.begin(), v.ind.end(), loc_idx);
187  if(iter == v.ind.end()) // beyond limits, insert from back
188  {
189  v.ind.push_back(loc_idx);
190  v.num.push_back(val);
191  }
192  else if (loc_idx < *iter) // not found, insert in the middle
193  {
194  // the order of insertions is crucial
195  // if we first insert to ind, then ind.begin() is invalidated !
196  v.num.insert(v.num.begin() + (iter-v.ind.begin()), val);
197  v.ind.insert(iter, loc_idx);
198  }
199  else // found
200  {
201  *(v.num.begin() + (iter-v.ind.begin())) = val;
202  }
203  }
204 
205  void Append(const IT loc_idx, const NT& val)
206  {
207  v.ind.push_back(loc_idx);
208  v.num.push_back(val);
209  }
210 };
211 
212 #include "VecIterator.cpp"
213 #endif