hb-array.hh 7.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
/*
 * Copyright © 2018  Google, Inc.
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Google Author(s): Behdad Esfahbod
 */

#ifndef HB_ARRAY_HH
#define HB_ARRAY_HH

#include "hb.hh"


template <typename Type>
struct hb_sorted_array_t;

template <typename Type>
struct hb_array_t
{
B
Behdad Esfahbod 已提交
39 40
  typedef Type ItemType;
  enum { item_size = hb_static_size (Type) };
41

B
Behdad Esfahbod 已提交
42 43 44
  /*
   * Constructors.
   */
45 46 47
  hb_array_t (void) : arrayZ (nullptr), len (0) {}
  hb_array_t (const hb_array_t &o) : arrayZ (o.arrayZ), len (o.len) {}
  hb_array_t (Type *array_, unsigned int len_) : arrayZ (array_), len (len_) {}
48
  template <unsigned int len_> hb_array_t (Type (&array_)[len_]) : arrayZ (array_), len (len_) {}
49

B
Behdad Esfahbod 已提交
50 51 52 53
  /*
   * Operators.
   */

54 55 56
  Type& operator [] (int i_) const
  {
    unsigned int i = (unsigned int) i_;
57
    if (unlikely (i >= len)) return CrapOrNull(Type);
58 59 60
    return arrayZ[i];
  }

B
Behdad Esfahbod 已提交
61
  explicit_operator bool (void) const { return len; }
62
  Type * operator & (void) const { return arrayZ; }
B
Behdad Esfahbod 已提交
63
  Type & operator * (void) { return (this->operator [])[0]; }
B
Behdad Esfahbod 已提交
64
  template <typename T> operator T * (void) const { return arrayZ; }
B
Behdad Esfahbod 已提交
65

B
Behdad Esfahbod 已提交
66 67 68 69 70 71 72 73
  hb_array_t<Type> & operator += (unsigned int count)
  {
    if (unlikely (count > len))
      count = len;
    len -= count;
    arrayZ += count;
    return *this;
  }
B
Behdad Esfahbod 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  hb_array_t<Type> & operator -= (unsigned int count)
  {
    if (unlikely (count > len))
      count = len;
    len -= count;
    return *this;
  }
  hb_array_t<Type> & operator ++ (void) { *this += 1; }
  hb_array_t<Type> & operator -- (void) { *this -= 1; }
  hb_array_t<Type> operator + (unsigned int count)
  { hb_array_t<Type> copy (*this); *this += count; return copy; }
  hb_array_t<Type> operator - (unsigned int count)
  { hb_array_t<Type> copy (*this); *this -= count; return copy; }
  hb_array_t<Type>  operator ++ (int)
  { hb_array_t<Type> copy (*this); ++*this; return copy; }
  hb_array_t<Type>  operator -- (int)
  { hb_array_t<Type> copy (*this); --*this; return copy; }
B
Behdad Esfahbod 已提交
91

B
Behdad Esfahbod 已提交
92 93 94
  /*
   * Compare, Sort, and Search.
   */
95

B
Behdad Esfahbod 已提交
96 97
  /* Note: our compare is NOT lexicographic; it also does NOT call Type::cmp. */
  int cmp (const hb_array_t<Type> &a) const
98
  {
B
Behdad Esfahbod 已提交
99 100 101 102 103 104 105 106 107
    if (len != a.len)
      return (int) a.len - (int) len;
    return hb_memcmp (a.arrayZ, arrayZ, get_size ());
  }
  static int cmp (const void *pa, const void *pb)
  {
    hb_array_t<Type> *a = (hb_array_t<Type> *) pa;
    hb_array_t<Type> *b = (hb_array_t<Type> *) pb;
    return b->cmp (*a);
108 109 110
  }

  template <typename T>
111
  Type *lsearch (const T &x, Type *not_found = nullptr)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  {
    unsigned int count = len;
    for (unsigned int i = 0; i < count; i++)
      if (!this->arrayZ[i].cmp (x))
	return &this->arrayZ[i];
    return not_found;
  }
  template <typename T>
  const Type *lsearch (const T &x, const Type *not_found = nullptr) const
  {
    unsigned int count = len;
    for (unsigned int i = 0; i < count; i++)
      if (!this->arrayZ[i].cmp (x))
	return &this->arrayZ[i];
    return not_found;
  }

129
  hb_sorted_array_t<Type> qsort (int (*cmp_)(const void*, const void*))
130
  {
131
    ::qsort (arrayZ, len, item_size, cmp_);
132 133 134 135
    return hb_sorted_array_t<Type> (*this);
  }
  hb_sorted_array_t<Type> qsort (void)
  {
136
    ::qsort (arrayZ, len, item_size, Type::cmp);
137 138 139 140 141 142
    return hb_sorted_array_t<Type> (*this);
  }
  void qsort (unsigned int start, unsigned int end)
  {
    end = MIN (end, len);
    assert (start <= end);
143
    ::qsort (arrayZ + start, end - start, item_size, Type::cmp);
144 145
  }

B
Behdad Esfahbod 已提交
146 147 148
  /*
   * Other methods.
   */
149

B
Behdad Esfahbod 已提交
150 151 152
  unsigned int get_size (void) const { return len * item_size; }

  hb_array_t<Type> sub_array (unsigned int start_offset = 0, unsigned int *seg_count = nullptr /* IN/OUT */) const
153
  {
B
Behdad Esfahbod 已提交
154 155 156 157 158 159 160 161 162 163 164
    if (!start_offset && !seg_count)
      return *this;

    unsigned int count = len;
    if (unlikely (start_offset > count))
      count = 0;
    else
      count -= start_offset;
    if (seg_count)
      count = *seg_count = MIN (count, *seg_count);
    return hb_array_t<Type> (arrayZ + start_offset, count);
165
  }
B
Behdad Esfahbod 已提交
166 167 168 169 170 171
  hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int seg_count) const
  { return sub_array (start_offset, &seg_count); }

  /* Only call if you allocated the underlying array using malloc() or similar. */
  void free (void)
  { ::free ((void *) arrayZ); arrayZ = nullptr; len = 0; }
172

173 174 175 176
  template <typename hb_sanitize_context_t>
  bool sanitize (hb_sanitize_context_t *c) const
  { return c->check_array (arrayZ, len); }

B
Behdad Esfahbod 已提交
177 178 179 180
  /*
   * Members
   */

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  public:
  Type *arrayZ;
  unsigned int len;
};
template <typename T>
inline hb_array_t<T> hb_array (T *array, unsigned int len)
{ return hb_array_t<T> (array, len); }


enum hb_bfind_not_found_t
{
  HB_BFIND_NOT_FOUND_DONT_STORE,
  HB_BFIND_NOT_FOUND_STORE,
  HB_BFIND_NOT_FOUND_STORE_CLOSEST,
};

template <typename Type>
struct hb_sorted_array_t : hb_array_t<Type>
{
  hb_sorted_array_t (void) : hb_array_t<Type> () {}
  hb_sorted_array_t (const hb_array_t<Type> &o) : hb_array_t<Type> (o) {}
  hb_sorted_array_t (Type *array_, unsigned int len_) : hb_array_t<Type> (array_, len_) {}

  hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int *seg_count /* IN/OUT */) const
  { return hb_sorted_array_t<Type> (((const hb_array_t<Type> *) (this))->sub_array (start_offset, seg_count)); }
  hb_sorted_array_t<Type> sub_array (unsigned int start_offset, unsigned int seg_count) const
  { return sub_array (start_offset, &seg_count); }

  template <typename T>
  Type *bsearch (const T &x, Type *not_found = nullptr)
  {
    unsigned int i;
    return bfind (x, &i) ? &this->arrayZ[i] : not_found;
  }
  template <typename T>
  const Type *bsearch (const T &x, const Type *not_found = nullptr) const
  {
    unsigned int i;
    return bfind (x, &i) ? &this->arrayZ[i] : not_found;
  }
  template <typename T>
  bool bfind (const T &x, unsigned int *i = nullptr,
		     hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
		     unsigned int to_store = (unsigned int) -1) const
  {
    int min = 0, max = (int) this->len - 1;
    const Type *array = this->arrayZ;
    while (min <= max)
    {
      int mid = ((unsigned int) min + (unsigned int) max) / 2;
      int c = array[mid].cmp (x);
      if (c < 0)
        max = mid - 1;
      else if (c > 0)
        min = mid + 1;
      else
      {
	if (i)
	  *i = mid;
	return true;
      }
    }
    if (i)
    {
      switch (not_found)
      {
	case HB_BFIND_NOT_FOUND_DONT_STORE:
	  break;

	case HB_BFIND_NOT_FOUND_STORE:
	  *i = to_store;
	  break;

	case HB_BFIND_NOT_FOUND_STORE_CLOSEST:
	  if (max < 0 || (max < (int) this->len && array[max].cmp (x) > 0))
	    max++;
	  *i = max;
	  break;
      }
    }
    return false;
  }
};
template <typename T>
inline hb_sorted_array_t<T> hb_sorted_array (T *array, unsigned int len)
{ return hb_sorted_array_t<T> (array, len); }


269 270
typedef hb_array_t<const char> hb_bytes_t;

271
template <typename Type> struct Supplier : hb_array_t<const Type>
272
{
273 274
  Supplier (const Type *array, unsigned int len) : hb_array_t<const Type> (array, len) {}
  Supplier (hb_array_t<const Type> v) : hb_array_t<const Type> (v) {}
275 276 277
};


278
#endif /* HB_ARRAY_HH */