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

Side by Side Diff: courgette/third_party/bsdiff/qsufsort.h

Issue 2031193002: [Courgette] Refactor BSDiff namespaces and bsdiff::search() interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make license style uniform; remove checklicenses.py entries. Created 4 years, 6 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
OLDNEW
1 /* 1 // Copyright 2003, 2004 Colin Percival
2 qsufsort.h -- Suffix array generation. 2 // All rights reserved
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted providing that the following conditions
6 // are met:
7 // 1. Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 //
13 // For the terms under which this work may be distributed, please see
14 // the adjoining file "LICENSE".
15 //
16 // qsufsort.h -- Suffix array generation.
17 //
18 // ChangeLog:
19 // 2005-05-05 - Use the modified header struct from bspatch.h; use 32-bit
20 // values throughout.
21 // --Benjamin Smedberg <benjamin@smedbergs.us>
22 // 2010-05-26 - Use a paged array for V and I. The address space may be too
23 // fragmented for these big arrays to be contiguous.
24 // --Stephen Adams <sra@chromium.org>
25 // 2015-08-03 - Extract QSufSort to a separate file as template.
26 // --Samuel Huang <huangs@chromium.org>
27 // 2015-08-19 - Optimize split(), add comments.
28 // --Samuel Huang <huangs@chromium.org>
29 // 2016-04-27 - Change split() to use Bentley & McIlroy's pivot selection
30 // algorithm, which QSufSort originally used. Reference:
31 // http://www.larsson.dogma.net/qsufsort.c
32 // --Samuel Huang <huangs@chromium.org>
3 33
4 Copyright 2003 Colin Percival 34 // Copyright 2016 The Chromium Authors. All rights reserved.
5 35 // Use of this source code is governed by a BSD-style license that can be
6 For the terms under which this work may be distributed, please see 36 // found in the LICENSE file.
7 the adjoining file "LICENSE".
8
9 ChangeLog:
10 2005-05-05 - Use the modified header struct from bspatch.h; use 32-bit
11 values throughout.
12 --Benjamin Smedberg <benjamin@smedbergs.us>
13 2010-05-26 - Use a paged array for V and I. The address space may be too
14 fragmented for these big arrays to be contiguous.
15 --Stephen Adams <sra@chromium.org>
16 2015-08-03 - Extract QSufSort to a separate file as template.
17 --Samuel Huang <huangs@chromium.org>
18 2015-08-19 - Optimize split() and search(), add comments.
19 --Samuel Huang <huangs@chromium.org>
20 2016-04-27 - Change split() to use Bentley & McIlroy's pivot selection
21 algorithm, which QSufSort originally used. Reference:
22 http://www.larsson.dogma.net/qsufsort.c
23 --Samuel Huang <huangs@chromium.org>
24 */
25 37
26 #ifndef COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_ 38 #ifndef COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_
27 #define COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_ 39 #define COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_
28 40
29 #include <algorithm>
30 #include <cstring>
31
32 namespace courgette {
33 namespace qsuf { 41 namespace qsuf {
34 42
35 // ------------------------------------------------------------------------ 43 // ------------------------------------------------------------------------
36 // 44 //
37 // The following code is taken verbatim from 'bsdiff.c'. Please keep all the 45 // The following code is taken verbatim from 'bsdiff.c'. Please keep all the
38 // code formatting and variable names. The changes from the original are: 46 // code formatting and variable names. The changes from the original are:
39 // (1) replacing tabs with spaces, 47 // (1) replacing tabs with spaces,
40 // (2) indentation and spacing, 48 // (2) indentation and spacing,
41 // (3) using 'const', 49 // (3) using 'const',
42 // (4) changing the V and I parameters from int* to template <typename T>. 50 // (4) changing the V and I parameters from int* to template <typename T>.
43 // (5) optimizing split() and search(); fix styles. 51 // (5) optimizing split(); fix styles.
52 // (6) moving matchlen() and search() to a separate file.
44 // 53 //
45 // The code appears to be a rewritten version of the suffix array algorithm 54 // The code appears to be a rewritten version of the suffix array algorithm
46 // presented in "Faster Suffix Sorting" by N. Jesper Larsson and Kunihiko 55 // presented in "Faster Suffix Sorting" by N. Jesper Larsson and Kunihiko
47 // Sadakane, special cased for bytes. 56 // Sadakane, special cased for bytes.
48 57
49 namespace { 58 namespace {
50 59
51 template <typename T> 60 template <typename T>
52 T median3(const T& a, const T& b, const T& c) { 61 T median3(const T& a, const T& b, const T& c) {
53 if (a < b) 62 if (a < b)
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 }; 200 };
192 }; 201 };
193 if (len) 202 if (len)
194 I[i - len] = -len; 203 I[i - len] = -len;
195 }; 204 };
196 205
197 for (i = 0; i < oldsize + 1; i++) 206 for (i = 0; i < oldsize + 1; i++)
198 I[V[i]] = i; 207 I[V[i]] = i;
199 } 208 }
200 209
201 static int matchlen(const unsigned char* old,
202 int oldsize,
203 const unsigned char* newbuf,
204 int newsize) {
205 int i;
206
207 for (i = 0; (i < oldsize) && (i < newsize); i++)
208 if (old[i] != newbuf[i])
209 break;
210
211 return i;
212 }
213
214 template <class T>
215 static int search(T I,
216 const unsigned char* old,
217 int oldsize,
218 const unsigned char* newbuf,
219 int newsize,
220 int* pos) {
221 int lo = 0;
222 int hi = oldsize;
223 while (hi - lo >= 2) {
224 int mid = (lo + hi) / 2;
225 if (memcmp(old + I[mid], newbuf, std::min(oldsize - I[mid], newsize)) < 0) {
226 lo = mid;
227 } else {
228 hi = mid;
229 }
230 }
231
232 int x = matchlen(old + I[lo], oldsize - I[lo], newbuf, newsize);
233 int y = matchlen(old + I[hi], oldsize - I[hi], newbuf, newsize);
234 if (x > y) {
235 *pos = I[lo];
236 return x;
237 }
238 *pos = I[hi];
239 return y;
240 }
241
242 // End of 'verbatim' code. 210 // End of 'verbatim' code.
243 // ------------------------------------------------------------------------ 211 // ------------------------------------------------------------------------
244 212
245 } // namespace qsuf 213 } // namespace qsuf
246 } // namespace courgette 214
247 #endif // COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_ 215 #endif // COURGETTE_THIRD_PARTY_BSDIFF_QSUFSORT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698