]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/array.icc
47636d335493a42fc522783127bbf6352e47162d
[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 const* src, int count)
23 {
24   for (int i_shadows_local=0; i_shadows_local < count ; i_shadows_local++)
25 #ifdef __powerpc__
26     {
27       /*
28         urg: wierd egcs-1.1.2-12c (stock LinuxPPC R5) bug on ppc
29         bug report filed
30         fixed in egcs-1.1.2-12f
31         ftp://dev.linuxppc.org/users/fsirl/R5/RPMS/ppc/ 
32       */
33       *dest = *src;
34       dest++, src++;
35     }
36 #else
37     *dest++ = *src++;
38 #endif
39 }
40
41 template<class T> INLINE void
42 Array<T>::insert (T k, int j) 
43 {
44   assert (j >=0 && j<= size_);
45   set_size (size_+1);
46   for (int i=size_-1; i > j; i--)
47     array_p_[i] = array_p_[i-1];
48   array_p_[j] = k;
49 }
50
51 template<class T> INLINE void
52 Array<T>::sort (int (*compare) (T const&,T const&), int lower, int upper)
53 {
54   if (lower < 0) 
55     {
56       lower = 0 ;
57       upper = size () - 1;
58     }
59   if (lower >= upper)
60     return;
61   swap (lower, (lower+upper)/2);
62   int last = lower;
63   for (int i= lower +1; i <= upper; i++)
64     if (compare (array_p_[i], array_p_[lower]) < 0)
65       swap (++last,i);
66   swap (lower, last);
67   sort (compare, lower, last-1);
68   sort (compare, last+1, upper);
69 }
70
71 template<class T> INLINE void
72 Array<T>::reverse () 
73 {
74   int h = size_/2;
75   for (int i =0,j = size_-1; i < h; i++,j--)
76     swap (i,j);
77 }
78
79 template<class T> INLINE
80 void
81 Array<T>::OK () const
82 {
83   assert (max_ >= size_ && size_ >=0);
84   if (max_) assert (array_p_);
85 }
86
87 template<class T> INLINE
88 T *
89 Array<T>::remove_array_p ()
90 {
91   T * p = array_p_;
92   size_ = 0;
93   max_ = 0;
94   array_p_ =0;
95   return p;
96 }
97
98 template<class T> INLINE
99 Array<T>
100 Array<T>::slice (int lower, int upper) const
101 {
102   assert (lower >= 0 && lower <=upper&& upper <= size_);
103   Array<T> r;
104   int s =upper-lower;
105   r.set_size (s);
106   arrcpy (r.array_p_, array_p_  + lower, s);
107   return r;
108 }
109
110
111 /*
112   lookup with binsearch, return array index.
113 */
114 template<class T>
115 int
116 binary_search (Array<T> const &table,
117                T const &key, int (*compare) (T const&, T const &),
118                int lo = 0,
119                int hi = -1
120                ) 
121 {
122   int cmp;
123   int result;
124   if (hi < 0)
125     hi = table.size ();
126
127   /* binary search */
128   do
129   {
130       cmp = (lo + hi) / 2;
131
132       result = (*compare)  (key, table[cmp]);
133
134       if (result < 0)
135           hi = cmp;
136       else
137           lo = cmp;
138     }
139   while (hi - lo > 1);
140   if (! (*compare) (key, table[lo]))
141     {
142       return lo;
143     }
144   else
145     return -1;              /* not found */
146 }
147