Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium 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. | |
| 4 | |
| 5 #ifndef SYNC_INTERNAL_API_PUBLIC_BASE_ORDINAL_H_ | |
| 6 #define SYNC_INTERNAL_API_PUBLIC_BASE_ORDINAL_H_ | |
| 7 | |
| 8 #include <algorithm> | |
| 9 #include <cstddef> | |
| 10 #include <ostream> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/logging.h" | |
| 15 | |
| 16 namespace syncer { | |
| 17 | |
| 18 // A Ordinal represents an object convertible to and from a string | |
| 19 // that can be used for ordering. The Ordinal class has an unbounded | |
| 20 // dense strict total order, which mean for any Ordinals a, b and c: | |
| 21 // | |
| 22 // - a < b and b < c implies a < c (transitivity); | |
| 23 // - exactly one of a < b, b < a and a = b holds (trichotomy); | |
| 24 // - if a < b, there is a Ordinal x such that a < x < b (density); | |
| 25 // - there are Ordinals x and y such that x < a < y (unboundedness). | |
| 26 // | |
| 27 // This means that when Ordinal is used for sorting a list, if any | |
| 28 // item changes its position in the list, only its Ordinal value has | |
| 29 // to change to represent the new order, and all the other older | |
| 30 // values can stay the same. | |
| 31 // | |
| 32 // The Traits class should look like the following: | |
| 33 // | |
| 34 // // Don't forget to #include "base/basictypes.h". | |
| 35 // struct MyOrdinalTraits { | |
| 36 // // There must be at least two distinct values greater than kZeroDigit | |
| 37 // // and less than kMaxDigit. | |
| 38 // static const uint8 kZeroDigit = '0'; | |
| 39 // static const uint8 kMaxDigit = '9'; | |
| 40 // // kMinLength must be positive. | |
| 41 // static const size_t kMinLength = 1; | |
| 42 // }; | |
| 43 // | |
| 44 // An Ordinal is valid iff its corresponding string has at least | |
| 45 // kMinLength characters, does not contain any characters less than | |
| 46 // kZeroDigit or greater than kMaxDigit, is not all zero digits, and | |
| 47 // does not have any unnecessary trailing zero digits. | |
| 48 // | |
| 49 // Note that even if the native char type is signed, strings still | |
| 50 // compare as if their they are unsigned. (This is explicitly in | |
| 51 // C++11 but not in C++98, even though all implementations do so | |
| 52 // anyway in practice.) Thus, it is safe to use any byte range for | |
| 53 // Ordinals. | |
| 54 template <typename Traits> | |
| 55 class Ordinal { | |
| 56 public: | |
| 57 // Functors for use with STL algorithms and containers. | |
| 58 class LessThanFn { | |
| 59 public: | |
| 60 LessThanFn(); | |
| 61 | |
| 62 bool operator()(const Ordinal<Traits>& lhs, | |
| 63 const Ordinal<Traits>& rhs) const; | |
| 64 }; | |
| 65 | |
| 66 class EqualsFn { | |
| 67 public: | |
| 68 EqualsFn(); | |
| 69 | |
| 70 bool operator()(const Ordinal<Traits>& lhs, | |
| 71 const Ordinal<Traits>& rhs) const; | |
| 72 }; | |
| 73 | |
| 74 // Creates an Ordinal from the given string. The Ordinal may be | |
| 75 // valid or invalid. | |
| 76 explicit Ordinal(const std::string& ordinal_string); | |
| 77 | |
| 78 // Creates an invalid Ordinal. | |
| 79 Ordinal(); | |
| 80 | |
| 81 // Creates a valid initial Ordinal. This is called to create the first | |
| 82 // element of Ordinal list (i.e. before we have any other values we can | |
| 83 // generate from). | |
| 84 static Ordinal CreateInitialOrdinal(); | |
| 85 | |
| 86 // Returns true iff this Ordinal is valid. This takes constant | |
| 87 // time. | |
| 88 bool IsValid() const; | |
| 89 | |
| 90 // Returns true iff |*this| == |other| or |*this| and |other| | |
| 91 // are both invalid. | |
| 92 bool EqualsOrBothInvalid(const Ordinal& other) const; | |
| 93 | |
| 94 // All remaining functions can only be called if IsValid() holds. | |
| 95 // It is an error to call them if IsValid() is false. | |
| 96 | |
| 97 // Order-related functions. | |
| 98 | |
| 99 // Returns true iff |*this| < |other|. | |
| 100 bool LessThan(const Ordinal& other) const; | |
| 101 | |
| 102 // Returns true iff |*this| > |other|. | |
| 103 bool GreaterThan(const Ordinal& other) const; | |
| 104 | |
| 105 // Returns true iff |*this| == |other| (i.e. |*this| < |other| and | |
| 106 // |other| < |*this| are both false). | |
| 107 bool Equals(const Ordinal& other) const; | |
| 108 | |
| 109 // Given |*this| != |other|, returns a Ordinal x such that | |
| 110 // min(|*this|, |other|) < x < max(|*this|, |other|). It is an error | |
| 111 // to call this function when |*this| == |other|. | |
| 112 Ordinal CreateBetween(const Ordinal& other) const; | |
| 113 | |
| 114 // Returns a Ordinal |x| such that |x| < |*this|. | |
| 115 Ordinal CreateBefore() const; | |
| 116 | |
| 117 // Returns a Ordinal |x| such that |*this| < |x|. | |
| 118 Ordinal CreateAfter() const; | |
| 119 | |
| 120 // It is guaranteed that a Ordinal constructed from the returned | |
| 121 // string will be valid. | |
| 122 std::string ToString() const; | |
| 123 | |
| 124 // Use of copy constructor and default assignment for this class is allowed. | |
| 125 | |
| 126 // Constants for Ordinal digits. | |
| 127 static const uint8 kZeroDigit = Traits::kZeroDigit; | |
| 128 static const uint8 kMaxDigit = Traits::kMaxDigit; | |
| 129 static const size_t kMinLength = Traits::kMinLength; | |
| 130 static const uint8 kOneDigit = kZeroDigit + 1; | |
| 131 static const uint8 kMidDigit = kOneDigit + (kMaxDigit - kOneDigit) / 2; | |
| 132 static const unsigned int kMidDigitValue = kMidDigit - kZeroDigit; | |
| 133 static const unsigned int kMaxDigitValue = kMaxDigit - kZeroDigit; | |
| 134 static const unsigned int kRadix = kMaxDigitValue + 1; | |
| 135 | |
| 136 COMPILE_ASSERT(kOneDigit > kZeroDigit, OrdinalOneDigitGreaterThanMinDigit); | |
| 137 COMPILE_ASSERT(kMidDigit > kOneDigit, OrdinalMidDigitGreaterThanOneDigit); | |
| 138 COMPILE_ASSERT(kMaxDigit > kMidDigit, OrdinalMaxDigitGreaterThanMidDigit); | |
| 139 COMPILE_ASSERT(kMinLength > 0, OrdinalMinLengthIsPositive); | |
| 140 COMPILE_ASSERT(kMidDigitValue > 1, OrdinalMidDigitValueGreaterThanOne); | |
| 141 COMPILE_ASSERT(kMaxDigitValue > kMidDigitValue, | |
| 142 OrdinalMaxDigitValueGreaterThanMidDigitValue); | |
| 143 COMPILE_ASSERT(kRadix == kMaxDigitValue + 1, | |
| 144 OrdinalRadixIsMaxDigitValuePlusOne); | |
| 145 | |
| 146 private: | |
| 147 // Returns true iff the given string satisfies the criteria for a | |
| 148 // valid Ordinal. | |
| 149 static bool IsValidOrdinalString(const std::string& str); | |
| 150 | |
| 151 // Returns the length that string value.substr(0, length) would be with | |
| 152 // trailing zeros removed. | |
| 153 static size_t GetLengthWithoutTrailingZeros( | |
| 154 const std::string& str, | |
| 155 size_t length); | |
| 156 | |
| 157 // Returns the digit at position i, padding with kZeroDigit if required. | |
| 158 static uint8 GetDigit(const std::string& str, size_t i); | |
| 159 | |
| 160 // Returns the digit value at position i, padding with 0 if required. | |
| 161 static int GetDigitValue(const std::string& str, size_t i); | |
| 162 | |
| 163 // Adds the given value to |str| at position i, carrying when | |
| 164 // necessary. Returns the left-most carry. | |
| 165 static int AddDigitValue(std::string* str, size_t i, int digit_value); | |
| 166 | |
| 167 // Returns the proper length |ordinal| should be resized to, | |
| 168 // i.e. the smallest length such that |ordinal| is still greater | |
| 169 // than |lower_bound| and is still valid. |ordinal| should be | |
| 170 // greater than |lower_bound|. | |
| 171 static size_t GetProperLength(const std::string& lower_bound, | |
| 172 const std::string& ordinal); | |
| 173 | |
| 174 // Compute the midpoint string that is between |start| and |end|. | |
| 175 static std::string ComputeMidpoint(const std::string& start, | |
| 176 const std::string& end); | |
| 177 | |
| 178 // Create a Ordinal that is lexigraphically greater than |start| and | |
| 179 // lexigraphically less than |end|. The returned Ordinal will be roughly | |
| 180 // between |start| and |end|. | |
| 181 static Ordinal<Traits> CreateOrdinalBetween(const Ordinal<Traits>& start, | |
| 182 const Ordinal<Traits>& end); | |
| 183 | |
| 184 // The string representation of the Ordinal. | |
| 185 std::string ordinal_string_; | |
| 186 | |
| 187 // The validity of the Ordinal created to cache validity to prevent | |
| 188 // frequent recalculations. | |
| 189 bool is_valid_; | |
| 190 }; | |
| 191 | |
| 192 template <typename Traits> const uint8 Ordinal<Traits>::kZeroDigit; | |
| 193 template <typename Traits> const uint8 Ordinal<Traits>::kMaxDigit; | |
| 194 template <typename Traits> const size_t Ordinal<Traits>::kMinLength; | |
| 195 template <typename Traits> const uint8 Ordinal<Traits>::kOneDigit; | |
| 196 template <typename Traits> const uint8 Ordinal<Traits>::kMidDigit; | |
| 197 template <typename Traits> const unsigned int Ordinal<Traits>::kMidDigitValue; | |
| 198 template <typename Traits> const unsigned int Ordinal<Traits>::kMaxDigitValue; | |
| 199 template <typename Traits> const unsigned int Ordinal<Traits>::kRadix; | |
| 200 | |
| 201 template <typename Traits> | |
| 202 Ordinal<Traits>::LessThanFn::LessThanFn() {} | |
| 203 | |
| 204 template <typename Traits> | |
| 205 bool Ordinal<Traits>::LessThanFn::operator()(const Ordinal<Traits>& lhs, | |
| 206 const Ordinal<Traits>& rhs) const { | |
| 207 return lhs.LessThan(rhs); | |
| 208 } | |
| 209 | |
| 210 template <typename Traits> | |
| 211 Ordinal<Traits>::EqualsFn::EqualsFn() {} | |
| 212 | |
| 213 template <typename Traits> | |
| 214 bool Ordinal<Traits>::EqualsFn::operator()(const Ordinal<Traits>& lhs, | |
| 215 const Ordinal<Traits>& rhs) const { | |
| 216 return lhs.Equals(rhs); | |
| 217 } | |
| 218 | |
| 219 template <typename Traits> | |
| 220 Ordinal<Traits>::Ordinal(const std::string& ordinal_string) | |
| 221 : ordinal_string_(ordinal_string), | |
| 222 is_valid_(IsValidOrdinalString(ordinal_string_)) {} | |
| 223 | |
| 224 template <typename Traits> | |
| 225 Ordinal<Traits>::Ordinal() : is_valid_(false) {} | |
| 226 | |
| 227 template <typename Traits> | |
| 228 Ordinal<Traits> Ordinal<Traits>::CreateInitialOrdinal() { | |
| 229 std::string ordinal_string(Traits::kMinLength, kZeroDigit); | |
| 230 ordinal_string[0] = kMidDigit; | |
| 231 return Ordinal(ordinal_string); | |
| 232 } | |
| 233 | |
| 234 template <typename Traits> | |
| 235 bool Ordinal<Traits>::IsValid() const { | |
| 236 return is_valid_; | |
|
rlarocque
2012/09/05 21:43:46
I'd feel safer if there was a DCHECK(IsInvalidOrdi
akalin
2012/09/06 19:25:14
assuming you meant DCHECK_EQ(is_valid_, ...), done
rlarocque
2012/09/06 21:25:55
Correct. Thanks.
| |
| 237 } | |
| 238 | |
| 239 template <typename Traits> | |
| 240 bool Ordinal<Traits>::EqualsOrBothInvalid(const Ordinal& other) const { | |
| 241 if (!IsValid() && !other.IsValid()) | |
| 242 return true; | |
| 243 | |
| 244 if (!IsValid() || !other.IsValid()) | |
| 245 return false; | |
| 246 | |
| 247 return Equals(other); | |
| 248 } | |
| 249 | |
| 250 template <typename Traits> | |
| 251 bool Ordinal<Traits>::LessThan(const Ordinal& other) const { | |
| 252 CHECK(IsValid()); | |
| 253 CHECK(other.IsValid()); | |
| 254 return ordinal_string_ < other.ordinal_string_; | |
| 255 } | |
| 256 | |
| 257 template <typename Traits> | |
| 258 bool Ordinal<Traits>::GreaterThan(const Ordinal& other) const { | |
| 259 CHECK(IsValid()); | |
| 260 CHECK(other.IsValid()); | |
| 261 return ordinal_string_ > other.ordinal_string_; | |
| 262 } | |
| 263 | |
| 264 template <typename Traits> | |
| 265 bool Ordinal<Traits>::Equals(const Ordinal& other) const { | |
| 266 CHECK(IsValid()); | |
| 267 CHECK(other.IsValid()); | |
| 268 return ordinal_string_ == other.ordinal_string_; | |
| 269 } | |
| 270 | |
| 271 template <typename Traits> | |
| 272 Ordinal<Traits> Ordinal<Traits>::CreateBetween(const Ordinal& other) const { | |
| 273 CHECK(IsValid()); | |
| 274 CHECK(other.IsValid()); | |
| 275 CHECK(!Equals(other)); | |
| 276 | |
| 277 if (LessThan(other)) { | |
| 278 return CreateOrdinalBetween(*this, other); | |
| 279 } else { | |
| 280 return CreateOrdinalBetween(other, *this); | |
| 281 } | |
| 282 } | |
| 283 | |
| 284 template <typename Traits> | |
| 285 Ordinal<Traits> Ordinal<Traits>::CreateBefore() const { | |
| 286 CHECK(IsValid()); | |
| 287 // Create the smallest valid Ordinal of the appropriate length | |
| 288 // to be the minimum boundary. | |
| 289 const size_t length = ordinal_string_.length(); | |
| 290 std::string start(length, kZeroDigit); | |
| 291 start[length - 1] = kOneDigit; | |
| 292 if (start == ordinal_string_) { | |
| 293 start[length - 1] = kZeroDigit; | |
| 294 start += kOneDigit; | |
| 295 } | |
| 296 | |
| 297 // Even though |start| is already a valid Ordinal that is less | |
| 298 // than |*this|, we don't return it because we wouldn't have much space in | |
| 299 // front of it to insert potential future values. | |
| 300 return CreateBetween(Ordinal(start)); | |
| 301 } | |
| 302 | |
| 303 template <typename Traits> | |
| 304 Ordinal<Traits> Ordinal<Traits>::CreateAfter() const { | |
| 305 CHECK(IsValid()); | |
| 306 // Create the largest valid Ordinal of the appropriate length to be | |
| 307 // the maximum boundary. | |
| 308 std::string end(ordinal_string_.length(), kMaxDigit); | |
| 309 if (end == ordinal_string_) | |
| 310 end += kMaxDigit; | |
| 311 | |
| 312 // Even though |end| is already a valid Ordinal that is greater than | |
| 313 // |*this|, we don't return it because we wouldn't have much space after | |
| 314 // it to insert potential future values. | |
| 315 return CreateBetween(Ordinal(end)); | |
| 316 } | |
| 317 | |
| 318 template <typename Traits> | |
| 319 std::string Ordinal<Traits>::ToString() const { | |
| 320 CHECK(IsValid()); | |
| 321 return ordinal_string_; | |
| 322 } | |
| 323 | |
| 324 template <typename Traits> | |
| 325 bool Ordinal<Traits>::IsValidOrdinalString(const std::string& str) { | |
| 326 const size_t length = str.length(); | |
| 327 if (length < kMinLength) | |
| 328 return false; | |
| 329 | |
| 330 bool found_non_zero = false; | |
| 331 for (size_t i = 0; i < length; ++i) { | |
| 332 const uint8 byte = str[i]; | |
| 333 if (byte < kZeroDigit || byte > kMaxDigit) | |
| 334 return false; | |
| 335 if (byte > kZeroDigit) | |
| 336 found_non_zero = true; | |
| 337 } | |
| 338 if (!found_non_zero) | |
| 339 return false; | |
| 340 | |
| 341 if (length > kMinLength) { | |
| 342 const uint8 last_byte = str[str.length() - 1]; | |
| 343 if (last_byte == kZeroDigit) | |
| 344 return false; | |
| 345 } | |
| 346 | |
| 347 return true; | |
| 348 } | |
| 349 | |
| 350 template <typename Traits> | |
| 351 size_t Ordinal<Traits>::GetLengthWithoutTrailingZeros( | |
| 352 const std::string& str, size_t length) { | |
| 353 DCHECK(!str.empty()); | |
| 354 DCHECK_GT(length, 0U); | |
| 355 | |
| 356 size_t end_position = | |
| 357 str.find_last_not_of(static_cast<char>(kZeroDigit), length - 1); | |
| 358 | |
| 359 // If no non kZeroDigit is found then the string is a string of all zeros | |
| 360 // digits so we return 0 as the correct length. | |
| 361 if (end_position == std::string::npos) | |
| 362 return 0; | |
| 363 | |
| 364 return end_position + 1; | |
| 365 } | |
| 366 | |
| 367 template <typename Traits> | |
| 368 uint8 Ordinal<Traits>::GetDigit(const std::string& str, size_t i) { | |
| 369 return (i < str.length()) ? str[i] : kZeroDigit; | |
| 370 } | |
| 371 | |
| 372 template <typename Traits> | |
| 373 int Ordinal<Traits>::GetDigitValue(const std::string& str, size_t i) { | |
| 374 return GetDigit(str, i) - kZeroDigit; | |
| 375 } | |
| 376 | |
| 377 template <typename Traits> | |
| 378 int Ordinal<Traits>::AddDigitValue(std::string* str, | |
| 379 size_t i, int digit_value) { | |
| 380 DCHECK_GE(i, 0U); | |
| 381 DCHECK_LT(i, str->length()); | |
| 382 | |
| 383 for (int j = i; j >= 0 && digit_value > 0; --j) { | |
| 384 int str_j_value = GetDigitValue(*str, j) + digit_value; | |
| 385 digit_value = str_j_value / kRadix; | |
| 386 DCHECK_LE(digit_value, 1); | |
| 387 str_j_value %= kRadix; | |
| 388 (*str)[j] = static_cast<char>(kZeroDigit + str_j_value); | |
| 389 } | |
| 390 return digit_value; | |
| 391 } | |
| 392 | |
| 393 template <typename Traits> | |
| 394 size_t Ordinal<Traits>::GetProperLength(const std::string& lower_bound, | |
| 395 const std::string& ordinal) { | |
| 396 CHECK_GT(ordinal, lower_bound); | |
| 397 | |
| 398 size_t drop_length = GetLengthWithoutTrailingZeros(ordinal, ordinal.length()); | |
| 399 // See if the |ordinal| can be truncated after its last non-zero | |
| 400 // digit without affecting the ordering. | |
| 401 if (drop_length > kMinLength) { | |
| 402 size_t truncated_length = | |
| 403 GetLengthWithoutTrailingZeros(ordinal, drop_length - 1); | |
| 404 | |
| 405 if (truncated_length > 0 && | |
| 406 ordinal.compare(0, truncated_length, lower_bound) > 0) | |
| 407 drop_length = truncated_length; | |
| 408 } | |
| 409 return std::max(drop_length, kMinLength); | |
| 410 } | |
| 411 | |
| 412 template <typename Traits> | |
| 413 std::string Ordinal<Traits>::ComputeMidpoint( | |
| 414 const std::string& start, | |
| 415 const std::string& end) { | |
| 416 size_t max_size = std::max(start.length(), end.length()) + 1; | |
| 417 std::string midpoint(max_size, kZeroDigit); | |
| 418 | |
| 419 // Perform the operation (start + end) / 2 left-to-right by | |
| 420 // maintaining a "forward carry" which is either 0 or | |
| 421 // kMidDigitValue. AddDigitValue() is in general O(n), but this | |
| 422 // operation is still O(n) despite that; calls to AddDigitValue() | |
| 423 // will overflow at most to the last position where AddDigitValue() | |
| 424 // last overflowed. | |
| 425 int forward_carry = 0; | |
| 426 for (size_t i = 0; i < max_size; ++i) { | |
| 427 const int sum_value = GetDigitValue(start, i) + GetDigitValue(end, i); | |
| 428 const int digit_value = sum_value / 2 + forward_carry; | |
| 429 // AddDigitValue returning a non-zero carry would imply that | |
| 430 // midpoint[0] >= kMaxDigit, which one can show is impossible. | |
| 431 CHECK_EQ(AddDigitValue(&midpoint, i, digit_value), 0); | |
| 432 forward_carry = (sum_value % 2 == 1) ? kMidDigitValue : 0; | |
| 433 } | |
| 434 DCHECK_EQ(forward_carry, 0); | |
| 435 | |
| 436 return midpoint; | |
| 437 } | |
| 438 | |
| 439 template <typename Traits> | |
| 440 Ordinal<Traits> Ordinal<Traits>::CreateOrdinalBetween( | |
| 441 const Ordinal<Traits>& start, | |
| 442 const Ordinal<Traits>& end) { | |
| 443 CHECK(start.IsValid()); | |
| 444 CHECK(end.IsValid()); | |
| 445 CHECK(start.LessThan(end)); | |
| 446 const std::string& start_string = start.ToString(); | |
| 447 const std::string& end_string = end.ToString(); | |
| 448 DCHECK_LT(start_string, end_string); | |
| 449 | |
| 450 std::string midpoint = ComputeMidpoint(start_string, end_string); | |
| 451 const size_t proper_length = GetProperLength(start_string, midpoint); | |
| 452 midpoint.resize(proper_length, kZeroDigit); | |
| 453 | |
| 454 DCHECK_GT(midpoint, start_string); | |
| 455 DCHECK_LT(midpoint, end_string); | |
| 456 | |
| 457 Ordinal<Traits> midpoint_ordinal(midpoint); | |
| 458 DCHECK(midpoint_ordinal.IsValid()); | |
| 459 return midpoint_ordinal; | |
| 460 } | |
| 461 | |
| 462 template <typename Traits> | |
| 463 void PrintTo(const Ordinal<Traits>& ordinal, ::std::ostream* os) { | |
| 464 *os << (ordinal.IsValid() ? ordinal.ToString() : "[invalid]"); | |
| 465 } | |
| 466 | |
| 467 } // namespace syncer | |
| 468 | |
| 469 #endif // SYNC_INTERNAL_API_PUBLIC_BASE_ORDINAL_H_ | |
| OLD | NEW |