X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=flower%2Finclude%2Farray.hh;h=d25f34c346e995460ac31f59ecc8c99dc005c9be;hb=586d833b4fb7a336524eb0929d693095a802fa26;hp=9a5b2e40266d8260efc3fbfb070109592ab6af02;hpb=c2adb6159c233a9ef3f8a22ab25342e15bd10c8b;p=lilypond.git diff --git a/flower/include/array.hh b/flower/include/array.hh index 9a5b2e4026..d25f34c346 100644 --- a/flower/include/array.hh +++ b/flower/include/array.hh @@ -17,6 +17,12 @@ using namespace std; #define INLINE inline #endif +#if !STD_VECTOR +#define index_assert(i) (assert (i >= 0 && i < size_)); +#else +#define index_assert(i) (assert (i != VPOS && i < size_)); +#endif + namespace std { /// copy a bare (C-)array from #src# to #dest# sized #count# template void arrcpy (T *dest, T const *src, int count); @@ -38,7 +44,8 @@ template void arrcpy (T *dest, T const *src, int count); template class Array { -protected: +public: + /// maximum length of array. vsize max_; @@ -60,6 +67,9 @@ protected: public: /* std::vector interface */ + typedef T* iterator; + typedef T const* const_iterator; + Array () { array_ = 0; @@ -73,6 +83,8 @@ public: max_ = size_ = src.size_; } + Array (const_iterator begin, const_iterator end); + T const &back () const { return (*this)[size_ - 1]; @@ -115,7 +127,11 @@ public: return array_; } - typedef T* iterator; + T const* + data () const + { + return array_; + } iterator begin () @@ -123,7 +139,7 @@ public: return data (); } - iterator const + const_iterator begin () const { return data (); @@ -135,7 +151,7 @@ public: return data () + size_; } - iterator const + const_iterator end () const { return data () + size_; @@ -143,36 +159,52 @@ public: void clear () { - resize (0); + //resize (0); + size_ = 0; + } + + T & + at (vsize i) + { + return (*this)[i]; + } + + T const & + at (vsize i) const + { + return (*this)[i]; } - /* std::vector uses unchecked variant for [] */ T &operator [] (vsize i) { - return elem_ref (i); + return array_[i]; } - /* std::vector uses unchecked variant for [] */ T const &operator [] (vsize i) const { - return elem_ref (i); + return array_[i]; } iterator erase (iterator p) { vsize i = p - data (); -#if !STD_VECTOR - assert (i >= 0 && i < size_); -#else - assert (i < size_); -#endif + index_assert (i); arrcpy (array_ + i, array_ + i + 1, size_ - i - 1); size_--; return p; } + /// add to the end of array + void push_back (T x) + { + if (size_ == max_) + remax (2 * max_ + 1); + // T::operator= (T &) is called here. Safe to use with automatic + // vars + array_[size_++] = x; + } /* Flower intererface */ @@ -230,43 +262,6 @@ public: T *remove_array (); - /// access element - T &elem_ref (vsize i) const - { -#if !STD_VECTOR - assert (i >= 0 && i < size_); -#else - assert (i < size_); -#endif - return ((T *)array_)[i]; - } - /// access element - T elem (vsize i) const - { - return elem_ref (i); - } - - /// add to the end of array - void push_back (T x) - { - if (size_ == max_) - remax (2 * max_ + 1); - - // T::operator= (T &) is called here. Safe to use with automatic - // vars - array_[size_++] = x; - } -#if 0 - /// remove and return last entry - T pop () - { - assert (!empty ()); - T l = top (0); - resize (size () - 1); - return l; - } -#endif - /// access last entry T &top (vsize j) { @@ -284,7 +279,7 @@ public: if (dir == 1) return top (idx); else - return elem_ref (idx); + return at (idx); } T boundary (int dir, vsize idx) const { @@ -292,7 +287,7 @@ public: if (dir == 1) return top (idx); else - return elem (idx); + return at (idx); } void swap (vsize i, vsize j) { @@ -301,37 +296,22 @@ public: (*this)[j] = t; } - void insert (T k, vsize j); + void insert (iterator j, T k); void unordered_del (vsize i) { - elem_ref (i) = back (); + at (i) = back (); resize (size () -1); } - // quicksort. - void sort (int (*compare) (T const &, T const &), - vsize lower=VPOS, vsize upper=VPOS); void concat (Array const &src) { vsize s = size_; resize (size_ + src.size_); arrcpy (array_ + s, src.array_, src.size_); } - Array slice (vsize lower, vsize upper) const; void reverse (); }; -template -int default_compare (T const &a, T const &b) -{ - if (a < b) - return -1; - else if (a > b) - return 1; - else - return 0; -} - #include "array.icc" }