Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: chrome/common/string_index.cc

Issue 8236002: Create StringOrdinal to allow placement of strings in sorted lists (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 #include "chrome/common/string_index.h"
6
7 StringIndex::StringIndex(): kMaxDigitValue(kMaxDigit - kZeroDigit),
akalin 2011/10/11 19:34:39 surely you don't need these to be initialized for
8 kMidDigitValue(kMidDigit - kZeroDigit) {
9 }
10
11 std::string StringIndex::RemoveTrailingZeros(const std::string& value) {
12 int end_position = value.length() - 1;
akalin 2011/10/11 19:34:39 assert that value is non-empty
13 while (value[end_position] == kZeroDigit) {
14 --end_position;
15 }
16
17 return value.substr(0, end_position + 1);
18 }
19
20 void StringIndex::AddHalf(int position, std::string& value) {
21 value[position] += kMidDigitValue;
22
23 if (value[position] > kMaxDigit) {
24 value[position] -= kMaxDigitValue;
25 ++value[position - 1];
akalin 2011/10/11 19:34:39 what if this also overflows? consider (.99999 + .
csharp1 2011/10/11 21:46:03 I'm fairly certain it can't overflow. It could on
csharp1 2011/10/11 21:46:03 I'm fairly certain it can't overflow. It could on
akalin 2011/10/11 22:00:24 Ah, it can't overflow because mid digit value is s
csharp1 2011/10/12 15:31:12 I fixed the middle digit bug, but I still don't se
akalin 2011/10/12 18:13:07 I did, in my previous two comments about it. :P To
csharp1 2011/10/12 18:52:46 Ok, I made a unit test with the example you gave a
26 }
27 }
28
29 int StringIndex::GetPositionValue(const std::string& value, size_t index) {
akalin 2011/10/11 19:34:39 this can be a static function, or a free function
30 return index < value.length() ? value[index] - kZeroDigit : 0;
31 }
32
33 std::string StringIndex::CreateStringBetween(const std::string& start,
34 const std::string& end) {
35 // If we have no end value, we create one that has the maximum value
36 // to use as our end point.
37 if (end.length() == 0) {
akalin 2011/10/11 19:34:39 end.empty()
38 return CreateStringBetween(start,
akalin 2011/10/11 19:34:39 this fails the precondition below
csharp1 2011/10/11 21:46:03 I'm not sure what you mean here. This does fail st
akalin 2011/10/11 22:00:24 Seems better to go the extra step to make the prec
39 std::string(start.length(), kMaxDigit));
40 }
akalin 2011/10/11 19:34:39 assert that start < end || start.empty() assert t
41
42 size_t max_size = std::max(start.length(), end.length());
43 std::string middle_string(max_size, kZeroDigit);
44
45 bool add_half = false;
46 for (size_t i = 0; i < max_size; ++i) {
47 int char_value = GetPositionValue(start, i);
48 char_value += GetPositionValue(end, i);
49
50 middle_string[i] += (char_value / 2);
51 if (add_half) {
52 AddHalf(i, middle_string);
53 }
54 add_half = (char_value % 2 == 1);
55 }
56
57 std::string no_trailing_zeros = RemoveTrailingZeros(middle_string);
58 if (no_trailing_zeros.length() == 0) {
59 middle_string += kMidDigit;
akalin 2011/10/11 19:34:39 pretty sure this is only needed because you don't
60 } else {
61 middle_string = no_trailing_zeros;
62 }
63
64 // We only add an extra digit if it is required for uniqueness, otherwise
65 // we accept a close to middle placement
66 if (start == middle_string) {
67 middle_string += kMidDigit;
68 }
69
70 return middle_string;
akalin 2011/10/11 19:34:39 assert that start < middle < end
71 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698