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

Side by Side Diff: src/core/SkPtrRecorder.cpp

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/SkPtrRecorder.h ('k') | src/core/SkTSort.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 #include "SkPtrRecorder.h" 8 #include "SkPtrRecorder.h"
9 #include "SkTSearch.h" 9 #include "SkTSearch.h"
10 10
11 SK_DEFINE_INST_COUNT(SkPtrSet) 11 SK_DEFINE_INST_COUNT(SkPtrSet)
12 SK_DEFINE_INST_COUNT(SkNamedFactorySet) 12 SK_DEFINE_INST_COUNT(SkNamedFactorySet)
13 13
14 void SkPtrSet::reset() { 14 void SkPtrSet::reset() {
15 Pair* p = fList.begin(); 15 Pair* p = fList.begin();
16 Pair* stop = fList.end(); 16 Pair* stop = fList.end();
17 while (p < stop) { 17 while (p < stop) {
18 this->decPtr(p->fPtr); 18 this->decPtr(p->fPtr);
19 p += 1; 19 p += 1;
20 } 20 }
21 fList.reset(); 21 fList.reset();
22 } 22 }
23 23
24 int SkPtrSet::Cmp(const Pair* a, const Pair* b) { 24 bool SkPtrSet::Less(const Pair& a, const Pair& b) {
25 return (char*)a->fPtr - (char*)b->fPtr; 25 return (char*)a.fPtr < (char*)b.fPtr;
26 } 26 }
27 27
28 uint32_t SkPtrSet::find(void* ptr) const { 28 uint32_t SkPtrSet::find(void* ptr) const {
29 if (NULL == ptr) { 29 if (NULL == ptr) {
30 return 0; 30 return 0;
31 } 31 }
32 32
33 int count = fList.count(); 33 int count = fList.count();
34 Pair pair; 34 Pair pair;
35 pair.fPtr = ptr; 35 pair.fPtr = ptr;
36 36
37 int index = SkTSearch<Pair, Cmp>(fList.begin(), count, pair, sizeof(pair)); 37 int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
38 if (index < 0) { 38 if (index < 0) {
39 return 0; 39 return 0;
40 } 40 }
41 return fList[index].fIndex; 41 return fList[index].fIndex;
42 } 42 }
43 43
44 uint32_t SkPtrSet::add(void* ptr) { 44 uint32_t SkPtrSet::add(void* ptr) {
45 if (NULL == ptr) { 45 if (NULL == ptr) {
46 return 0; 46 return 0;
47 } 47 }
48 48
49 int count = fList.count(); 49 int count = fList.count();
50 Pair pair; 50 Pair pair;
51 pair.fPtr = ptr; 51 pair.fPtr = ptr;
52 52
53 int index = SkTSearch<Pair, Cmp>(fList.begin(), count, pair, sizeof(pair)); 53 int index = SkTSearch<Pair, Less>(fList.begin(), count, pair, sizeof(pair));
54 if (index < 0) { 54 if (index < 0) {
55 index = ~index; // turn it back into an index for insertion 55 index = ~index; // turn it back into an index for insertion
56 this->incPtr(ptr); 56 this->incPtr(ptr);
57 pair.fIndex = count + 1; 57 pair.fIndex = count + 1;
58 *fList.insert(index) = pair; 58 *fList.insert(index) = pair;
59 return count + 1; 59 return count + 1;
60 } else { 60 } else {
61 return fList[index].fIndex; 61 return fList[index].fIndex;
62 } 62 }
63 } 63 }
64 64
65 void SkPtrSet::copyToArray(void* array[]) const { 65 void SkPtrSet::copyToArray(void* array[]) const {
66 int count = fList.count(); 66 int count = fList.count();
67 if (count > 0) { 67 if (count > 0) {
68 SkASSERT(array); 68 SkASSERT(array);
69 const Pair* p = fList.begin(); 69 const Pair* p = fList.begin();
70 // p->fIndex is base-1, so we need to subtract to find its slot 70 // p->fIndex is base-1, so we need to subtract to find its slot
71 for (int i = 0; i < count; i++) { 71 for (int i = 0; i < count; i++) {
72 int index = p[i].fIndex - 1; 72 int index = p[i].fIndex - 1;
73 SkASSERT((unsigned)index < (unsigned)count); 73 SkASSERT((unsigned)index < (unsigned)count);
74 array[index] = p[i].fPtr; 74 array[index] = p[i].fPtr;
75 } 75 }
76 } 76 }
77 } 77 }
OLDNEW
« no previous file with comments | « src/core/SkPtrRecorder.h ('k') | src/core/SkTSort.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698