Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Classes | Macros | Functions
SkTSearch.h File Reference
#include "include/private/base/SkAssert.h"
#include <cstddef>

Go to the source code of this file.

Classes

class  SkAutoAsciiToLC
 

Macros

#define SkCastForQSort(compare)   reinterpret_cast<int (*)(const void*, const void*)>(compare)
 

Functions

template<typename T , typename K , typename LESS >
int SkTSearch (const T base[], int count, const K &key, size_t elemSize, const LESS &less)
 
template<typename T , bool(LESS)(const T &, const T &) >
int SkTSearch (const T base[], int count, const T &target, size_t elemSize)
 
template<typename T >
int SkTSearch (const T base[], int count, const T &target, size_t elemSize)
 
template<typename T , bool(LESS)(const T &, const T &) >
int SkTSearch (T *base[], int count, T *target, size_t elemSize)
 
int SkStrSearch (const char *const *base, int count, const char target[], size_t target_len, size_t elemSize)
 
int SkStrSearch (const char *const *base, int count, const char target[], size_t elemSize)
 
int SkStrLCSearch (const char *const *base, int count, const char target[], size_t target_len, size_t elemSize)
 
int SkStrLCSearch (const char *const *base, int count, const char target[], size_t elemSize)
 

Macro Definition Documentation

◆ SkCastForQSort

#define SkCastForQSort (   compare)    reinterpret_cast<int (*)(const void*, const void*)>(compare)

Definition at line 130 of file SkTSearch.h.

Function Documentation

◆ SkStrLCSearch() [1/2]

int SkStrLCSearch ( const char *const *  base,
int  count,
const char  target[],
size_t  elemSize 
)

Definition at line 74 of file SkTSearch.cpp.

76{
77 return SkStrLCSearch(base, count, target, strlen(target), elemSize);
78}
int count
int SkStrLCSearch(const char *const *base, int count, const char target[], size_t len, size_t elemSize)
Definition SkTSearch.cpp:64
uint32_t * target

◆ SkStrLCSearch() [2/2]

int SkStrLCSearch ( const char *const *  base,
int  count,
const char  target[],
size_t  target_len,
size_t  elemSize 
)

Like SkStrSearch, but treats target as if it were all lower-case. Assumes that base points to a table of lower-case strings.

Definition at line 64 of file SkTSearch.cpp.

66{
68
69 SkAutoAsciiToLC tolc(target, len);
70
71 return SkStrSearch(base, count, tolc.lc(), len, elemSize);
72}
#define SkASSERT(cond)
Definition SkAssert.h:116
int SkStrSearch(const char *const *base, int count, const char target[], size_t target_len, size_t elemSize)
Definition SkTSearch.cpp:22

◆ SkStrSearch() [1/2]

int SkStrSearch ( const char *const *  base,
int  count,
const char  target[],
size_t  elemSize 
)

Definition at line 58 of file SkTSearch.cpp.

60{
61 return SkStrSearch(base, count, target, strlen(target), elemSize);
62}

◆ SkStrSearch() [2/2]

int SkStrSearch ( const char *const *  base,
int  count,
const char  target[],
size_t  target_len,
size_t  elemSize 
)

Definition at line 22 of file SkTSearch.cpp.

24{
25 if (count <= 0)
26 return ~0;
27
28 SkASSERT(base != nullptr);
29
30 int lo = 0;
31 int hi = count - 1;
32
33 while (lo < hi)
34 {
35 int mid = (hi + lo) >> 1;
36 const char* elem = index_into_base(base, mid, elemSize);
37
38 int cmp = strncmp(elem, target, target_len);
39 if (cmp < 0)
40 lo = mid + 1;
41 else if (cmp > 0 || strlen(elem) > target_len)
42 hi = mid;
43 else
44 return mid;
45 }
46
47 const char* elem = index_into_base(base, hi, elemSize);
48 int cmp = strncmp(elem, target, target_len);
49 if (cmp || strlen(elem) > target_len)
50 {
51 if (cmp < 0)
52 hi += 1;
53 hi = ~hi;
54 }
55 return hi;
56}
static const char * index_into_base(const char *const *base, int index, size_t elemSize)
Definition SkTSearch.cpp:16

◆ SkTSearch() [1/4]

template<typename T , typename K , typename LESS >
int SkTSearch ( const T  base[],
int  count,
const K key,
size_t  elemSize,
const LESS &  less 
)

All of the SkTSearch variants want to return the index (0...N-1) of the found element, or the bit-not of where to insert the element.

At a simple level, if the return value is negative, it was not found.

For clients that want to insert the new element if it was not found, use the following logic:

int index = SkTSearch(...); if (index >= 0) { // found at index } else { index = ~index; // now we are positive // insert at index }

Definition at line 41 of file SkTSearch.h.

42{
43 SkASSERT(count >= 0);
44 if (count <= 0) {
45 return ~0;
46 }
47
48 SkASSERT(base != nullptr); // base may be nullptr if count is zero
49
50 int lo = 0;
51 int hi = count - 1;
52
53 while (lo < hi) {
54 int mid = lo + ((hi - lo) >> 1);
55 const T* elem = (const T*)((const char*)base + mid * elemSize);
56
57 if (less(*elem, key))
58 lo = mid + 1;
59 else
60 hi = mid;
61 }
62
63 const T* elem = (const T*)((const char*)base + hi * elemSize);
64 if (less(*elem, key)) {
65 hi += 1;
66 hi = ~hi;
67 } else if (less(key, *elem)) {
68 hi = ~hi;
69 }
70 return hi;
71}
#define T

◆ SkTSearch() [2/4]

template<typename T , bool(LESS)(const T &, const T &) >
int SkTSearch ( const T  base[],
int  count,
const T target,
size_t  elemSize 
)

Definition at line 75 of file SkTSearch.h.

75 {
76 return SkTSearch(base, count, target, elemSize,
77 [](const T& a, const T& b) { return LESS(a, b); });
78}
int SkTSearch(const T base[], int count, const K &key, size_t elemSize, const LESS &less)
Definition SkTSearch.h:41
static bool b
struct MyStruct a[10]

◆ SkTSearch() [3/4]

template<typename T >
int SkTSearch ( const T  base[],
int  count,
const T target,
size_t  elemSize 
)

Definition at line 82 of file SkTSearch.h.

82 {
83 return SkTSearch(base, count, target, elemSize, [](const T& a, const T& b) { return a < b; });
84}

◆ SkTSearch() [4/4]

template<typename T , bool(LESS)(const T &, const T &) >
int SkTSearch ( T base[],
int  count,
T target,
size_t  elemSize 
)

Definition at line 89 of file SkTSearch.h.

89 {
90 return SkTSearch(base, count, target, elemSize,
91 [](const T* t, const T* k) { return LESS(*t, *k); });
92}