7 #ifndef KUDU_UTIL_SLICE_H_
8 #define KUDU_UTIL_SLICE_H_
17 #ifdef KUDU_HEADERS_USE_RICH_SLICE
18 #include "kudu/gutil/strings/fastmem.h"
19 #include "kudu/gutil/strings/stringpiece.h"
20 #include "kudu/util/faststring.h"
22 #include "kudu/util/kudu_export.h"
46 Slice() : data_(reinterpret_cast<const uint8_t *>(
"")),
55 Slice(
const uint8_t* d,
size_t n) : data_(d), size_(n) { }
63 Slice(
const char* d,
size_t n) :
64 data_(reinterpret_cast<const uint8_t *>(d)),
72 data_(reinterpret_cast<const uint8_t *>(s.data())),
80 data_(reinterpret_cast<const uint8_t *>(s)),
83 #ifdef KUDU_HEADERS_USE_RICH_SLICE
84 Slice(
const faststring &s)
99 Slice(
const StringPiece& s)
100 : data_(reinterpret_cast<const uint8_t*>(s.data())),
106 const uint8_t*
data()
const {
return data_; }
112 size_t size()
const {
return size_; }
115 bool empty()
const {
return size_ == 0; }
129 data_ =
reinterpret_cast<const uint8_t *
>(
"");
166 Status check_size(
size_t expected_size)
const;
169 std::string ToString()
const;
177 std::string ToDebugString(
size_t max_len = 0)
const;
187 int compare(
const Slice& b)
const;
194 return ((size_ >= x.size_) &&
195 (MemEqual(data_, x.data_, x.size_)));
220 memcpy(d, data_, size_);
226 friend bool operator==(
const Slice& x,
const Slice& y);
228 static bool MemEqual(
const void* a,
const void* b,
size_t n) {
229 #ifdef KUDU_HEADERS_USE_RICH_SLICE
230 return strings::memeq(a, b, n);
232 return memcmp(a, b, n) == 0;
236 static int MemCompare(
const void* a,
const void* b,
size_t n) {
237 #ifdef KUDU_HEADERS_USE_RICH_SLICE
238 return strings::fastmemcmp_inlined(a, b, n);
240 return memcmp(a, b, n);
244 const uint8_t* data_;
269 inline bool operator!=(
const Slice& x,
const Slice& y) {
280 inline std::ostream& operator<<(std::ostream& o,
const Slice& s) {
281 return o << s.ToDebugString(16);
285 const int min_len = (size_ < b.size_) ? size_ : b.size_;
286 int r = MemCompare(data_, b.data_, min_len);
288 if (size_ < b.size_) r = -1;
289 else if (size_ > b.size_) r = +1;
309 template <
typename T>
312 typedef std::map<Slice, T, Slice::Comparator>
type;
317 #endif // KUDU_UTIL_SLICE_H_
A representation of an operation's outcome.
Definition: status.h:116
void relocate(uint8_t *d)
Definition: slice.h:218
Slice(const char *s)
Definition: slice.h:79
bool starts_with(const Slice &x) const
Definition: slice.h:193
std::map< Slice, T, Slice::Comparator > type
A handy typedef for the slice map with appropriate comparison operator.
Definition: slice.h:312
bool operator()(const Slice &a, const Slice &b) const
Definition: slice.h:207
void clear()
Change this slice to refer to an empty array.
Definition: slice.h:128
const uint8_t & operator[](size_t n) const
Definition: slice.h:122
Comparator struct, useful for ordered collections (like STL maps).
Definition: slice.h:199
void truncate(size_t n)
Definition: slice.h:157
STL map whose keys are Slices.
Definition: slice.h:310
A wrapper around externally allocated data.
Definition: slice.h:43
size_t size() const
Definition: slice.h:112
Slice(const uint8_t *d, size_t n)
Definition: slice.h:55
Slice(const char *d, size_t n)
Definition: slice.h:63
uint8_t * mutable_data()
Definition: slice.h:109
const uint8_t * data() const
Definition: slice.h:106
void remove_prefix(size_t n)
Definition: slice.h:142
Slice(const std::string &s)
Definition: slice.h:71
int compare(const Slice &b) const
Definition: slice.h:284
Slice()
Create an empty slice.
Definition: slice.h:46
bool empty() const
Definition: slice.h:115