source: tags/initial/MULTI_PROBE/SoTl.hxx

Last change on this file was 2, checked in by oldcode, 24 years ago

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.3 KB
Line 
1#ifndef SOTL
2#define SOTL            // SOTL = SelfOrganising TemplateList
3
4/*
5        Copyright by Andrej Konkow 1996
6
7        User has to take care by his own on calling update_pos_no, when inserting
8        elements somewhere in the middle. If elements are inserted only at the last
9        position the pos is incremented correctly. By default the first element in the list
10        has position 1.
11        no_of_members is counted and updated automatically.
12
13  Caution:
14        This List by default is NOT a selforganizing list. This means that by default an element
15        that is being asked for is NOT put at front of the list. This only will be done while the
16        flag sotl is set to FALSE(this is default). You can change this flag with the methods
17        sotl_list() (sets the flag TRUE) and no_sotl_list() (sets the flag FALSE).
18
19        The List is created by
20                List<Typename> *instance_variable = new List<Typename>(sotl_flag);
21                                                                        ^^^^^^^
22                                                        sotl_flag is optional.
23                                                        For a normal double linked list
24                                                        this flag has not to be set. For
25                                                        a selforganizing list : TRUE
26
27        Afterwards the elements can be inserted.
28        The User of this list has to delete the elements which are inserted to the list by
29        its own.
30        The User has only have to work with the List class.
31  */
32
33#include <stdio.h>
34
35typedef char BOOL;
36typedef unsigned long positiontype;
37#define TRUE    1
38#define FALSE   0
39
40#define RELATION_GREATER        1
41#define RELATION_LESS           2
42
43
44template <class Type> class list_elem
45{
46        private:
47                positiontype            pos;
48
49
50        public:
51                list_elem<Type>         *next;
52                list_elem<Type>         *prev;
53                Type                    *elem;
54                BOOL                    isolate_list_elem();            // set next and prev links to NULL
55                                                                        // TRUE if isolation has taken place,
56                                                                        // else FALSE(for example if we're the
57                                                                        // only element
58               
59                Type                    *get_elem()                     { return elem; };
60                void                    set_elem( Type *el )            { elem = el; };
61                positiontype            get_pos()                       { return pos; };                       
62                void                    set_pos(positiontype p)         { pos = p; };
63                list_elem<Type>         *get_next()                     { return next; };
64                void                    set_next(list_elem<Type> *n)    { next = n; };
65                list_elem<Type>         *get_prev()                     { return prev; };
66                void                    set_prev(list_elem<Type> *p)    { prev = p; };
67               
68                list_elem();
69                list_elem(Type *el);
70                ~list_elem();
71};
72
73
74template <class Type> class List
75{
76        private:
77                list_elem<Type> *first;
78                list_elem<Type> *last;
79                list_elem<Type> *last_asked_list_elem;
80                list_elem<Type> *remembered_elem;
81
82                positiontype    no_of_members;
83                BOOL            sotl;
84
85
86                list_elem<Type> *get_list_elem_with_member      ( Type *object );
87                list_elem<Type> *get_list_elem_at_pos           ( positiontype pos );
88                list_elem<Type> *get_list_elem_at_pos_simple    ( positiontype pos );
89        public:
90// general List functions
91
92
93//only use these functions if you know what you are doing !!!  BEGINNING
94                list_elem<Type> *get_first_list_elem()  { return first; };      // do not use !!!
95                list_elem<Type> *get_last_list_elem()   { return last; };       // do not use !!!
96                list_elem<Type> *get_current_list_elem(){ return last_asked_list_elem; };// do not use !!!
97                void            remember_current()      { remembered_elem = last_asked_list_elem; };
98                void            set_current_ARC(list_elem<Type> *t);            //ARC = and remember current
99                void            set_remembered_as_current_ARC();                //ARC = and remember current
100//only use these functions if you know what you are doing !!!  END
101
102                void            sotl_list()             { sotl = TRUE; };
103                void            no_sotl_list()          { sotl = FALSE; };
104                positiontype    get_no_of_members()     { return no_of_members; };
105
106                positiontype    insert_as_first         ( Type *object );
107                positiontype    insert_as_last          ( Type *object );       //returns pos_no
108                positiontype    insert_after_current    ( Type *object );
109                positiontype    insert_before_current   ( Type *object );
110                positiontype    insert                  ( Type *object );       //returns pos_no
111                Type            *get_first              ();
112                Type            *get_last               ();     
113                Type            *get_prev               ();
114                Type            *get_next               ();
115
116                void            remove_member_from_list ( Type *object );       //object won't be deleted
117                void            remove_first();
118                void            remove_last();
119                List<Type>      *duplicate_list         ( Type *object );       // the list is duplicated
120                                                                // from the element given til the end.
121                                                                // For duplicating the whole list
122                                                                // object = get_first(). Only the list is
123                                                                // duplicated, not the elems. Caution:
124                                                                // In a sotl list the asked element is put
125                                                                // to the front. So first make a no_sotl_list()
126                                                                // ask and duplicate the list, and then make a
127                                                                // sotl_list() again.
128
129                BOOL            exchange_members        ( Type *ex, Type *change );
130
131/*      following functions only make sense if our list is sorted by the address of
132        our item inserted to the list.
133        Caution:
134                The list is only sorted if it's NOT a sotl list. So take care to
135                create or set it to a no_sotl_list().
136*/
137
138                // Comment to insert_sorted_by_address_of_object:
139                // The function returns the no_of_members in the list.
140                // By default an object(the meaning here is that one object equals another,
141                // if the address of both objects matches) can be inserted to the list
142                // several times. If there is the need to insert an object only once
143                // in the list, the flag duplicates has to be set to FALSE.
144                // Finally the relation has to be set, by which the list is sorted.
145                // The possiblilities are : RELATION_GREATER and RELATION_LESS
146                positiontype    insert_sorted_by_address_of_object(     Type *object,
147                                                                        int relation=RELATION_LESS,
148                                                                        BOOL duplicates=TRUE );
149
150                // Comment to sort_list_join:
151                // if an object found in List l is found in this list it won't be inserted
152                // a second time.
153                void            sort_list_join          (       List<Type> *l,                 
154                                                                int relation=RELATION_LESS);    //Lists given as parameter
155                                                                                                //won't be affected
156                // Comment to sort_list_subtract:
157                // this function call only makes sense if the same element can be found
158                // in both lists. The flag relation tells the method how this list(not list l)
159                // is sorted. Both lists have to be sorted by the same relation.
160                void            sort_list_subtract      (       List<Type> *l,
161                                                                int relation=RELATION_LESS);           
162
163
164
165                positiontype    insert_at_pos_simple    ( Type *object, positiontype pos );     //returns pos inserted to
166                                                                                                //doesn't refer to get_pos()
167                Type            *get_member_at_pos_simple( positiontype pos );
168                                                               
169/*
170        Following functions only make sense if the user takes care of the list as
171        a numbered list.
172*/
173                // Comment to insert_at_pos :
174                // user does not have to call update_pos_no after insert_at_pos()
175                positiontype    insert_at_pos           ( Type *object, positiontype pos );     //returns pos inserted to
176                positiontype    get_pos_of_member       ( Type *object );
177                Type            *get_member_at_pos      ( positiontype pos );
178                BOOL            remove_pos_from_list    ( positiontype pos );           //element won't be deleted
179                BOOL            exchange_positions      ( positiontype ex, positiontype change );       //exchange elems in list
180                void            update_pos_no( list_elem<Type> *elem, positiontype nr); //updates no from
181                                                                // the given elem with nr til last
182                void            update_pos_no();                //updates pos number from first to last
183
184                List(BOOL so=FALSE);
185                ~List();
186};
187
188/**************************
189Beginn list_elem
190***************************/
191
192template <class Type> inline list_elem<Type>::list_elem()
193{
194        pos = 0;
195        next = NULL;
196        prev = NULL;
197        elem = NULL;
198}
199
200template <class Type> inline list_elem<Type>::list_elem(Type *el)
201{
202        pos = 0;
203        next = NULL;
204        prev = NULL;
205        elem = el;
206}
207
208template <class Type> inline list_elem<Type>::~list_elem()
209{
210        isolate_list_elem();
211}
212
213template <class Type> inline  BOOL list_elem<Type>::isolate_list_elem()
214{
215        if (prev && next)               //somewhere in the middle
216        {
217                prev->next      = next;
218                next->prev      = prev;
219                next            = NULL;
220                prev            = NULL;
221        }
222        else if (prev)                  //we're the last
223        {
224                prev->next      = NULL;
225                prev            = NULL;
226        }
227        else if (next)                  //we're the first
228        {
229                next->prev      = NULL;
230                next            = NULL;
231        }
232        else
233                return FALSE;
234
235        return TRUE;
236}
237
238/**************************
239Ende list_elem
240**************************/
241
242/**************************
243Beginn List
244***************************/
245
246template <class Type> inline List<Type>::List(BOOL so)
247{
248        first = last = last_asked_list_elem = remembered_elem = NULL;
249        no_of_members = 0;
250        sotl = so;
251}
252
253template <class Type> inline List<Type>::~List()
254{
255        list_elem<Type> *elem, *help;
256
257        elem = first;
258        while (elem)                            //delete every object in list
259        {
260                help = elem->next;
261                delete elem;
262                elem = help;
263        }
264}
265
266template <class Type> inline void List<Type>::set_current_ARC(list_elem<Type> *t)       //ARC = and remember current
267{
268        remembered_elem = last_asked_list_elem;
269        last_asked_list_elem = t;
270}
271
272template <class Type> inline void List<Type>::set_remembered_as_current_ARC()   //ARC = and remember current
273{
274        list_elem<Type> *mark;
275
276        mark = last_asked_list_elem;
277        last_asked_list_elem = remembered_elem;
278        remembered_elem = mark;
279}
280
281template <class Type> inline list_elem<Type> *List<Type>::get_list_elem_with_member( Type *object )
282{
283        list_elem<Type> *loc_elem;
284
285        loc_elem = first;
286        while (loc_elem && loc_elem->elem!=object)
287                loc_elem = loc_elem->next;
288
289        return loc_elem;
290}
291
292template <class Type> inline list_elem<Type> *List<Type>::get_list_elem_at_pos( positiontype pos )
293{
294        list_elem<Type> *elem;
295       
296        if (pos < 1 || pos > no_of_members)
297                return NULL;
298
299        if (! last_asked_list_elem)
300        {
301                if (pos > no_of_members/2)
302                {
303                        elem = last;
304                        while (elem && elem->get_pos() != pos)
305                                elem = elem->get_prev();
306                }
307                else
308                {
309                        elem = first;
310                        while (elem && elem->get_pos() != pos)
311                                elem = elem->next;
312                }
313        }
314        else
315        {
316                elem = last_asked_list_elem;
317
318                if (pos >= last_asked_list_elem->get_pos())
319                {
320                        while (elem && elem->get_pos() != pos)
321                                elem = elem->next;
322                }
323                else    // pos < last_asked_list_elem->pos
324                {
325                        while (elem && elem->get_pos() != pos)
326                                elem = elem->get_prev();
327                }
328        }
329
330        return elem;
331}
332
333template <class Type> inline list_elem<Type> *List<Type>::get_list_elem_at_pos_simple( positiontype pos )
334{
335        list_elem<Type>         *elem;
336        register positiontype   counter = 1;
337       
338        if (pos < 1 || pos > no_of_members)
339                return NULL;
340
341        if (pos > no_of_members/2)
342        {
343                elem = last;
344                counter = no_of_members;
345                while (elem && counter != pos)
346                {
347                        elem = elem->get_prev();
348                        counter --;
349                }
350        }
351        else
352        {
353                elem = first;
354                while (elem && counter != pos)
355                {
356                        elem = elem->next;
357                        counter ++;
358                }
359        }
360
361        return elem;
362}
363
364template <class Type> inline Type *List<Type>::get_first()
365{
366        if (first)
367        {
368                last_asked_list_elem = first;
369                return first->elem;
370        }
371        else
372                return NULL;
373}       
374       
375template <class Type> inline Type *List<Type>::get_last()
376{
377        if (last && ! sotl)                     //behaviour of a normal linked list
378        {
379                last_asked_list_elem = last;
380                return last->elem;
381        }
382        else if (last && sotl)
383        {
384                last_asked_list_elem = last->get_prev();
385                insert_as_first(last->elem);
386                remove_last();
387                return first->elem;
388        }
389        else
390                return NULL;
391}
392
393template <class Type> inline Type *List<Type>::get_prev()
394{
395        Type            *result = NULL;
396        list_elem<Type> *mark_prev;
397
398        if (last_asked_list_elem){
399
400        if (!sotl)              //behaviour of a normal linked list
401        {
402                last_asked_list_elem = last_asked_list_elem->get_prev();
403
404                if (last_asked_list_elem)
405                        result = last_asked_list_elem->elem;
406        }
407        else if (sotl)
408        {
409                if (last_asked_list_elem)
410                {
411                        result          = last_asked_list_elem->elem;
412                        mark_prev       = last_asked_list_elem->get_prev();
413
414                        remove_member_from_list( result );
415                        insert_as_first( result );
416                        last_asked_list_elem = mark_prev;
417                }
418        }
419        }
420        return result;
421}
422
423template <class Type> inline Type *List<Type>::get_next()
424{
425        Type            *result = NULL;
426        list_elem<Type> *mark_next;
427
428        if (last_asked_list_elem){
429
430        if (!sotl)              //behaviour of a normal linked list
431        {
432                last_asked_list_elem = last_asked_list_elem->next;
433
434                if (last_asked_list_elem)
435                        result = last_asked_list_elem->elem;
436        }
437        else if (sotl)
438        {
439                if (last_asked_list_elem)
440                {
441                        result          = last_asked_list_elem->elem;
442                        mark_next       = last_asked_list_elem->next;
443
444                        remove_member_from_list( result );
445                        insert_as_first( result );
446                        last_asked_list_elem = mark_next;
447                }
448        }
449        }
450
451        return result;
452}
453
454
455template <class Type> inline positiontype List<Type>::insert_as_first( Type *object )
456{
457        list_elem<Type> *help = NULL;
458
459        if (! first)                                    // create first element
460        {                                               // in list
461                first = new list_elem<Type>( object );
462                first->set_pos(1);
463                last = first;
464        }
465        else
466        {
467                help = new list_elem<Type>( object );
468                help->set_pos(1);                               //update by USER !!!
469                help->set_next(first);
470                first->set_prev(help);
471                first = help;
472        }
473
474        no_of_members ++;
475        return 1;
476}
477
478template <class Type> inline  positiontype List<Type>::insert_as_last( Type *object )
479{
480        list_elem<Type> *help = NULL;
481
482        if (! first)                                    // create first element
483        {                                               // in list
484                first = new list_elem<Type>( object );
485                first->set_pos(1);
486                last = first;
487        }
488        else
489        {
490                help = new list_elem<Type>( object );
491                help->set_pos( no_of_members+1 );
492                help->set_prev(last);
493                last->set_next(help);
494                last = help;
495        }
496       
497        no_of_members ++;
498        return no_of_members;
499}
500
501template <class Type> inline positiontype List<Type>::insert_after_current( Type *object )
502{
503    list_elem<Type>     *help = NULL;
504    positiontype result = 0;
505
506    if (last_asked_list_elem){
507
508    if (!last_asked_list_elem->get_next()){
509        result = insert_as_last( object );
510        }else{
511
512    help = new list_elem<Type>( object );
513    help->set_pos( last_asked_list_elem->get_pos() + 1 );
514    help->set_prev(last_asked_list_elem);
515    help->set_next( last_asked_list_elem->get_next() );
516    help->get_next()->set_prev(help);
517    last_asked_list_elem->set_next( help );
518    last_asked_list_elem = help;
519
520    no_of_members ++;
521    result =  no_of_members;
522    }
523    }
524    return result;
525}
526
527template <class Type> inline positiontype List<Type>::insert_before_current( Type *object )
528{
529    list_elem<Type>     *help = NULL;
530        positiontype result = 0;
531    if (last_asked_list_elem){
532
533    if (!last_asked_list_elem->get_prev()){
534        result=  insert_as_first( object );
535}else{
536
537    help = new list_elem<Type>( object );
538    help->set_pos( last_asked_list_elem->get_pos() - 1 );
539    help->set_next(last_asked_list_elem);
540    help->set_prev( last_asked_list_elem->get_prev() );
541    help->get_prev()->set_next(help);
542    last_asked_list_elem->set_prev( help );
543    last_asked_list_elem = help;
544
545    no_of_members ++;
546   result = no_of_members;
547   }
548   }
549    return result;
550}
551
552template <class Type> inline  positiontype List<Type>::insert( Type *object )
553{
554        return insert_as_first( object );
555}
556
557template <class Type> inline positiontype List<Type>::insert_at_pos_simple( Type *object, positiontype temp_pos )       //returns pos inserted to
558{
559        list_elem<Type> *elem,
560                        *new_elem;
561
562        if (temp_pos<=1)
563        {
564                insert_as_first( object );
565                return 1;
566        }
567        else if (temp_pos>no_of_members)
568        {
569                insert_as_last( object );
570                return no_of_members;
571        }
572        else
573        {
574                elem = get_list_elem_at_pos_simple( temp_pos );
575
576                new_elem = new list_elem<Type>;
577                new_elem->elem = object;
578                new_elem->set_pos(temp_pos);
579                new_elem->prev = elem->prev;
580                new_elem->next = elem;
581                elem->prev->next = new_elem;
582                elem->prev = new_elem;
583
584                no_of_members ++;
585                return temp_pos;
586        }
587}
588
589template <class Type> inline void List<Type>::remove_member_from_list( Type *object )
590{
591        list_elem<Type> *loc_elem;
592       
593        if (last_asked_list_elem &&
594                last_asked_list_elem->elem==object)
595        {
596                if (! last_asked_list_elem->next )      //we're the last
597                        last = last_asked_list_elem->get_prev();
598                       
599                if (! last_asked_list_elem->get_prev() )        //we're the first
600                        first = last_asked_list_elem->next;
601
602                delete last_asked_list_elem;
603
604                if (no_of_members == 1)
605                        first = last = NULL;
606
607                last_asked_list_elem = NULL;
608        }
609        else
610        {
611                if (last_asked_list_elem &&
612                        last_asked_list_elem->next && 
613                        last_asked_list_elem->next->elem == object )
614                        loc_elem = last_asked_list_elem->next;
615                else if (last_asked_list_elem &&
616                        last_asked_list_elem->get_prev() && 
617                        last_asked_list_elem->get_prev()->elem == object )
618                        loc_elem = last_asked_list_elem->get_prev();
619                else
620                        loc_elem = get_list_elem_with_member( object );
621
622                if (! loc_elem)
623                        return;
624
625                if (! loc_elem->next )  //we're the last
626                        last = loc_elem->get_prev();
627                       
628                if (! loc_elem->get_prev() )    //we're the first
629                        first = loc_elem->next;
630
631                delete loc_elem;
632        }
633
634        no_of_members --;
635}
636
637template <class Type> inline void List<Type>::remove_first()
638{
639        list_elem<Type> *new_first;
640
641        if (no_of_members <= 1)                 //case 0 or 1
642        {
643                delete first;
644                first = last = last_asked_list_elem = NULL;
645                no_of_members = 0;
646        }
647        else
648        {
649                new_first = first->next;
650                delete first;
651                first = new_first;
652                no_of_members --;
653        }
654}
655
656template <class Type> inline void List<Type>::remove_last()
657{
658        list_elem<Type> *new_last;
659
660        if (no_of_members <= 1)                 //case 0 or 1
661        {
662                delete last;
663                first = last = last_asked_list_elem = NULL;
664                no_of_members = 0;
665        }
666        else
667        {
668                new_last = last->get_prev();
669                delete last;
670                last = new_last;
671                no_of_members --;
672        }       
673}
674
675template <class Type> inline positiontype List<Type>::insert_sorted_by_address_of_object( Type *object,
676                                                                                int relation,
677                                                                                BOOL duplicates )               //falls object schon vorhanden, dann
678{
679        list_elem<Type> *help = NULL,
680                        *l_help;
681
682        if (! first)                            // create first element
683        {                                       // in list
684                first = new list_elem<Type>( object );
685                first->set_pos(1);
686                last = first;
687        }
688        else
689        {       
690                if (relation == RELATION_GREATER)       // search for a place to insert to
691                {                                       // objects which are created later,
692                        l_help = first;                 // have a greater address !!!
693                        while (l_help && object < l_help->elem)
694                                l_help = l_help->next;
695                }
696                else            //RELATION_LESS
697                {
698                        l_help = last;
699                        while (l_help && object < l_help->elem)
700                                l_help = l_help->get_prev();
701                }
702       
703                if (l_help && object == l_help->elem && ! duplicates )  //Element already is in the list
704                        return no_of_members;
705
706                help = new list_elem<Type>( object );   // generate new element
707
708                if (! l_help)                           //new element in front/at end
709                {
710                        if (relation == RELATION_GREATER)
711                        {
712                                last->set_next(help);
713                                help->set_prev(last);
714                                last = help;
715                        }
716                        else            //RELATION_LESS
717                        {
718                                first->set_prev(help);
719                                help->set_next(first);
720                                first = help;
721                        }
722                }       
723                else
724                {
725                        if (relation == RELATION_GREATER)
726                        {
727                                if (first == l_help)
728                                        first = help;
729
730                                help->set_next(l_help);
731                                help->set_prev(l_help->get_prev());
732                        }
733                        else            //RELATION_LESS
734                        {
735                                if (last == l_help)
736                                        last = help;
737
738                                help->set_prev(l_help);
739                                help->set_next(l_help->next);
740                        }                       
741
742                        if (help->next)
743                                help->next->set_prev(help);
744
745                        if (help->get_prev())
746                                help->get_prev()->set_next(help);
747                }
748               
749        }
750       
751        no_of_members ++;
752       
753        return no_of_members;
754}
755
756template <class Type> inline void List<Type>::sort_list_join( List<Type> *l,
757                                                        int relation)
758{
759        list_elem<Type> *this_list = first,
760                        *join_list,
761                        *new_elem;
762
763        if (! l || ! l->get_no_of_members())
764                return;
765
766        join_list = l->get_first_list_elem();
767
768        while (this_list && join_list)
769        {
770                if ( this_list->elem == join_list->elem )
771                {
772                        this_list = this_list->next;
773                        join_list = join_list->next;
774                }
775                else
776                {
777                        if (    ( relation == RELATION_GREATER && this_list->elem > join_list->elem) ||
778                                ( relation == RELATION_LESS    && this_list->elem < join_list->elem) )
779                                this_list = this_list->next;
780                        else
781                        {
782                                new_elem = new list_elem<Type>( join_list->elem );
783                                new_elem->set_prev( this_list->get_prev() );
784                                new_elem->set_next( this_list );
785                                this_list->set_prev( new_elem );
786                               
787                                if (new_elem->get_prev())
788                                        new_elem->get_prev()->set_next( new_elem );
789                                else
790                                        first = new_elem;
791
792                                join_list = join_list->next;
793
794                                no_of_members ++;
795                        }
796                }
797        }
798
799        if (! join_list || (!this_list && !join_list))
800                return;
801       
802        if (!this_list)
803        {
804                while(join_list)
805                {
806                        new_elem = new list_elem<Type>( join_list->elem );
807                        new_elem->set_prev( last );
808
809                        if (last)
810                                last->set_next( new_elem );
811       
812                        if (!first)
813                                first = new_elem;
814
815                        last = new_elem;
816                        no_of_members ++;
817                        join_list = join_list->next;
818                }
819        }               
820}
821
822template <class Type> inline  void List<Type>::sort_list_subtract( List<Type> *l, int relation )
823{
824        list_elem<Type> *this_list = first,
825                        *join_list = l->get_first_list_elem(),
826                        *mark;
827       
828        while (this_list && join_list)
829        {               
830                if ( (  relation == RELATION_GREATER && this_list->elem > join_list->elem) ||
831                     (  relation == RELATION_LESS    && this_list->elem < join_list->elem) )
832                        this_list = this_list->next;
833                else if ( (     relation == RELATION_GREATER && this_list->elem < join_list->elem) ||
834                     (          relation == RELATION_LESS    && this_list->elem > join_list->elem) )
835                        join_list = join_list->next;
836                else if (this_list->elem == join_list->elem) //same element => delete from this_list
837                {
838                        if (this_list == first)
839                                first = this_list->next;
840
841                        if (this_list == last)
842                                last = this_list->get_prev();
843
844                        mark = this_list->next;
845                        delete this_list;
846                        this_list = mark;
847
848                        join_list = join_list->next;
849                        no_of_members --;
850
851                        if (! no_of_members)
852                        {
853                                last_asked_list_elem = NULL;
854                                return;
855                        }
856                }
857        }
858}
859
860template <class Type> inline List<Type> *List<Type>::duplicate_list( Type *object )
861{
862        list_elem<Type> *help_l = first;
863        List<Type>      *new_list = NULL;
864
865        if (last_asked_list_elem->elem == object)
866                help_l = last_asked_list_elem;
867        else
868                help_l = get_list_elem_with_member( object );
869
870        if (help_l)
871                new_list = new List<Type>;
872
873        while (help_l)
874        {
875                new_list->insert_as_last(help_l->elem);
876
877                help_l = help_l->next;
878        }
879
880        return new_list;               
881}
882
883template <class Type>  inline void List<Type>::update_pos_no( list_elem<Type> *elem, positiontype nr ) 
884{
885        list_elem<Type> *mark;
886       
887        if (!elem)
888                return;
889
890        elem->set_pos(nr++);
891        mark = elem->next;
892        while (mark)
893        {
894                mark->set_pos(nr++);
895                mark = mark->next;
896        }
897}
898
899template <class Type> inline void List<Type>::update_pos_no() 
900{
901        update_pos_no( first, 1);
902}
903
904template <class Type> inline BOOL List<Type>::exchange_members( Type *ex, Type *change )
905{
906        list_elem<Type> *one, *two;
907        BOOL result = FALSE;
908        while (1){
909        if (!ex || !change)
910                break;
911
912        if (last_asked_list_elem && last_asked_list_elem->elem == ex)
913                one = last_asked_list_elem;
914        else
915                one = get_list_elem_with_member( ex );
916
917        if (! one)
918                break;
919
920        if (last_asked_list_elem && last_asked_list_elem->elem == change)
921                two = last_asked_list_elem;
922        else
923                two = get_list_elem_with_member( change );
924
925        if (!two)
926                break;
927
928        one->set_elem(change);
929        two->set_elem(ex);
930        result = TRUE;
931        break;
932        }
933
934        return result;
935}
936
937template <class Type> inline BOOL List<Type>::exchange_positions( positiontype ex, positiontype change )
938{
939        list_elem<Type> *one, *two;
940        Type            *dummy;
941        BOOL    result = FALSE;
942        while(1){
943        if (    ex < 1 || ex > no_of_members ||
944                change < 1 || change > no_of_members)
945                break;
946
947        one = get_list_elem_at_pos( ex );
948        if (! one)
949                break;
950
951        two = get_list_elem_at_pos( change );
952        if (!two)
953                break;
954
955        dummy = one->elem;
956        one->elem = two->elem;
957        two->elem = dummy;
958        result = TRUE;
959        break;
960        }
961        return result;
962}
963
964template <class Type> inline positiontype List<Type>::get_pos_of_member( Type *object )
965{
966        list_elem<Type> *elem;
967
968        if (!object)
969                return 0;
970
971        if (last_asked_list_elem && last_asked_list_elem->elem == object)
972                return last_asked_list_elem->get_pos();
973        else
974        {
975                elem = first;
976                while (elem && elem->elem != object)
977                        elem = elem->next;
978
979                if (elem)
980                        return elem->get_pos();
981                else
982                        return 0;
983        }
984}
985
986
987template <class Type> inline positiontype List<Type>::insert_at_pos( Type *object, positiontype pos )   //returns pos inserted to
988{
989        list_elem<Type> *elem,
990                        *new_elem;
991                        positiontype result ;
992
993        elem = get_list_elem_at_pos( pos );
994
995        if (! elem)
996        {
997                insert_as_last( object );
998                result = last->get_pos();
999        }
1000        else
1001        {
1002                new_elem = new list_elem<Type>( object );
1003                new_elem->set_prev(elem->get_prev());
1004                new_elem->set_next(elem);
1005
1006                if (elem->get_prev())
1007                        elem->get_prev()->set_next(new_elem);
1008
1009                elem->set_prev(new_elem);
1010
1011                if (first == elem)
1012                        first = new_elem;
1013
1014                update_pos_no ( new_elem, pos );
1015
1016                result =  pos;
1017        }
1018        return result;
1019}
1020
1021template <class Type> inline Type *List<Type>::get_member_at_pos( positiontype pos )
1022{
1023        list_elem<Type> *elem;
1024
1025        elem = get_list_elem_at_pos( pos );
1026
1027        if (elem)
1028        {
1029                last_asked_list_elem = elem;
1030                return elem->elem;
1031        }
1032        else
1033                return NULL;
1034}
1035
1036template <class Type> inline Type *List<Type>::get_member_at_pos_simple( positiontype pos )
1037{
1038        list_elem<Type> *elem;
1039
1040        elem = get_list_elem_at_pos_simple( pos );
1041
1042        if (elem)
1043        {
1044                last_asked_list_elem = elem;
1045                return elem->elem;
1046        }
1047        else
1048                return NULL;
1049}
1050
1051template <class Type> inline BOOL List<Type>::remove_pos_from_list( positiontype pos )
1052{
1053        list_elem<Type> *loc_elem;
1054        BOOL result = FALSE;
1055        while(1){
1056        if (pos < 1 || pos > no_of_members)
1057                break;
1058
1059        loc_elem = last_asked_list_elem;
1060       
1061        if (loc_elem && loc_elem->get_pos() == pos)
1062                last_asked_list_elem = NULL;
1063        else
1064        {
1065                if (! (loc_elem = get_list_elem_at_pos( pos )))
1066                        break;
1067        }
1068
1069        if (last == loc_elem)
1070                last = last->get_prev();
1071
1072        if (first == loc_elem)
1073
1074
1075                first = first->next;
1076
1077        delete loc_elem;
1078        no_of_members --;
1079        result = TRUE;
1080        break;
1081        }
1082
1083        return result;
1084}
1085
1086/**************************
1087Ende List
1088**************************/
1089
1090
1091#endif
Note: See TracBrowser for help on using the repository browser.