1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.6 -------------------------------------------------*/
4 /* date: 6/15/2017 ---------------------------------------------*/
5 /* authors: Ariful Azad, Aydin Buluc  --------------------------*/
6 /****************************************************************/
7 /*
8  Copyright (c) 2010-2017, The Regents of the University of California
9 
10  Permission is hereby granted, free of charge, to any person obtaining a copy
11  of this software and associated documentation files (the "Software"), to deal
12  in the Software without restriction, including without limitation the rights
13  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the Software is
15  furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included in
18  all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  THE SOFTWARE.
27  */
28 
29 
30 #ifndef VEC_ITERATOR_H
31 #define VEC_ITERATOR_H
32 
33 #include "FullyDistVec.h"
34 #include "FullyDistSpVec.h"
35 
36 namespace combblas {
37 
38 template <class IT, class NT>
39 class VectorLocalIterator
40 {
41 	public:
~VectorLocalIterator()42 	virtual ~VectorLocalIterator() {}
43 
44 	virtual IT LocalToGlobal(IT loc_idx) const = 0;
45 	virtual IT GlobalToLocal(IT gbl_idx) const = 0;
46 
47 	virtual bool Next() = 0;
48 	virtual bool NextTo(IT loc_idx) = 0;
49 	virtual bool HasNext() = 0;
50 	virtual IT GetLocIndex() const = 0;
51 	virtual NT& GetValue() const = 0;
52 
53 	virtual void Del() = 0;
54 
55 	virtual void Set(const IT loc_idx, const NT& val) = 0;
56 };
57 
58 template <class IT, class NT>
59 class DenseVectorLocalIterator: public VectorLocalIterator<IT, NT>
60 {
61 	protected:
62 	FullyDistVec<IT, NT>& v;
63 	IT iter_idx;
64 
65 	public:
DenseVectorLocalIterator(FullyDistVec<IT,NT> & in_v)66 	DenseVectorLocalIterator(FullyDistVec<IT, NT>& in_v): v(in_v), iter_idx(0) {}
67 
LocalToGlobal(IT loc_idx)68 	IT LocalToGlobal(IT loc_idx) const
69 	{
70 		return v.LengthUntil() + loc_idx;
71 	}
72 
GlobalToLocal(IT gbl_idx)73 	IT GlobalToLocal(IT gbl_idx) const
74 	{
75 		IT ret;
76 		v.Owner(gbl_idx, ret);
77 		return ret;
78 	}
79 
80 
Next()81 	bool Next()
82 	{
83 		iter_idx++;
84 		bool exists = ((unsigned)iter_idx < v.arr.size());
85 		if (!exists)
86 			iter_idx = -1;
87 		return exists;
88 	}
89 
NextTo(IT loc_idx)90 	bool NextTo(IT loc_idx)
91 	{
92 		iter_idx = loc_idx;
93 		return iter_idx > 0 && (unsigned)iter_idx < v.arr.size();
94 	}
95 
HasNext()96 	bool HasNext()
97 	{
98 		return iter_idx >= 0 && (unsigned)iter_idx < v.arr.size();
99 	}
100 
GetLocIndex()101 	IT GetLocIndex() const
102 	{
103 		if ((unsigned)iter_idx < v.arr.size())
104 			return iter_idx;
105 		else
106 			return -1;
107 	}
108 
GetValue()109 	NT& GetValue() const
110 	{
111 		return v.arr[iter_idx];
112 	}
113 
Del()114 	void Del()
115 	{
116 		assert(false);
117 	}
118 
Set(const IT loc_idx,const NT & val)119 	void Set(const IT loc_idx, const NT& val)
120 	{
121 		v.arr[loc_idx] = val;
122 	}
123 };
124 
125 template <class IT, class NT>
126 class SparseVectorLocalIterator: public VectorLocalIterator<IT, NT>
127 {
128 	protected:
129 	FullyDistSpVec<IT, NT>& v;
130 	IT iter_idx;
131 
132 	public:
SparseVectorLocalIterator(FullyDistSpVec<IT,NT> & in_v)133 	SparseVectorLocalIterator(FullyDistSpVec<IT, NT>& in_v): v(in_v), iter_idx(0)
134 	{
135 		if (v.ind.size() == 0)
136 			iter_idx = -1;
137 	}
138 
LocalToGlobal(IT loc_idx)139 	IT LocalToGlobal(IT loc_idx) const
140 	{
141 		return v.LengthUntil() + loc_idx;
142 	}
143 
GlobalToLocal(IT gbl_idx)144 	IT GlobalToLocal(IT gbl_idx) const
145 	{
146 		IT ret;
147 		v.Owner(gbl_idx, ret);
148 		return ret;
149 	}
150 
Next()151 	bool Next()
152 	{
153 		iter_idx++;
154 		bool exists = ((unsigned)iter_idx < v.ind.size());
155 		if (!exists)
156 			iter_idx = -1;
157 		return exists;
158 	}
159 
NextTo(IT loc_idx)160 	bool NextTo(IT loc_idx)
161 	{
162 		typename std::vector<IT>::iterator iter = std::lower_bound(v.ind.begin()+iter_idx, v.ind.end(), loc_idx);
163 		if(iter == v.ind.end())	// beyond limits, insert from back
164 		{
165 			iter_idx = -1;
166 			return false;
167 		}
168 		else if (loc_idx < *iter)	// not found, but almost
169 		{
170 			iter_idx = iter - v.ind.begin();
171 			return false;
172 		}
173 		else // found
174 		{
175 			iter_idx = iter - v.ind.begin();
176 			return true;
177 		}
178 	}
179 
HasNext()180 	bool HasNext()
181 	{
182 		return iter_idx >= 0 && (unsigned)iter_idx < v.ind.size();
183 	}
184 
GetLocIndex()185 	IT GetLocIndex() const
186 	{
187 		if (iter_idx < 0)
188 			return -1;
189 		else
190 			return v.ind[iter_idx];
191 	}
192 
GetValue()193 	NT& GetValue() const
194 	{
195 		return v.num[iter_idx];
196 	}
197 
Del()198 	void Del()
199 	{
200 		v.ind.erase(v.ind.begin()+iter_idx);
201 		v.num.erase(v.num.begin()+iter_idx);
202 		if ((unsigned)iter_idx >= v.ind.size())
203 			iter_idx = -1;
204 	}
205 
Set(const IT loc_idx,const NT & val)206 	void Set(const IT loc_idx, const NT& val)
207 	{
208 		// see if we're just replacing the current value
209 		/*if (loc_idx >= 0 && loc_idx == v.ind[iter_idx])
210 		{
211 			v.num[iter_idx] = val;
212 			return;
213 		}*/
214 
215 		// inserted elsewhere
216 		// This is from FullyDistSpVec::SetElement():
217 		typename std::vector<IT>::iterator iter = std::lower_bound(v.ind.begin(), v.ind.end(), loc_idx);
218 		if(iter == v.ind.end())	// beyond limits, insert from back
219 		{
220 			v.ind.push_back(loc_idx);
221 			v.num.push_back(val);
222 		}
223 		else if (loc_idx < *iter)	// not found, insert in the middle
224 		{
225 			// the order of insertions is crucial
226 			// if we first insert to ind, then ind.begin() is invalidated !
227 			v.num.insert(v.num.begin() + (iter-v.ind.begin()), val);
228 			v.ind.insert(iter, loc_idx);
229 		}
230 		else // found
231 		{
232 			*(v.num.begin() + (iter-v.ind.begin())) = val;
233 		}
234 	}
235 
Append(const IT loc_idx,const NT & val)236 	void Append(const IT loc_idx, const NT& val)
237 	{
238 		v.ind.push_back(loc_idx);
239 		v.num.push_back(val);
240 	}
241 };
242 
243 #include "VecIterator.cpp"
244 
245 }
246 
247 #endif
248