forked from arangodb/velocypack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexampleArrayHandling.cpp
52 lines (42 loc) · 1.46 KB
/
exampleArrayHandling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include "velocypack/vpack.h"
#include "velocypack/velocypack-exception-macros.h"
using namespace arangodb::velocypack;
int main(int, char*[]) {
VELOCYPACK_GLOBAL_EXCEPTION_TRY
// create an array with 10 number members
Builder b;
b(Value(ValueType::Array));
for (size_t i = 0; i < 10; ++i) {
b.add(Value(i));
}
b.close();
// a Slice is a lightweight accessor for a VPack value
Slice s(b.start());
// filter out all array values less than 5
Builder filtered =
Collection::filter(s, [](Slice const& current, ValueLength) {
if (current.getNumber<int>() < 5) {
// exclude
return false;
}
// include
return true;
});
// print filtered array
Slice f(filtered.slice());
std::cout << "Filtered length: " << f.length() << std::endl;
// now iterate over the (left over) array members
std::cout << "Iterating Array members:" << std::endl;
for (auto const& it : ArrayIterator(f)) {
std::cout << it << ", number value: " << it.getUInt() << std::endl;
}
// iterate again, this time using Collection::forEach
std::cout << "Iterating some Array members using forEach:" << std::endl;
Collection::forEach(f, [](Slice const& current, ValueLength index) {
std::cout << current << ", number value: " << current.getUInt()
<< std::endl;
return (index != 2); // stop after the 3rd element (indexes are 0-based)
});
VELOCYPACK_GLOBAL_EXCEPTION_CATCH
}