]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/parray.hh
release: 1.1.18
[lilypond.git] / flower / include / parray.hh
1 /*
2   parray.hh -- declare Pointer_array
3
4   source file of the Flower Library
5
6   (c)  1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #ifndef PARRAY_HH
11 #define PARRAY_HH
12
13 #include "array.hh"
14
15 /**
16   an array of pointers.
17
18   TODO
19   should init to 0. Derive from Array<void*>? 
20  */
21 template<class T>
22 class Link_array : public Array<T*>
23 {
24   static default_compare (T *const& p1, T  *const&p2) {
25     /* can't do p1 -p2, since T might be an incomplete type */
26     if (p1 < p2)
27       return -1 ;
28     if (p2 < p1)
29       return 1;
30     return 0;
31   }
32 public:
33   void substitute (T *old, T*new_l)
34     {
35       int i;
36       while ((i = find_i (old)) >=0) 
37         if (new_l)
38           elem (i) =new_l;
39         else
40           del (i);
41     }
42   void unordered_substitute (T* old, T * new_l)
43     {
44       int i;
45       while ((i = find_i (old)) >=0) 
46         if (new_l)
47           elem (i) =new_l;
48         else {
49           unordered_del (i);
50         }
51     
52     }
53   void default_sort() {
54     sort (default_compare);
55   }
56   void uniq() {
57     Link_array<T> l_arr;
58     for (int i=0; i < size(); i++) 
59       if (!i || elem (i-1) != elem (i))
60         l_arr.push (elem (i)); 
61     *this = l_arr;
62   }
63
64   int find_i (T const * t) const {
65     for (int i=0; i < size(); i++)
66       if (elem (i) == t)
67         return i;
68     return -1;
69   }
70   T *find_l (T const *t) const
71     {
72       int i = find_i (t);
73       if (i >= 0)
74         return elem (i);
75       else
76         return 0;
77     }
78 };
79
80 template<class T, class V>
81 Link_array<T>
82 typecast_array (Link_array<V> const &a, T * /* dummy */ )
83 {
84   Link_array<T> ret;
85   for (int i=a.size (); i-- ; )
86         ret.push (dynamic_cast<T*> (a[i]));     // ugh?
87   return ret;
88 }
89
90
91 #endif // PARRAY_HH
92