forked from heremaps/flatdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.cpp
More file actions
86 lines (75 loc) · 2.22 KB
/
fibonacci.cpp
File metadata and controls
86 lines (75 loc) · 2.22 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Copyright (c) 2017 HERE Europe B.V.
* See the LICENSE file in the root of this project for license details.
*/
/**
* A simple example, which serializes the first 30 fibonacci numbers, and deserializes them.
*/
#include "fibonacci.hpp" // generated by flatdata from fibonacci.flatdata
#include <flatdata/flatdata.h>
#include <iostream>
#include <string>
int
write( const char* folder )
{
auto storage = flatdata::FileResourceStorage::create( folder ); // create storage
auto builder = fib::FibonacciBuilder::open( std::move( storage ) ); // create builder
auto numbers = builder.start_numbers( ); // start writing numbers
uint64_t a = 1;
uint64_t b = 1;
for ( size_t n = 0; n < 30; ++n )
{
fib::NumberMutator number = numbers.grow( ); // get next element to write
number.value = a; // set data of the element
uint64_t c = a + b;
a = b;
b = c;
}
numbers.close( ); // flush not yet flushed data to disk
return 0;
}
int
read( const char* folder )
{
auto storage = flatdata::FileResourceStorage::create( folder ); // open storage
auto archive = fib::Fibonacci::open( std::move( storage ) ); // create archive
for ( fib::Number number : archive.numbers( ) ) // iterate through numbers
{
// Note: number is just a handle to memory. We can copy it without copying the data itself.
std::cout << number.value.as< uint64_t >( ) << " ";
}
std::cout << std::endl;
return 0;
}
static const char* USAGE = "USAGE: fibonacci <write|read> <folder>";
int
main( int argc, char const* argv[] )
{
if ( argc != 3 )
{
std::cerr << USAGE << std::endl;
return 1;
}
std::string verb( argv[ 1 ] );
try
{
if ( verb == "write" )
{
return write( argv[ 2 ] );
}
else if ( verb == "read" )
{
return read( argv[ 2 ] );
}
else
{
std::cerr << USAGE << std::endl;
return 1;
}
}
catch ( const std::runtime_error& err )
{
std::cerr << "Error: " << err.what( ) << std::endl;
}
return 0;
}