Imported Upstream version 2.6.4
[platform/upstream/harfbuzz.git] / src / hb-vector.hh
index 2fd739b..7b150fb 100644 (file)
@@ -38,103 +38,141 @@ struct hb_vector_t
   typedef Type item_t;
   static constexpr unsigned item_size = hb_static_size (Type);
 
-  HB_NO_COPY_ASSIGN_TEMPLATE (hb_vector_t, Type);
   hb_vector_t ()  { init (); }
+  hb_vector_t (const hb_vector_t &o)
+  {
+    init ();
+    alloc (o.length);
+    hb_copy (o, *this);
+  }
+  hb_vector_t (hb_vector_t &&o)
+  {
+    allocated = o.allocated;
+    length = o.length;
+    arrayZ = o.arrayZ;
+    o.init ();
+  }
   ~hb_vector_t () { fini (); }
 
-  unsigned int length;
   private:
   int allocated; /* == -1 means allocation failed. */
-  Type *arrayZ_;
   public:
+  unsigned int length;
+  public:
+  Type *arrayZ;
 
   void init ()
   {
     allocated = length = 0;
-    arrayZ_ = nullptr;
+    arrayZ = nullptr;
   }
 
   void fini ()
   {
-    if (arrayZ_)
-      free (arrayZ_);
+    free (arrayZ);
     init ();
   }
   void fini_deep ()
   {
-    Type *array = arrayZ();
     unsigned int count = length;
     for (unsigned int i = 0; i < count; i++)
-      array[i].fini ();
+      arrayZ[i].fini ();
+    fini ();
+  }
+
+  void reset () { resize (0); }
+
+  hb_vector_t& operator = (const hb_vector_t &o)
+  {
+    reset ();
+    alloc (o.length);
+    hb_copy (o, *this);
+    return *this;
+  }
+  hb_vector_t& operator = (hb_vector_t &&o)
+  {
     fini ();
+    allocated = o.allocated;
+    length = o.length;
+    arrayZ = o.arrayZ;
+    o.init ();
+    return *this;
   }
 
-  const Type * arrayZ () const { return arrayZ_; }
-        Type * arrayZ ()       { return arrayZ_; }
+  hb_bytes_t as_bytes () const
+  { return hb_bytes_t ((const char *) arrayZ, length * item_size); }
+
+  bool operator == (const hb_vector_t &o) const { return as_array () == o.as_array (); }
+  bool operator != (const hb_vector_t &o) const { return !(*this == o); }
+  uint32_t hash () const { return as_array ().hash (); }
 
   Type& operator [] (int i_)
   {
     unsigned int i = (unsigned int) i_;
     if (unlikely (i >= length))
       return Crap (Type);
-    return arrayZ()[i];
+    return arrayZ[i];
   }
   const Type& operator [] (int i_) const
   {
     unsigned int i = (unsigned int) i_;
     if (unlikely (i >= length))
       return Null(Type);
-    return arrayZ()[i];
+    return arrayZ[i];
   }
 
-  explicit_operator bool () const { return length; }
+  Type& tail () { return (*this)[length - 1]; }
+  const Type& tail () const { return (*this)[length - 1]; }
 
-  hb_array_t<Type> as_array ()
-  { return hb_array (arrayZ(), length); }
-  hb_array_t<const Type> as_array () const
-  { return hb_array (arrayZ(), length); }
+  explicit operator bool () const { return length; }
+  unsigned get_size () const { return length * item_size; }
+
+  /* Sink interface. */
+  template <typename T>
+  hb_vector_t& operator << (T&& v) { push (hb_forward<T> (v)); return *this; }
+
+  hb_array_t<      Type> as_array ()       { return hb_array (arrayZ, length); }
+  hb_array_t<const Type> as_array () const { return hb_array (arrayZ, length); }
+
+  /* Iterator. */
+  typedef hb_array_t<const Type>   iter_t;
+  typedef hb_array_t<      Type> writer_t;
+    iter_t   iter () const { return as_array (); }
+  writer_t writer ()       { return as_array (); }
+  operator   iter_t () const { return   iter (); }
+  operator writer_t ()       { return writer (); }
 
   hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int count) const
-  { return as_array ().sub_array (start_offset, count);}
+  { return as_array ().sub_array (start_offset, count); }
   hb_array_t<const Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const
-  { return as_array ().sub_array (start_offset, count);}
+  { return as_array ().sub_array (start_offset, count); }
   hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int count)
-  { return as_array ().sub_array (start_offset, count);}
+  { return as_array ().sub_array (start_offset, count); }
   hb_array_t<Type> sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
-  { return as_array ().sub_array (start_offset, count);}
+  { return as_array ().sub_array (start_offset, count); }
 
   hb_sorted_array_t<Type> as_sorted_array ()
-  { return hb_sorted_array (arrayZ(), length); }
+  { return hb_sorted_array (arrayZ, length); }
   hb_sorted_array_t<const Type> as_sorted_array () const
-  { return hb_sorted_array (arrayZ(), length); }
-
-  hb_array_t<const Type> sorted_sub_array (unsigned int start_offset, unsigned int count) const
-  { return as_sorted_array ().sorted_sub_array (start_offset, count);}
-  hb_array_t<const Type> sorted_sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */) const
-  { return as_sorted_array ().sorted_sub_array (start_offset, count);}
-  hb_array_t<Type> sorted_sub_array (unsigned int start_offset, unsigned int count)
-  { return as_sorted_array ().sorted_sub_array (start_offset, count);}
-  hb_array_t<Type> sorted_sub_array (unsigned int start_offset, unsigned int *count = nullptr /* IN/OUT */)
-  { return as_sorted_array ().sorted_sub_array (start_offset, count);}
+  { return hb_sorted_array (arrayZ, length); }
 
-  template <typename T> explicit_operator T * () { return arrayZ(); }
-  template <typename T> explicit_operator const T * () const { return arrayZ(); }
-  operator hb_array_t<Type> ()             { return as_array (); }
-  operator hb_array_t<const Type> () const { return as_array (); }
+  template <typename T> explicit operator T * () { return arrayZ; }
+  template <typename T> explicit operator const T * () const { return arrayZ; }
 
-  Type * operator  + (unsigned int i) { return arrayZ() + i; }
-  const Type * operator  + (unsigned int i) const { return arrayZ() + i; }
+  Type * operator  + (unsigned int i) { return arrayZ + i; }
+  const Type * operator  + (unsigned int i) const { return arrayZ + i; }
 
   Type *push ()
   {
     if (unlikely (!resize (length + 1)))
       return &Crap(Type);
-    return &arrayZ()[length - 1];
+    return &arrayZ[length - 1];
   }
-  Type *push (const Type& v)
+  template <typename T>
+  Type *push (T&& v)
   {
     Type *p = push ();
-    *p = v;
+    *p = hb_forward<T> (v);
     return p;
   }
 
@@ -161,7 +199,7 @@ struct hb_vector_t
       (new_allocated < (unsigned) allocated) ||
       hb_unsigned_mul_overflows (new_allocated, sizeof (Type));
     if (likely (!overflows))
-      new_array = (Type *) realloc (arrayZ_, new_allocated * sizeof (Type));
+      new_array = (Type *) realloc (arrayZ, new_allocated * sizeof (Type));
 
     if (unlikely (!new_array))
     {
@@ -169,7 +207,7 @@ struct hb_vector_t
       return false;
     }
 
-    arrayZ_ = new_array;
+    arrayZ = new_array;
     allocated = new_allocated;
 
     return true;
@@ -182,25 +220,24 @@ struct hb_vector_t
       return false;
 
     if (size > length)
-      memset (arrayZ() + length, 0, (size - length) * sizeof (*arrayZ()));
+      memset (arrayZ + length, 0, (size - length) * sizeof (*arrayZ));
 
     length = size;
     return true;
   }
 
-  void pop ()
+  Type pop ()
   {
-    if (!length) return;
-    length--;
+    if (!length) return Null(Type);
+    return hb_move (arrayZ[--length]); /* Does this move actually work? */
   }
 
   void remove (unsigned int i)
   {
     if (unlikely (i >= length))
       return;
-    Type *array = arrayZ();
-    memmove (static_cast<void *> (&array[i]),
-            static_cast<void *> (&array[i + 1]),
+    memmove (static_cast<void *> (&arrayZ[i]),
+            static_cast<void *> (&arrayZ[i + 1]),
             (length - i - 1) * sizeof (Type));
     length--;
   }
@@ -215,19 +252,17 @@ struct hb_vector_t
   template <typename T>
   Type *find (T v)
   {
-    Type *array = arrayZ();
     for (unsigned int i = 0; i < length; i++)
-      if (array[i] == v)
-       return &array[i];
+      if (arrayZ[i] == v)
+       return &arrayZ[i];
     return nullptr;
   }
   template <typename T>
   const Type *find (T v) const
   {
-    const Type *array = arrayZ();
     for (unsigned int i = 0; i < length; i++)
-      if (array[i] == v)
-       return &array[i];
+      if (arrayZ[i] == v)
+       return &arrayZ[i];
     return nullptr;
   }
 
@@ -242,19 +277,34 @@ struct hb_vector_t
   template <typename T>
   const Type *lsearch (const T &x, const Type *not_found = nullptr) const
   { return as_array ().lsearch (x, not_found); }
+};
+
+template <typename Type>
+struct hb_sorted_vector_t : hb_vector_t<Type>
+{
+  hb_sorted_array_t<      Type> as_array ()       { return hb_sorted_array (this->arrayZ, this->length); }
+  hb_sorted_array_t<const Type> as_array () const { return hb_sorted_array (this->arrayZ, this->length); }
+
+  /* Iterator. */
+  typedef hb_sorted_array_t<const Type> const_iter_t;
+  typedef hb_sorted_array_t<      Type>       iter_t;
+  const_iter_t  iter () const { return as_array (); }
+  const_iter_t citer () const { return as_array (); }
+       iter_t  iter ()       { return as_array (); }
+  operator       iter_t ()       { return iter (); }
+  operator const_iter_t () const { return iter (); }
 
   template <typename T>
   Type *bsearch (const T &x, Type *not_found = nullptr)
-  { return as_sorted_array ().bsearch (x, not_found); }
+  { return as_array ().bsearch (x, not_found); }
   template <typename T>
   const Type *bsearch (const T &x, const Type *not_found = nullptr) const
-  { return as_sorted_array ().bsearch (x, not_found); }
+  { return as_array ().bsearch (x, 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
-  { return as_sorted_array ().bfind (x, i, not_found, to_store); }
+             hb_bfind_not_found_t not_found = HB_BFIND_NOT_FOUND_DONT_STORE,
+             unsigned int to_store = (unsigned int) -1) const
+  { return as_array ().bfind (x, i, not_found, to_store); }
 };
 
-
 #endif /* HB_VECTOR_HH */