-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathforcodec.h
More file actions
92 lines (79 loc) · 2.78 KB
/
Copy pathforcodec.h
File metadata and controls
92 lines (79 loc) · 2.78 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
87
88
89
90
91
92
/*
* Copyright (C) 2005-2015 Christoph Rupp (chris@crupp.de).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Frame of Reference encoding
*
* based on code from http://github.com/cruppstahl/for
*/
#ifndef INCLUDE_FOR_H
#define INCLUDE_FOR_H
#include "common.h"
#include "codecs.h"
#include "util.h"
#include "for.h"
namespace SIMDCompressionLib {
/*
* Optimized scalar implementation of FOR (frame-of-reference) compression
*/
class ForCODEC : public IntegerCODEC {
public:
void encodeArray(uint32_t *in, const size_t length, uint32_t *out,
size_t &nvalue) {
uint32_t cappedLength = static_cast<uint32_t>(
std::min<size_t>(length, std::numeric_limits<uint32_t>::max()));
*(uint32_t *)out = cappedLength;
out++;
// for_compress_sorted() would be a bit faster, but requires
// sorted input
nvalue = (4 + for_compress_unsorted((const uint32_t *)in, (uint8_t *)out,
cappedLength) +
3) /
4;
}
const uint32_t *decodeArray(const uint32_t *in, const size_t, uint32_t *out,
size_t &nvalue) {
nvalue = *in;
in++;
return in + for_uncompress((const uint8_t *)in, out,
static_cast<uint32_t>(nvalue));
}
// append a key.
// Returns the new size of the compressed array *in bytes*
size_t appendToByteArray(uint8_t *in, const size_t bytesize,
uint32_t /*previous_key*/, uint32_t key) {
return append(in, bytesize, key);
}
size_t append(uint8_t *in, const size_t /* unused */, uint32_t value) {
uint32_t length = *(uint32_t *)in;
size_t s = for_append_unsorted(in + 4, length, value);
*(uint32_t *)in = length + 1;
return s + 4;
}
size_t findLowerBound(const uint32_t *in, const size_t, uint32_t key,
uint32_t *presult) {
uint32_t length = *in;
in++;
return (size_t)for_lower_bound_search((const uint8_t *)in, length, key,
presult);
}
uint32_t select(const uint32_t *in, size_t index) {
in++; // Skip length
return for_select((const uint8_t *)in, static_cast<uint32_t>(index));
}
string name() const { return "For"; }
};
} /* namespace */
#endif /* INCLUDE_FOR_H */