hb-array.hh 9.4 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.
   */
45
  hb_array_t () : arrayZ (nullptr), length (0) {}
B
Behdad Esfahbod 已提交
46 47 48
  hb_array_t (const hb_array_t<Type> &o) :
    hb_iter_with_fallback_t<hb_array_t<Type>, Type&> (),
    arrayZ (o.arrayZ), length (o.length) {}
B
Behdad Esfahbod 已提交
49
  template <typename U = Type, hb_enable_if (hb_is_const (U))>
B
Behdad Esfahbod 已提交
50
  hb_array_t (const hb_array_t<hb_remove_const (Type)> &o) : arrayZ (o.arrayZ), length (o.length) {}
51

52
  hb_array_t (Type *array_, unsigned int length_) : arrayZ (array_), length (length_) {}
53
  template <unsigned int length_> hb_array_t (Type (&array_)[length_]) : arrayZ (array_), length (length_) {}
54

55 56 57 58 59
  template <typename U = Type, hb_enable_if (hb_is_const (U))>
  hb_array_t& operator = (const hb_array_t<hb_remove_const (Type)> &o)
  { arrayZ = o.arrayZ; length = o.length; return *this; }
  hb_array_t& operator = (const hb_array_t &o)
  { arrayZ = o.arrayZ; length = o.length; return *this; }
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 75
    if (unlikely (n > length))
      n = length;
    length -= n;
    arrayZ += n;
B
Behdad Esfahbod 已提交
76
  }
B
Behdad Esfahbod 已提交
77
  void __rewind__ (unsigned n)
B
Behdad Esfahbod 已提交
78
  {
B
Behdad Esfahbod 已提交
79 80 81
    if (unlikely (n > length))
      n = length;
    length -= n;
B
Behdad Esfahbod 已提交
82
  }
B
Behdad Esfahbod 已提交
83 84 85 86 87 88 89
  unsigned __len__ () const { return length; }

  /* 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 已提交
90

B
Behdad Esfahbod 已提交
91 92
  HB_INTERNAL bool operator == (const hb_array_t &o) const;
  HB_INTERNAL uint32_t hash () const;
B
Behdad Esfahbod 已提交
93

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

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

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

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

B
Behdad Esfahbod 已提交
151 152 153
  /*
   * Other methods.
   */
154

B
Behdad Esfahbod 已提交
155
  unsigned int get_size () const { return length * this->item_size; }
B
Behdad Esfahbod 已提交
156 157

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

162
    unsigned int count = length;
B
Behdad Esfahbod 已提交
163 164 165 166 167 168 169
    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);
170
  }
B
Behdad Esfahbod 已提交
171 172 173 174
  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. */
175
  void free ()
176
  { ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; }
177

178 179
  template <typename hb_sanitize_context_t>
  bool sanitize (hb_sanitize_context_t *c) const
180
  { return c->check_array (arrayZ, length); }
181

B
Behdad Esfahbod 已提交
182 183 184 185
  /*
   * Members
   */

186 187
  public:
  Type *arrayZ;
188
  unsigned int length;
189
};
190
template <typename T> inline hb_array_t<T>
191 192 193 194
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_])
195
{ return hb_array_t<T> (array_); }
196 197 198 199 200 201 202 203 204

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 已提交
205
struct hb_sorted_array_t :
B
Behdad Esfahbod 已提交
206
	hb_iter_t<hb_sorted_array_t<Type>, Type&>,
207
	hb_array_t<Type>
208
{
B
Behdad Esfahbod 已提交
209
  typedef hb_iter_t<hb_sorted_array_t<Type>, Type&> iter_base_t;
210
  HB_ITER_USING (iter_base_t);
211 212
  static constexpr bool is_random_access_iterator = true;
  static constexpr bool is_sorted_iterator = true;
213

214
  hb_sorted_array_t () : hb_array_t<Type> () {}
215
  hb_sorted_array_t (const hb_array_t<Type> &o) : hb_array_t<Type> (o) {}
B
Behdad Esfahbod 已提交
216
  template <typename U = Type, hb_enable_if (hb_is_const (U))>
B
Behdad Esfahbod 已提交
217
  hb_sorted_array_t (const hb_sorted_array_t<hb_remove_const (Type)> &o) : hb_array_t<Type> (o) {}
218 219
  hb_sorted_array_t (Type *array_, unsigned int length_) : hb_array_t<Type> (array_, length_) {}
  template <unsigned int length_> hb_sorted_array_t (Type (&array_)[length_]) : hb_array_t<Type> (array_) {}
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

  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
  {
243
    int min = 0, max = (int) this->length - 1;
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 269 270 271
    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:
272
	  if (max < 0 || (max < (int) this->length && array[max].cmp (x) > 0))
273 274 275 276 277 278 279 280
	    max++;
	  *i = max;
	  break;
      }
    }
    return false;
  }
};
281
template <typename T> inline hb_sorted_array_t<T>
282 283 284 285
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_])
286
{ return hb_sorted_array_t<T> (array_); }
287

288 289 290 291 292 293 294 295 296
template <typename T>
bool hb_array_t<T>::operator == (const hb_array_t<T> &o) const
{
  return length == o.length &&
  + hb_zip (*this, o)
  | hb_map ([] (hb_pair_t<T&, T&> &&_) -> bool { return _.first == _.second; })
  | hb_all
  ;
}
B
Behdad Esfahbod 已提交
297 298 299
template <typename T>
uint32_t hb_array_t<T>::hash () const
{
300
  return
301
  + hb_iter (*this)
302 303 304
  | hb_map (hb_hash)
  | hb_reduce ([] (uint32_t a, uint32_t b) -> uint32_t { return a * 31 + b; }, 0)
  ;
B
Behdad Esfahbod 已提交
305
}
306

307
typedef hb_array_t<const char> hb_bytes_t;
308
typedef hb_array_t<const unsigned char> hb_ubytes_t;
309

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

314
#endif /* HB_ARRAY_HH */