| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc. | 2 * Copyright © 2007,2008,2009,2010 Red Hat, Inc. |
| 3 * Copyright © 2012 Google, Inc. | 3 * Copyright © 2012 Google, Inc. |
| 4 * | 4 * |
| 5 * This is part of HarfBuzz, a text shaping library. | 5 * This is part of HarfBuzz, a text shaping library. |
| 6 * | 6 * |
| 7 * Permission is hereby granted, without written agreement and without | 7 * Permission is hereby granted, without written agreement and without |
| 8 * license or royalty fees, to use, copy, modify, and distribute this | 8 * license or royalty fees, to use, copy, modify, and distribute this |
| 9 * software and its documentation for any purpose, provided that the | 9 * software and its documentation for any purpose, provided that the |
| 10 * above copyright notice and the following two paragraphs appear in | 10 * above copyright notice and the following two paragraphs appear in |
| (...skipping 931 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 942 public: | 942 public: |
| 943 DEFINE_SIZE_ARRAY (sizeof (USHORT), array); | 943 DEFINE_SIZE_ARRAY (sizeof (USHORT), array); |
| 944 }; | 944 }; |
| 945 | 945 |
| 946 | 946 |
| 947 /* An array with sorted elements. Supports binary searching. */ | 947 /* An array with sorted elements. Supports binary searching. */ |
| 948 template <typename Type> | 948 template <typename Type> |
| 949 struct SortedArrayOf : ArrayOf<Type> { | 949 struct SortedArrayOf : ArrayOf<Type> { |
| 950 | 950 |
| 951 template <typename SearchType> | 951 template <typename SearchType> |
| 952 inline int search (const SearchType &x) const { | 952 inline int search (const SearchType &x) const |
| 953 unsigned int count = this->len; | 953 { |
| 954 /* Linear search is *much* faster for small counts. */ | 954 /* Hand-coded bsearch here since this is in the hot inner loop. */ |
| 955 if (likely (count < 32)) { | 955 int min = 0, max = (int) this->len - 1; |
| 956 for (unsigned int i = 0; i < count; i++) | 956 while (min <= max) |
| 957 » if (this->array[i].cmp (x) == 0) | 957 { |
| 958 » return i; | 958 int mid = (min + max) / 2; |
| 959 return -1; | 959 int c = this->array[mid].cmp (x); |
| 960 } else { | 960 if (c < 0) |
| 961 struct Cmp { | 961 max = mid - 1; |
| 962 » static int cmp (const SearchType *a, const Type *b) { return b->cmp (*a)
; } | 962 else if (c > 0) |
| 963 }; | 963 min = mid + 1; |
| 964 const Type *p = (const Type *) bsearch (&x, this->array, this->len, sizeof
(this->array[0]), (hb_compare_func_t) Cmp::cmp); | 964 else |
| 965 return p ? p - this->array : -1; | 965 return mid; |
| 966 } | 966 } |
| 967 return -1; |
| 967 } | 968 } |
| 968 }; | 969 }; |
| 969 | 970 |
| 970 | 971 |
| 971 } /* namespace OT */ | 972 } /* namespace OT */ |
| 972 | 973 |
| 973 | 974 |
| 974 #endif /* HB_OPEN_TYPE_PRIVATE_HH */ | 975 #endif /* HB_OPEN_TYPE_PRIVATE_HH */ |
| OLD | NEW |