add edit clear submenu/clear hard_edges, fix tessy gl segv, mask toolgui layout,...
[goodguy/cinelerra.git] / cinelerra-5.1 / guicast / linklist.h
1 #ifndef LINKLIST_H
2 #define LINKLIST_H
3
4 template<class TYPE> class List;
5
6 template<class TYPE>
7 class ListItem {
8 public:
9         TYPE *previous, *next;
10         List<TYPE> *list;
11
12         int get_item_number() { return !list ? -1 : list->number_of(this); }
13         ListItem() { list = 0;  previous = next = 0; }
14         ListItem(List<TYPE> &me) { list = me;  previous = next = 0; }
15         virtual ~ListItem() { if( list ) list->remove_pointer(this); }
16 };
17
18 template<class TYPE>
19 class List {
20         TYPE *split(int (*cmpr)(TYPE *a, TYPE *b),TYPE *l, TYPE *r);
21         static int cmpr(TYPE *a, TYPE *b) {
22                 if( *a == *b ) return 0;
23                 return *a > *b ? 1 : -1;
24         }
25 public:
26         TYPE *first, *last;
27         void remove(TYPE *item) { if(item) delete item; }
28         void remove_pointer(ListItem<TYPE> *item);
29         TYPE *append(TYPE *new_item);
30         TYPE *append() { return append(new TYPE()); }
31         TYPE *insert_before(TYPE *here, TYPE *item);
32         TYPE *insert_before(TYPE *here) { return insert_before(here, new TYPE()); }
33         TYPE *insert_after(TYPE *here, TYPE *item);
34         TYPE *insert_after(TYPE *here) { return insert_after(here, new TYPE()); }
35         int total() { TYPE *p=first; int n=0; while(p) { p=p->next; ++n; } return n; }
36         TYPE *get_item_number(int i) { TYPE *p=first;
37                 while(p && i>0) { --i; p=p->next; }
38                 return p;
39         }
40         TYPE &operator[](int i) { return *get_item_number(i); }
41         int number_of(TYPE *item) { TYPE *p=first; int i=0;
42                 while(p && p!=item) { ++i; p=p->next; }
43                 return p ? i : -1;
44         }
45         void swap(TYPE *item1, TYPE *item2);
46         void sort(TYPE *ap=0, TYPE *bp=0) { return sort(cmpr,ap,bp); }
47         void sort(int (*cmp)(TYPE *a, TYPE *b), TYPE *ap=0, TYPE *bp=0);
48         List() { first = last = 0; }
49         virtual ~List() { while(last) delete last; }
50 };
51
52 // convenience macros
53 #define PREVIOUS current->previous
54 #define NEXT current->next
55
56 template<class TYPE>
57 TYPE* List<TYPE>::append(TYPE *item)
58 {
59         item->list = this;  item->next = 0;
60         if( !last ) { item->previous = 0; first = item; }
61         else { item->previous = last;  last->next = item; }
62         return last = item;
63 }
64
65 template<class TYPE>
66 TYPE* List<TYPE>::insert_before(TYPE *here, TYPE *item)
67 {
68         if( !here || !last ) return append(item);
69         item->list = this;  item->next = here;
70         *( !(item->previous=here->previous) ? &first : &here->previous->next ) = item;
71         return here->previous = item;
72 }
73
74 template<class TYPE>
75 TYPE* List<TYPE>::insert_after(TYPE *here, TYPE *item)
76 {
77         if( !here || !last ) return append(item);
78         item->list = this;  item->previous = here;
79         *( !(item->next=here->next) ? &last : &here->next->previous ) = item;
80         return here->next = item;
81 }
82
83
84 template<class TYPE>
85 void List<TYPE>::remove_pointer(ListItem<TYPE> *item)
86 {
87         if( !item ) return;
88         TYPE *previous = item->previous, *next = item->next;
89         *( previous ? &previous->next : &first ) = next;
90         *( next ? &next->previous : &last ) = previous;
91         item->list = 0;
92 }
93
94 template<class TYPE>
95 void List<TYPE>::swap(TYPE *item1, TYPE *item2)
96 {
97         if( item1 == item2 ) return;
98         TYPE *item1_previous = item1->previous, *item1_next = item1->next;
99         TYPE *item2_previous = item2->previous, *item2_next = item2->next;
100         *( item1_previous ? &item1_previous->next : &first ) = item2;
101         *( item1_next     ? &item1_next->previous : &last  ) = item2;
102         *( item2_previous ? &item2_previous->next : &first ) = item1;
103         *( item2_next     ? &item2_next->previous : &last  ) = item1;
104         item1->previous = item2_previous == item1 ? item2 : item2_previous;
105         item1->next     = item2_next     == item1 ? item2 : item2_next;
106         item2->previous = item1_previous == item2 ? item1 : item1_previous;
107         item2->next     = item1_next     == item2 ? item1 : item1_next;
108 }
109
110 template<class TYPE>
111 TYPE *List<TYPE>::split(int (*cmpr)(TYPE *a, TYPE *b), TYPE *l, TYPE *r)
112 {
113         for(;;) {
114                 while( cmpr(r,l) >= 0 ) if( (l=l->next) == r ) return r;
115                 swap(l, r);
116                 while( cmpr(l,r) >= 0 ) if( r == (l=l->previous) ) return r;
117                 swap(l, r);
118         }
119 }
120
121 template<class TYPE>
122 void List<TYPE>::sort(int (*cmpr)(TYPE *a, TYPE *b), TYPE *ll, TYPE *rr)
123 {
124         for(;;) {
125                 TYPE *l = !ll ? first : ll->next;
126                 if( l == rr ) return;
127                 TYPE *r = !rr ? last  : rr->previous;
128                 if( l == r ) return;
129                 TYPE *m = split(cmpr, l, r);
130                 sort(cmpr, ll, m);
131                 ll = m;
132         }
133 }
134
135 #endif