10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.3 KiB

  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. #ifndef STORAGE_LEVELDB_INCLUDE_SLICE_H_
  15. #define STORAGE_LEVELDB_INCLUDE_SLICE_H_
  16. #include <cassert>
  17. #include <cstddef>
  18. #include <cstring>
  19. #include <string>
  20. #include "leveldb/export.h"
  21. namespace leveldb {
  22. class LEVELDB_EXPORT Slice {
  23. public:
  24. // Create an empty slice.
  25. Slice() : data_(""), size_(0) {}
  26. // Create a slice that refers to d[0,n-1].
  27. Slice(const char* d, size_t n) : data_(d), size_(n) {}
  28. // Create a slice that refers to the contents of "s"
  29. Slice(const std::string& s) : data_(s.data()), size_(s.size()) {}
  30. // Create a slice that refers to s[0,strlen(s)-1]
  31. Slice(const char* s) : data_(s), size_(strlen(s)) {}
  32. // Intentionally copyable.
  33. Slice(const Slice&) = default;
  34. Slice& operator=(const Slice&) = default;
  35. // Return a pointer to the beginning of the referenced data
  36. const char* data() const { return data_; }
  37. // Return the length (in bytes) of the referenced data
  38. size_t size() const { return size_; }
  39. // Return true iff the length of the referenced data is zero
  40. bool empty() const { return size_ == 0; }
  41. const char* begin() const { return data(); }
  42. const char* end() const { return data() + size(); }
  43. // Return the ith byte in the referenced data.
  44. // REQUIRES: n < size()
  45. char operator[](size_t n) const {
  46. assert(n < size());
  47. return data_[n];
  48. }
  49. // Change this slice to refer to an empty array
  50. void clear() {
  51. data_ = "";
  52. size_ = 0;
  53. }
  54. // Drop the first "n" bytes from this slice.
  55. void remove_prefix(size_t n) {
  56. assert(n <= size());
  57. data_ += n;
  58. size_ -= n;
  59. }
  60. // Return a string that contains the copy of the referenced data.
  61. std::string ToString() const { return std::string(data_, size_); }
  62. // Three-way comparison. Returns value:
  63. // < 0 iff "*this" < "b",
  64. // == 0 iff "*this" == "b",
  65. // > 0 iff "*this" > "b"
  66. int compare(const Slice& b) const;
  67. // Return true iff "x" is a prefix of "*this"
  68. bool starts_with(const Slice& x) const {
  69. return ((size_ >= x.size_) && (memcmp(data_, x.data_, x.size_) == 0));
  70. }
  71. private:
  72. const char* data_;
  73. size_t size_;
  74. };
  75. inline bool operator==(const Slice& x, const Slice& y) {
  76. return ((x.size() == y.size()) &&
  77. (memcmp(x.data(), y.data(), x.size()) == 0));
  78. }
  79. inline bool operator!=(const Slice& x, const Slice& y) { return !(x == y); }
  80. inline int Slice::compare(const Slice& b) const {
  81. const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
  82. int r = memcmp(data_, b.data_, min_len);
  83. if (r == 0) {
  84. if (size_ < b.size_)
  85. r = -1;
  86. else if (size_ > b.size_)
  87. r = +1;
  88. }
  89. return r;
  90. }
  91. } // namespace leveldb
  92. #endif // STORAGE_LEVELDB_INCLUDE_SLICE_H_