add find()

git-svn-id: http://skia.googlecode.com/svn/trunk@1243 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2011-05-04 18:03:00 +00:00
parent 2dbd0449bf
commit 8d90eeba09
2 changed files with 22 additions and 0 deletions

View File

@ -28,6 +28,12 @@
*/
class SkPtrSet : public SkRefCnt {
public:
/**
* Search for the specified ptr in the set. If it is found, return its
* 32bit ID [1..N], or if not found, return 0. Always returns 0 for NULL.
*/
uint32_t find(void*) const;
/**
* Add the specified ptr to the set, returning a unique 32bit ID for it
* [1...N]. Duplicate ptrs will return the same ID.

View File

@ -15,6 +15,22 @@ int SkPtrSet::Cmp(const Pair& a, const Pair& b) {
return (char*)a.fPtr - (char*)b.fPtr;
}
uint32_t SkPtrSet::find(void* ptr) const {
if (NULL == ptr) {
return 0;
}
int count = fList.count();
Pair pair;
pair.fPtr = ptr;
int index = SkTSearch<Pair>(fList.begin(), count, pair, sizeof(pair), &Cmp);
if (index < 0) {
return 0;
}
return fList[index].fIndex;
}
uint32_t SkPtrSet::add(void* ptr) {
if (NULL == ptr) {
return 0;