10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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