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

Side by Side Diff: src/core/SkPictureFlat.h

Issue 15070011: One SkTSearch to rule them all. Allow key to be of different type than the array. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: fixes to compile on gcc Created 7 years, 7 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
« no previous file with comments | « src/core/SkPathMeasure.cpp ('k') | src/core/SkPtrRecorder.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2011 Google Inc. 3 * Copyright 2011 Google Inc.
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 #ifndef SkPictureFlat_DEFINED 8 #ifndef SkPictureFlat_DEFINED
9 #define SkPictureFlat_DEFINED 9 #define SkPictureFlat_DEFINED
10 10
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 * the buffers. 270 * the buffers.
271 * 271 *
272 * dataToCompare() returns 2 fields before the flattened data: 272 * dataToCompare() returns 2 fields before the flattened data:
273 * - checksum 273 * - checksum
274 * - size 274 * - size
275 * This ensures that if we see two blocks of different length, we will 275 * This ensures that if we see two blocks of different length, we will
276 * notice that right away, and not read any further. It also ensures that 276 * notice that right away, and not read any further. It also ensures that
277 * we see the checksum right away, so that most of the time it is enough 277 * we see the checksum right away, so that most of the time it is enough
278 * to short-circuit our comparison. 278 * to short-circuit our comparison.
279 */ 279 */
280 static int Compare(const SkFlatData* a, const SkFlatData* b) { 280 static int Compare(const SkFlatData& a, const SkFlatData& b) {
281 const uint32_t* stop = a->dataStop(); 281 const uint32_t* stop = a.dataStop();
282 const uint32_t* a_ptr = a->dataToCompare() - 1; 282 const uint32_t* a_ptr = a.dataToCompare() - 1;
283 const uint32_t* b_ptr = b->dataToCompare() - 1; 283 const uint32_t* b_ptr = b.dataToCompare() - 1;
284 // We use -1 above, so we can pre-increment our pointers in the loop 284 // We use -1 above, so we can pre-increment our pointers in the loop
285 while (*++a_ptr == *++b_ptr) {} 285 while (*++a_ptr == *++b_ptr) {}
286 286
287 if (a_ptr == stop) { // sentinel 287 if (a_ptr == stop) { // sentinel
288 SkASSERT(b->dataStop() == b_ptr); 288 SkASSERT(b.dataStop() == b_ptr);
289 return 0; 289 return 0;
290 } 290 }
291 SkASSERT(a_ptr < a->dataStop()); 291 SkASSERT(a_ptr < a.dataStop());
292 SkASSERT(b_ptr < b->dataStop()); 292 SkASSERT(b_ptr < b.dataStop());
293 return (*a_ptr < *b_ptr) ? -1 : 1; 293 return (*a_ptr < *b_ptr) ? -1 : 1;
294 } 294 }
295 295
296 // Adapts Compare to be used with SkTSearch
297 static bool Less(const SkFlatData& a, const SkFlatData& b) {
298 return Compare(a, b) < 0;
299 }
300
296 int index() const { return fIndex; } 301 int index() const { return fIndex; }
297 const void* data() const { return (const char*)this + sizeof(*this); } 302 const void* data() const { return (const char*)this + sizeof(*this); }
298 void* data() { return (char*)this + sizeof(*this); } 303 void* data() { return (char*)this + sizeof(*this); }
299 // Our data is always 32bit aligned, so we can offer this accessor 304 // Our data is always 32bit aligned, so we can offer this accessor
300 uint32_t* data32() { return (uint32_t*)this->data(); } 305 uint32_t* data32() { return (uint32_t*)this->data(); }
301 // Returns the size of the flattened data. 306 // Returns the size of the flattened data.
302 size_t flatSize() const { return fFlatSize; } 307 size_t flatSize() const { return fFlatSize; }
303 308
304 void setSentinelInCache() { 309 void setSentinelInCache() {
305 this->setSentinel(kInCache_Sentinel); 310 this->setSentinel(kInCache_Sentinel);
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 T* dst = new T; 526 T* dst = new T;
522 this->unflatten(dst, element); 527 this->unflatten(dst, element);
523 return dst; 528 return dst;
524 } 529 }
525 530
526 const SkFlatData* findAndReturnFlat(const T& element) { 531 const SkFlatData* findAndReturnFlat(const T& element) {
527 SkFlatData* flat = SkFlatData::Create(fController, &element, fNextIndex, fFlattenProc); 532 SkFlatData* flat = SkFlatData::Create(fController, &element, fNextIndex, fFlattenProc);
528 533
529 int hashIndex = ChecksumToHashIndex(flat->checksum()); 534 int hashIndex = ChecksumToHashIndex(flat->checksum());
530 const SkFlatData* candidate = fHash[hashIndex]; 535 const SkFlatData* candidate = fHash[hashIndex];
531 if (candidate && !SkFlatData::Compare(flat, candidate)) { 536 if (candidate && !SkFlatData::Compare(*flat, *candidate)) {
532 fController->unalloc(flat); 537 fController->unalloc(flat);
533 return candidate; 538 return candidate;
534 } 539 }
535 540
536 int index = SkTSearch<SkFlatData>((const SkFlatData**) fSortedData.begin (), 541 int index = SkTSearch<const SkFlatData,
537 fSortedData.count(), flat, sizeof(flat ), 542 SkFlatData::Less>((const SkFlatData**) fSortedData .begin(),
538 &SkFlatData::Compare); 543 fSortedData.count(), flat, sizeo f(flat));
539 if (index >= 0) { 544 if (index >= 0) {
540 fController->unalloc(flat); 545 fController->unalloc(flat);
541 fHash[hashIndex] = fSortedData[index]; 546 fHash[hashIndex] = fSortedData[index];
542 return fSortedData[index]; 547 return fSortedData[index];
543 } 548 }
544 549
545 index = ~index; 550 index = ~index;
546 *fSortedData.insert(index) = flat; 551 *fSortedData.insert(index) = flat;
547 *fIndexedData.insert(flat->index()) = flat; 552 *fIndexedData.insert(flat->index()) = flat;
548 SkASSERT(fSortedData.count() == fNextIndex); 553 SkASSERT(fSortedData.count() == fNextIndex);
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 static void flattenRegion(SkOrderedWriteBuffer& buffer, const void* obj) { 714 static void flattenRegion(SkOrderedWriteBuffer& buffer, const void* obj) {
710 buffer.getWriter32()->writeRegion(*(SkRegion*)obj); 715 buffer.getWriter32()->writeRegion(*(SkRegion*)obj);
711 } 716 }
712 717
713 static void unflattenRegion(SkOrderedReadBuffer& buffer, void* obj) { 718 static void unflattenRegion(SkOrderedReadBuffer& buffer, void* obj) {
714 buffer.getReader32()->readRegion((SkRegion*)obj); 719 buffer.getReader32()->readRegion((SkRegion*)obj);
715 } 720 }
716 }; 721 };
717 722
718 #endif 723 #endif
OLDNEW
« no previous file with comments | « src/core/SkPathMeasure.cpp ('k') | src/core/SkPtrRecorder.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698