Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
delta_stream.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/core/delta_stream.hpp
3  *
4  * Encode ascending values as deltas and deliver them to another stream.
5  * Contains both delta stream writer and stream reader.
6  *
7  * Part of Project Thrill - http://project-thrill.org
8  *
9  * Copyright (C) 2017 Timo Bingmann <[email protected]>
10  *
11  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
12  ******************************************************************************/
13 
14 #pragma once
15 #ifndef THRILL_CORE_DELTA_STREAM_HEADER
16 #define THRILL_CORE_DELTA_STREAM_HEADER
17 
19 
20 namespace thrill {
21 namespace core {
22 
23 /******************************************************************************/
24 // DeltaStreamWriter
25 
26 template <typename StreamWriter, typename Type, Type offset_ = Type()>
28 {
29 public:
31  StreamWriter& writer, const Type& initial = Type())
32  : writer_(writer), delta_(initial) { }
33 
34  void Put(const Type& value) {
35  assert(value >= delta_ + offset_);
36  writer_.Put(value - delta_ - offset_);
37  delta_ = value;
38  }
39 
40 private:
41  //! output writer
42  StreamWriter& writer_;
43 
44  //! delta for output
46 };
47 
48 /******************************************************************************/
49 // DeltaStreamReader
50 
51 template <typename StreamReader, typename Type, Type offset_ = Type()>
53 {
54 public:
56  StreamReader& reader, const Type& initial = Type())
57  : reader_(reader), delta_(initial) { }
58 
59  bool HasNext() {
60  return reader_.HasNext();
61  }
62 
63  template <typename Type2>
64  Type Next() {
65  static_assert(std::is_same<Type, Type2>::value, "Invalid Next() call");
66  Type value = reader_.template Next<Type>();
67  delta_ += value + offset_;
68  return delta_;
69  }
70 
71 private:
72  //! output reader
73  StreamReader& reader_;
74 
75  //! delta for output
77 };
78 
79 /******************************************************************************/
80 
81 } // namespace core
82 } // namespace thrill
83 
84 #endif // !THRILL_CORE_DELTA_STREAM_HEADER
85 
86 /******************************************************************************/
Type
VFS object type.
Definition: file_io.hpp:52
StreamReader & reader_
output reader
DeltaStreamReader(StreamReader &reader, const Type &initial=Type())
void Put(const Type &value)
int value
Definition: gen_data.py:41
Type delta_
delta for output
DeltaStreamWriter(StreamWriter &writer, const Type &initial=Type())
Type delta_
delta for output
StreamWriter & writer_
output writer