Debian Codegraph @

CorsixTH/Src/run_length_encoder.h

raw
1 /*
2 Copyright (c) 2010 Peter "Corsix" Cawley
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #ifndef CORSIX_TH_RLE_H_
24 #define CORSIX_TH_RLE_H_
25 #include "config.h"
26
27 #include <vector>
28
29 class lua_persist_reader;
30 class lua_persist_writer;
31
32 //! Encoder for reducing the amount of space to store a sequence of integers
33 /*!
34 Designed primarily for reducing the amount of space taken up by persisting
35 a THMap, this encoder transforms a sequence of integer records into a
36 sequence of pairs, each pair having an integer object, and a repeat count.
37 Terminology:
38 * Integer - An unsigned integer (up to) 32 bits wide.
39 * Record - One or more integers (for example, when encoding map nodes,
40 each record would be 6 integers if a map node had 6 fields).
41 * Object - One or more records. Each object has an associated repeat
42 count.
43 */
44 class integer_run_length_encoder {
45 public:
46 //! Initialise the encoder
47 /*!
48 Prepares the encoder for accepting a sequence of records.
49
50 \param iRecordSize The number of integers in a record.
51 */
52 explicit integer_run_length_encoder(size_t iRecordSize);
53
54 //! Supply the next integer in the input sequence to the encoder
55 void write(uint32_t iValue);
56
57 //! Inform the encoder that the input sequence has finished
58 /*!
59 finish() must be called prior to getOutput() or pumpOutput().
60 write() must not be called after finish() has been called.
61 */
62 void finish();
63
64 const uint32_t* get_output(size_t* pCount) const;
65 void pump_output(lua_persist_writer* pWriter) const;
66
67 private:
68 //! Reduce the amount of data in the buffer
69 /*!
70 \param bAll If true, will reduce buffer_size to zero.
71 If false, will reduce buffer_size by some amount.
72 */
73 void flush(bool bAll);
74
75 bool are_ranges_equal(size_t iObjIdx1, size_t iObjIdx2, size_t iOffset,
76 size_t iObjSize) const;
77 bool move_object_to_output(size_t iObjSize, size_t iObjCount);
78
79 //! A circular fixed-size buffer holding the most recent input
80 std::vector<uint32_t> buffer;
81 //! A variable-length array holding the output sequence
82 std::vector<uint32_t> output;
83 //! The number of integers in a record
84 size_t record_size;
85 //! The current number of integers stored in the buffer
86 size_t buffer_size{};
87 //! The index into buffer of the 1st integer
88 size_t buffer_offset{};
89 //! The current number of integers stored in the output
90 size_t output_size{};
91 //! The number of integers in the current object (multiple of record size)
92 size_t object_size{};
93 //! The number of copies of the current object already seen and removed
94 //! from the buffer.
95 size_t object_copies{};
96 };
97
98 //! Decoder for reading a sequence of integers encoded by
99 //! integer_run_length_encoder
100 class integer_run_length_decoder {
101 public:
102 integer_run_length_decoder(size_t iRecordSize, lua_persist_reader* pReader);
103
104 uint32_t read();
105 bool is_finished() const;
106
107 private:
108 std::vector<uint32_t> buffer;
109
110 // Not owned by the decoder, the reader to read from.
111 lua_persist_reader* reader;
112
113 size_t reads_remaining{};
114 size_t record_size;
115 size_t object_copies{};
116 size_t object_index{};
117 size_t object_size{};
118 };
119
120 #endif // CORSIX_TH_RLE_H_