Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
slice.h
Go to the documentation of this file.
1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 //
5 // Slice is a simple structure containing a pointer into some external
6 // storage and a size. The user of a Slice must ensure that the slice
7 // is not used after the corresponding external storage has been
8 // deallocated.
9 //
10 // Multiple threads can invoke const methods on a Slice without
11 // external synchronization, but if any of the threads may call a
12 // non-const method, all threads accessing the same Slice must use
13 // external synchronization.
14 
15 #ifndef STORAGE_LEVELDB_INCLUDE_SLICE_H_
16 #define STORAGE_LEVELDB_INCLUDE_SLICE_H_
17 
18 #include <assert.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include <string>
22 
23 namespace leveldb {
24 
25 class Slice {
26  public:
27  // Create an empty slice.
28  Slice() : data_(""), size_(0) { }
29 
30  // Create a slice that refers to d[0,n-1].
31  Slice(const char* d, size_t n) : data_(d), size_(n) { }
32 
33  // Create a slice that refers to the contents of "s"
34  Slice(const std::string& s) : data_(s.data()), size_(s.size()) { }
35 
36  // Create a slice that refers to s[0,strlen(s)-1]
37  Slice(const char* s) : data_(s), size_(strlen(s)) { }
38 
39  // Return a pointer to the beginning of the referenced data
40  const char* data() const { return data_; }
41 
42  // Return the length (in bytes) of the referenced data
43  size_t size() const { return size_; }
44 
45  // Return true iff the length of the referenced data is zero
46  bool empty() const { return size_ == 0; }
47 
48  // Return the ith byte in the referenced data.
49  // REQUIRES: n < size()
50  char operator[](size_t n) const {
51  assert(n < size());
52  return data_[n];
53  }
54 
55  // Change this slice to refer to an empty array
56  void clear() { data_ = ""; size_ = 0; }
57 
58  // Drop the first "n" bytes from this slice.
59  void remove_prefix(size_t n) {
60  assert(n <= size());
61  data_ += n;
62  size_ -= n;
63  }
64 
65  // Return a string that contains the copy of the referenced data.
66  std::string ToString() const { return std::string(data_, size_); }
67 
68  // Three-way comparison. Returns value:
69  // < 0 iff "*this" < "b",
70  // == 0 iff "*this" == "b",
71  // > 0 iff "*this" > "b"
72  int compare(const Slice& b) const;
73 
74  // Return true iff "x" is a prefix of "*this"
75  bool starts_with(const Slice& x) const {
76  return ((size_ >= x.size_) &&
77  (memcmp(data_, x.data_, x.size_) == 0));
78  }
79 
80  private:
81  const char* data_;
82  size_t size_;
83 
84  // Intentionally copyable
85 };
86 
87 inline bool operator==(const Slice& x, const Slice& y) {
88  return ((x.size() == y.size()) &&
89  (memcmp(x.data(), y.data(), x.size()) == 0));
90 }
91 
92 inline bool operator!=(const Slice& x, const Slice& y) {
93  return !(x == y);
94 }
95 
96 inline int Slice::compare(const Slice& b) const {
97  const int min_len = (size_ < b.size_) ? size_ : b.size_;
98  int r = memcmp(data_, b.data_, min_len);
99  if (r == 0) {
100  if (size_ < b.size_) r = -1;
101  else if (size_ > b.size_) r = +1;
102  }
103  return r;
104 }
105 
106 } // namespace leveldb
107 
108 
109 #endif // STORAGE_LEVELDB_INCLUDE_SLICE_H_
char operator[](size_t n) const
Definition: slice.h:50
bool starts_with(const Slice &x) const
Definition: slice.h:75
const char * data() const
Definition: slice.h:40
Slice(const std::string &s)
Definition: slice.h:34
const char * data_
Definition: slice.h:81
void clear()
Definition: slice.h:56
bool empty() const
Definition: slice.h:46
void remove_prefix(size_t n)
Definition: slice.h:59
bool operator!=(const Slice &x, const Slice &y)
Definition: slice.h:92
size_t size() const
Definition: slice.h:43
Slice(const char *d, size_t n)
Definition: slice.h:31
Slice(const char *s)
Definition: slice.h:37
int compare(const Slice &b) const
Definition: slice.h:96
bool operator==(const Slice &x, const Slice &y)
Definition: slice.h:87
size_t size_
Definition: slice.h:82
std::string ToString() const
Definition: slice.h:66