Skip to content

Commit eb796c1

Browse files
author
Cris Cecka
committed
Solid iterators, range type maps, examples.
1 parent efdd722 commit eb796c1

2 files changed

Lines changed: 100 additions & 0 deletions

File tree

makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ CXXFLAGS := -std=c++11 -Wall
44
soa-iterator: soa-iterator.cc
55
$(CXX) $(CXXFLAGS) $< -o $@
66

7+
soa-range: soa-range.cc
8+
$(CXX) $(CXXFLAGS) $< -o $@
9+
710
.PHONY: run
811
run: soa-iterator
912
./$<

soa-range.cc

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
#include <thrust/iterator/zip_iterator.h>
6+
7+
// Printer helpers for library types
8+
9+
std::ostream& operator<<(std::ostream& s, const thrust::tuple<double,double>& p) {
10+
std::cout << "(" << thrust::get<0>(p) << ", " << thrust::get<1>(p) << ")";
11+
}
12+
13+
// define the container
14+
class PointBuffer {
15+
16+
public:
17+
18+
typedef thrust::tuple<double,double> value_type;
19+
typedef std::vector<double>::size_type size_type;
20+
21+
typedef typename std::vector<double>::iterator c_iterator;
22+
typedef thrust::tuple<c_iterator, c_iterator> iter_pair;
23+
typedef thrust::zip_iterator<iter_pair> iterator;
24+
25+
size_type size() const { return x.size(); }
26+
27+
void resize(size_type count) {
28+
x.resize(count);
29+
y.resize(count);
30+
}
31+
32+
iterator begin() { return iterator(iter_pair(x.begin(), y.begin())); }
33+
iterator end() { return iterator(iter_pair(x.end(), y.end() )); }
34+
35+
c_iterator x_begin() { return x.begin(); }
36+
c_iterator x_end() { return x.end(); }
37+
38+
c_iterator y_begin() { return y.begin(); }
39+
c_iterator y_end() { return y.end(); }
40+
41+
private:
42+
std::vector<double> x;
43+
std::vector<double> y;
44+
};
45+
46+
// Range access
47+
48+
struct xrange {
49+
xrange(PointBuffer& _pb) : pb(_pb) {}
50+
typename PointBuffer::c_iterator begin() { return pb.x_begin(); }
51+
typename PointBuffer::c_iterator end() { return pb.x_end(); }
52+
PointBuffer& pb;
53+
};
54+
55+
struct yrange {
56+
yrange(PointBuffer& _pb) : pb(_pb) {}
57+
typename PointBuffer::c_iterator begin() { return pb.y_begin(); }
58+
typename PointBuffer::c_iterator end() { return pb.y_end(); }
59+
PointBuffer& pb;
60+
};
61+
62+
// Printers
63+
64+
struct Printor {
65+
template <typename T>
66+
void operator()(const T& p) {
67+
std::cout << p << std::endl;
68+
}
69+
};
70+
71+
template <typename Range>
72+
void print(Range&& r) {
73+
for (const auto& i : r)
74+
std::cout << i << std::endl;
75+
}
76+
77+
int main() {
78+
PointBuffer p;
79+
p.resize(3);
80+
double c = 0.0;
81+
82+
for (auto& x : xrange(p))
83+
x = c++;
84+
for (auto& y : yrange(p))
85+
y = c++;
86+
87+
print(xrange(p));
88+
print(yrange(p));
89+
print(p);
90+
std::cout << std::endl;
91+
92+
std::for_each(p.begin(), p.end(), Printor());
93+
94+
std::cout << "--- end of soa-iterator ---" << std::endl;
95+
96+
return 0;
97+
}

0 commit comments

Comments
 (0)