hb-array.hh 10.6 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
/*
 * 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"
B
Behdad Esfahbod 已提交
31
#include "hb-algs.hh"
32 33
#include "hb-iter.hh"
#include "hb-null.hh"
34 35 36 37 38 39


template <typename Type>
struct hb_sorted_array_t;

template <typename Type>
40
struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
41
{
B
Behdad Esfahbod 已提交
42 43 44
  /*
   * Constructors.
   */
D
David Corbett 已提交
45 46
  hb_array_t () : arrayZ (nullptr), length (0), backwards_length (0) {}
  hb_array_t (Type *array_, unsigned int length_) : arrayZ (array_), length (length_), backwards_length (0) {}
B
Behdad Esfahbod 已提交
47
  template <unsigned int length_>
D
David Corbett 已提交
48
  hb_array_t (Type (&array_)[length_]) : arrayZ (array_), length (length_), backwards_length (0) {}
49

50
  template <typename U,
51
	    hb_enable_if (hb_is_cr_convertible(U, Type))>
52 53
  hb_array_t (const hb_array_t<U> &o) :
    hb_iter_with_fallback_t<hb_array_t<Type>, Type&> (),
D
David Corbett 已提交
54
    arrayZ (o.arrayZ), length (o.length), backwards_length (o.backwards_length) {}
55
  template <typename U,
56
	    hb_enable_if (hb_is_cr_convertible(U, Type))>
57
  hb_array_t& operator = (const hb_array_t<U> &o)
D
David Corbett 已提交
58
  { arrayZ = o.arrayZ; length = o.length; backwards_length = o.backwards_length; return *this; }
59

B
Behdad Esfahbod 已提交
60
  /*
B
Behdad Esfahbod 已提交
61
   * Iterator implementation.
B
Behdad Esfahbod 已提交
62
   */
63
  typedef Type& __item_t__;
64
  static constexpr bool is_random_access_iterator = true;
B
Behdad Esfahbod 已提交
65
  Type& __item_at__ (unsigned i) const
66
  {
B
Behdad Esfahbod 已提交
67
    if (unlikely (i >= length)) return CrapOrNull (Type);
68 69
    return arrayZ[i];
  }
B
Behdad Esfahbod 已提交
70
  void __forward__ (unsigned n)
B
Behdad Esfahbod 已提交
71
  {
B
Behdad Esfahbod 已提交
72 73 74
    if (unlikely (n > length))
      n = length;
    length -= n;
D
David Corbett 已提交
75
    backwards_length += n;
B
Behdad Esfahbod 已提交
76
    arrayZ += n;
B
Behdad Esfahbod 已提交
77
  }
B
Behdad Esfahbod 已提交
78
  void __rewind__ (unsigned n)
B
Behdad Esfahbod 已提交
79
  {
D
David Corbett 已提交
80 81 82 83 84
    if (unlikely (n > backwards_length))
      n = backwards_length;
    length += n;
    backwards_length -= n;
    arrayZ -= n;
B
Behdad Esfahbod 已提交
85
  }
B
Behdad Esfahbod 已提交
86
  unsigned __len__ () const { return length; }
B
Behdad Esfahbod 已提交
87 88 89 90 91
  /* Ouch. The operator== compares the contents of the array.  For range-based for loops,
   * it's best if we can just compare arrayZ, though comparing contents is still fast,
   * but also would require that Type has operator==.  As such, we optimize this operator
   * for range-based for loop and just compare arrayZ.  No need to compare length, as we
   * assume we're only compared to .end(). */
92
  bool operator != (const hb_array_t& o) const
B
Behdad Esfahbod 已提交
93
  { return arrayZ != o.arrayZ; }
B
Behdad Esfahbod 已提交
94 95 96 97 98 99

  /* Extra operators.
   */
  Type * operator & () const { return arrayZ; }
  operator hb_array_t<const Type> () { return hb_array_t<const Type> (arrayZ, length); }
  template <typename T> operator T * () const { return arrayZ; }
B
Behdad Esfahbod 已提交
100

B
Behdad Esfahbod 已提交
101 102
  HB_INTERNAL bool operator == (const hb_array_t &o) const;
  HB_INTERNAL uint32_t hash () const;
B
Behdad Esfahbod 已提交
103

B
Behdad Esfahbod 已提交
104 105 106
  /*
   * Compare, Sort, and Search.
   */
107

B
Behdad Esfahbod 已提交
108 109
  /* Note: our compare is NOT lexicographic; it also does NOT call Type::cmp. */
  int cmp (const hb_array_t<Type> &a) const
110
  {
111 112
    if (length != a.length)
      return (int) a.length - (int) length;
B
Behdad Esfahbod 已提交
113 114
    return hb_memcmp (a.arrayZ, arrayZ, get_size ());
  }
B
Behdad Esfahbod 已提交
115
  HB_INTERNAL static int cmp (const void *pa, const void *pb)
B
Behdad Esfahbod 已提交
116 117 118 119
  {
    hb_array_t<Type> *a = (hb_array_t<Type> *) pa;
    hb_array_t<Type> *b = (hb_array_t<Type> *) pb;
    return b->cmp (*a);
120 121 122
  }

  template <typename T>
123
  Type *lsearch (const T &x, Type *not_found = nullptr)
124
  {
125
    unsigned int count = length;
126 127 128 129 130 131 132 133
    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
  {
134
    unsigned int count = length;
135 136 137 138 139 140
    for (unsigned int i = 0; i < count; i++)
      if (!this->arrayZ[i].cmp (x))
	return &this->arrayZ[i];
    return not_found;
  }

141
  hb_sorted_array_t<Type> qsort (int (*cmp_)(const void*, const void*))
142
  {
143
    if (likely (length))
144
      hb_qsort (arrayZ, length, this->item_size, cmp_);
145 146
    return hb_sorted_array_t<Type> (*this);
  }
147
  hb_sorted_array_t<Type> qsort ()
148
  {
149
    if (likely (length))
150
      hb_qsort (arrayZ, length, this->item_size, Type::cmp);
151 152 153 154
    return hb_sorted_array_t<Type> (*this);
  }
  void qsort (unsigned int start, unsigned int end)
  {
155
    end = hb_min (end, length);
156
    assert (start <= end);
157
    if (likely (start < end))
158
      hb_qsort (arrayZ + start, end - start, this->item_size, Type::cmp);
159 160
  }

B
Behdad Esfahbod 已提交
161 162 163
  /*
   * Other methods.
   */
164

B
Behdad Esfahbod 已提交
165
  unsigned int get_size () const { return length * this->item_size; }
B
Behdad Esfahbod 已提交
166 167

  hb_array_t<Type> sub_array (unsigned int start_offset = 0, unsigned int *seg_count = nullptr /* IN/OUT */) const
168
  {
B
Behdad Esfahbod 已提交
169 170 171
    if (!start_offset && !seg_count)
      return *this;

172
    unsigned int count = length;
B
Behdad Esfahbod 已提交
173 174 175 176 177
    if (unlikely (start_offset > count))
      count = 0;
    else
      count -= start_offset;
    if (seg_count)
178
      count = *seg_count = hb_min (count, *seg_count);
B
Behdad Esfahbod 已提交
179
    return hb_array_t<Type> (arrayZ + start_offset, count);
180
  }
B
Behdad Esfahbod 已提交
181 182 183 184
  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. */
185
  void free ()
186
  { ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; }
187

B
Behdad Esfahbod 已提交
188 189 190 191
  template <typename hb_serialize_context_t>
  hb_array_t copy (hb_serialize_context_t *c) const
  {
    TRACE_SERIALIZE (this);
192
    auto* out = c->start_embed (arrayZ);
B
Behdad Esfahbod 已提交
193 194 195 196 197 198
    if (unlikely (!c->extend_size (out, get_size ()))) return_trace (hb_array_t ());
    for (unsigned i = 0; i < length; i++)
      out[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */
    return_trace (hb_array_t (out, length));
  }

199 200
  template <typename hb_sanitize_context_t>
  bool sanitize (hb_sanitize_context_t *c) const
201
  { return c->check_array (arrayZ, length); }
202

B
Behdad Esfahbod 已提交
203 204 205 206
  /*
   * Members
   */

207 208
  public:
  Type *arrayZ;
209
  unsigned int length;
D
David Corbett 已提交
210
  unsigned int backwards_length;
211
};
212
template <typename T> inline hb_array_t<T>
213 214 215 216
hb_array (T *array, unsigned int length)
{ return hb_array_t<T> (array, length); }
template <typename T, unsigned int length_> inline hb_array_t<T>
hb_array (T (&array_)[length_])
217
{ return hb_array_t<T> (array_); }
218 219 220 221 222 223 224 225 226

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>
B
Behdad Esfahbod 已提交
227
struct hb_sorted_array_t :
B
Behdad Esfahbod 已提交
228
	hb_iter_t<hb_sorted_array_t<Type>, Type&>,
229
	hb_array_t<Type>
230
{
B
Behdad Esfahbod 已提交
231
  typedef hb_iter_t<hb_sorted_array_t<Type>, Type&> iter_base_t;
232
  HB_ITER_USING (iter_base_t);
233
  static constexpr bool is_random_access_iterator = true;
234
  static constexpr bool is_sorted_iterator = true;
235

236
  hb_sorted_array_t () : hb_array_t<Type> () {}
237
  hb_sorted_array_t (Type *array_, unsigned int length_) : hb_array_t<Type> (array_, length_) {}
B
Behdad Esfahbod 已提交
238 239
  template <unsigned int length_>
  hb_sorted_array_t (Type (&array_)[length_]) : hb_array_t<Type> (array_) {}
240

241
  template <typename U,
242
	    hb_enable_if (hb_is_cr_convertible(U, Type))>
243 244 245 246
  hb_sorted_array_t (const hb_array_t<U> &o) :
    hb_iter_t<hb_sorted_array_t<Type>, Type&> (),
    hb_array_t<Type> (o) {}
  template <typename U,
247
	    hb_enable_if (hb_is_cr_convertible(U, Type))>
248 249 250
  hb_sorted_array_t& operator = (const hb_array_t<U> &o)
  { hb_array_t<Type> (*this) = o; return *this; }

251 252 253 254
  /* Iterator implementation. */
  bool operator != (const hb_sorted_array_t& o) const
  { return this->arrayZ != o.arrayZ || this->length != o.length; }

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  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
  {
277
    int min = 0, max = (int) this->length - 1;
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    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:
306
	  if (max < 0 || (max < (int) this->length && array[max].cmp (x) > 0))
307 308 309 310 311 312 313 314
	    max++;
	  *i = max;
	  break;
      }
    }
    return false;
  }
};
315
template <typename T> inline hb_sorted_array_t<T>
316 317 318 319
hb_sorted_array (T *array, unsigned int length)
{ return hb_sorted_array_t<T> (array, length); }
template <typename T, unsigned int length_> inline hb_sorted_array_t<T>
hb_sorted_array (T (&array_)[length_])
320
{ return hb_sorted_array_t<T> (array_); }
321

322 323 324 325 326
template <typename T>
bool hb_array_t<T>::operator == (const hb_array_t<T> &o) const
{
  return length == o.length &&
  + hb_zip (*this, o)
327
  | hb_map ([] (hb_pair_t<T&, T&> &&_) { return _.first == _.second; })
328 329 330
  | hb_all
  ;
}
B
Behdad Esfahbod 已提交
331 332 333
template <typename T>
uint32_t hb_array_t<T>::hash () const
{
334
  return
335
  + hb_iter (*this)
336
  | hb_map (hb_hash)
337
  | hb_reduce ([] (uint32_t a, uint32_t b) { return a * 31 + b; }, 0)
338
  ;
B
Behdad Esfahbod 已提交
339
}
340

341
typedef hb_array_t<const char> hb_bytes_t;
342
typedef hb_array_t<const unsigned char> hb_ubytes_t;
343

344
/* TODO Specialize opeator==/hash() for hb_bytes_t and hb_ubytes_t. */
B
Behdad Esfahbod 已提交
345 346
//template <>
//uint32_t hb_array_t<const char>::hash () const { return 0; }
347

348
#endif /* HB_ARRAY_HH */