Debian Codegraph @

lib/dat/key.hpp

raw symbols SCIP
1 /*
2 Copyright (C) 2011-2016 Brazil
3 Copyright (C) 2022 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #pragma once
21
22 #include "string.hpp"
23
24 namespace grn {
25 namespace dat {
26
27 class GRN_DAT_API Key {
28 public:
29 const UInt8 &operator[](UInt32 i) const {
30 GRN_DAT_DEBUG_THROW_IF(i >= length());
31 return buf_[i];
32 }
33
34 bool is_valid() const {
35 return id() != INVALID_KEY_ID;
36 }
37
38 String str() const {
39 return String(ptr(), length());
40 }
41
42 const void *ptr() const {
43 return buf_;
44 }
45 UInt32 length() const {
46 const UInt32 length = (length_high_ << 4) | (id_and_length_low_ & 0x0F);
47 // 4096 bytes key overflows to 0, so 0 is treated as 4096.
48 return (length == 0) ? MAX_KEY_LENGTH : length;
49 }
50 UInt32 id() const {
51 return id_and_length_low_ >> 4;
52 }
53
54 bool equals_to(const void *ptr, UInt32 length, UInt32 offset = 0) const {
55 if (length != this->length()) {
56 return false;
57 }
58 for ( ; offset < length; ++offset) {
59 if ((*this)[offset] != static_cast<const UInt8 *>(ptr)[offset]) {
60 return false;
61 }
62 }
63 return true;
64 }
65
66 // Creates an object of Key from given parameters. Then, the created object
67 // is embedded into a specified buffer.
68 static const Key &create(UInt32 *buf, UInt32 key_id,
69 const void *key_ptr, UInt32 key_length) {
70 GRN_DAT_DEBUG_THROW_IF(buf == NULL);
71 GRN_DAT_DEBUG_THROW_IF(key_id > MAX_KEY_ID);
72 GRN_DAT_DEBUG_THROW_IF((key_ptr == NULL) && (key_length != 0));
73 GRN_DAT_DEBUG_THROW_IF(key_length > MAX_KEY_LENGTH);
74
75 *buf = (key_id << 4) | (key_length & 0x0F);
76 UInt8 *ptr = reinterpret_cast<UInt8 *>(buf + 1);
77 *ptr++ = static_cast<UInt8>(key_length >> 4);
78 for (UInt32 i = 0; i < key_length; ++i) {
79 ptr[i] = static_cast<const UInt8 *>(key_ptr)[i];
80 }
81 return *reinterpret_cast<const Key *>(buf);
82 }
83
84 // Calculates how many UInt32s are required for a string. It is guaranteed
85 // that the estimated size is not less than the actual size.
86 static UInt32 estimate_size(UInt32 length) {
87 return static_cast<UInt32>(2 + (length / sizeof(UInt32)));
88 }
89
90 // Returns a reference to an invalid key.
91 static const Key &invalid_key() {
92 static const Key invalid_key;
93 return invalid_key;
94 // static const UInt32 invalid_key_buf[2] = { INVALID_KEY_ID << 4, 0 };
95 // return *reinterpret_cast<const Key *>(invalid_key_buf);
96 }
97
98 private:
99 UInt32 id_and_length_low_;
100 UInt8 length_high_;
101 UInt8 buf_[3];
102
103 // Disallows instantiation.
104 Key() : id_and_length_low_(INVALID_KEY_ID << 4), length_high_(0) {}
105 ~Key() {}
106
107 // Disallows copy and assignment.
108 Key(const Key &);
109 Key &operator=(const Key &);
110 };
111
112 } // namespace dat
113 } // namespace grn