]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/array.icc
807382880e61624d90875ff9f325dec28de9c5d6
[lilypond.git] / flower / include / array.icc
1 /*
2   (c) Han-Wen Nienhuys 1995,96,97,98
3
4   Distributed under GNU GPL
5 */
6
7
8 #if 0
9 #include "array.hh"
10 #ifdef INLINE
11 #undef INLINE
12 #endif
13
14 #define INLINE
15 #endif
16
17 /*
18  functions with loops don't inline
19  */
20
21 template<class T> INLINE void 
22 arrcpy (T*dest, T*src, int count)
23 {
24   for (int i_shadows_local=0; i_shadows_local < count ; i_shadows_local++)
25     *dest++ = *src++;
26 }
27
28 template<class T> INLINE void
29 Array<T>::insert (T k, int j) 
30 {
31   assert (j >=0 && j<= size_);
32   set_size (size_+1);
33   for (int i=size_-1; i > j; i--)
34     array_p_[i] = array_p_[i-1];
35   array_p_[j] = k;
36 }
37
38 template<class T> INLINE void
39 Array<T>::sort (int (*compare)(T const&,T const&),
40            int lower = -1, int upper = -1) 
41 {
42   if (lower < 0) 
43     {
44       lower = 0 ;
45       upper = size () - 1;
46     }
47   if (lower >= upper)
48     return;
49   swap (lower, (lower+upper)/2);
50   int last = lower;
51   for (int i= lower +1; i <= upper; i++)
52     if (compare (array_p_[i], array_p_[lower]) < 0)
53       swap (++last,i);
54   swap (lower, last);
55   sort (compare, lower, last-1);
56   sort (compare, last+1, upper);
57 }
58
59 template<class T> INLINE void
60 Array<T>::reverse () 
61 {
62   int h = size_/2;
63   for (int i =0,j = size_-1; i < h; i++,j--)
64     swap (i,j);
65 }
66
67 template<class T> INLINE
68 void
69 Array<T>::OK() const
70 {
71   assert (max_ >= size_ && size_ >=0);
72   if (max_) assert (array_p_);
73 }
74
75 template<class T> INLINE
76 T *
77 Array<T>::remove_array_p ()
78 {
79   T * p = array_p_;
80   size_ = 0;
81   max_ = 0;
82   array_p_ =0;
83   return p;
84 }
85
86 template<class T> INLINE
87 Array<T>
88 Array<T>::slice (int lower, int upper)
89 {
90   assert (lower >= 0 && lower <=upper&& upper <= size_);
91   Array<T> r;
92   int s =upper-lower;
93   r.set_size (s);
94   arrcpy (r.array_p_, array_p_  + lower, s);
95   return r;
96 }
97