7 #ifndef KUDU_UTIL_SLICE_H_
8 #define KUDU_UTIL_SLICE_H_
21 #ifdef KUDU_HEADERS_USE_RICH_SLICE
22 #include "kudu/gutil/strings/fastmem.h"
23 #include "kudu/gutil/strings/stringpiece.h"
24 #include "kudu/util/faststring.h"
26 #include "kudu/util/kudu_export.h"
50 Slice() : data_(reinterpret_cast<const uint8_t *>(
"")),
59 Slice(
const uint8_t* d,
size_t n) : data_(d), size_(n) { }
67 Slice(
const char* d,
size_t n) :
68 data_(reinterpret_cast<const uint8_t *>(d)),
76 data_(reinterpret_cast<const uint8_t *>(s.data())),
84 data_(reinterpret_cast<const uint8_t *>(s)),
87 #ifdef KUDU_HEADERS_USE_RICH_SLICE
88 Slice(
const faststring &s)
103 Slice(
const StringPiece& s)
104 : data_(reinterpret_cast<const uint8_t*>(s.data())),
110 const uint8_t*
data()
const {
return data_; }
116 size_t size()
const {
return size_; }
119 bool empty()
const {
return size_ == 0; }
133 data_ =
reinterpret_cast<const uint8_t *
>(
"");
170 Status check_size(
size_t expected_size)
const;
173 std::string ToString()
const;
181 std::string ToDebugString(
size_t max_len = 0)
const;
191 int compare(
const Slice& b)
const;
198 return ((size_ >= x.size_) &&
199 (MemEqual(data_, x.data_, x.size_)));
224 memcpy(d, data_, size_);
230 friend bool operator==(
const Slice& x,
const Slice& y);
232 static bool MemEqual(
const void* a,
const void* b,
size_t n) {
233 #ifdef KUDU_HEADERS_USE_RICH_SLICE
234 return strings::memeq(a, b, n);
236 return memcmp(a, b, n) == 0;
240 static int MemCompare(
const void* a,
const void* b,
size_t n) {
241 #ifdef KUDU_HEADERS_USE_RICH_SLICE
242 return strings::fastmemcmp_inlined(a, b, n);
244 return memcmp(a, b, n);
248 const uint8_t* data_;
273 inline bool operator!=(
const Slice& x,
const Slice& y) {
284 inline std::ostream& operator<<(std::ostream& o,
const Slice& s) {
285 return o << s.ToDebugString(16);
289 const int min_len = (size_ < b.size_) ? size_ : b.size_;
290 int r = MemCompare(data_, b.data_, min_len);
292 if (size_ < b.size_) r = -1;
293 else if (size_ > b.size_) r = +1;
313 template <
typename T>
316 typedef std::map<Slice, T, Slice::Comparator>
type;
321 #endif // KUDU_UTIL_SLICE_H_
A representation of an operation's outcome.
Definition: status.h:145
void relocate(uint8_t *d)
Definition: slice.h:222
Slice(const char *s)
Definition: slice.h:83
bool starts_with(const Slice &x) const
Definition: slice.h:197
std::map< Slice, T, Slice::Comparator > type
A handy typedef for the slice map with appropriate comparison operator.
Definition: slice.h:316
bool operator()(const Slice &a, const Slice &b) const
Definition: slice.h:211
void clear()
Change this slice to refer to an empty array.
Definition: slice.h:132
const uint8_t & operator[](size_t n) const
Definition: slice.h:126
Comparator struct, useful for ordered collections (like STL maps).
Definition: slice.h:203
void truncate(size_t n)
Definition: slice.h:161
STL map whose keys are Slices.
Definition: slice.h:314
A wrapper around externally allocated data.
Definition: slice.h:47
size_t size() const
Definition: slice.h:116
Slice(const uint8_t *d, size_t n)
Definition: slice.h:59
Slice(const char *d, size_t n)
Definition: slice.h:67
uint8_t * mutable_data()
Definition: slice.h:113
const uint8_t * data() const
Definition: slice.h:110
void remove_prefix(size_t n)
Definition: slice.h:146
Slice(const std::string &s)
Definition: slice.h:75
int compare(const Slice &b) const
Definition: slice.h:288
Slice()
Create an empty slice.
Definition: slice.h:50
bool empty() const
Definition: slice.h:119