summaryrefslogtreecommitdiff
path: root/flower/list.inl
blob: 8bdcf158772df21f6764b6fa507742870c022ca8 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// -*-c++-*-
#ifndef LIST_INL
#define LIST_INL
template<class T>
inline
List<T>::List()
{
    set_empty();
}

template<class T>
inline void
List<T>::set_empty()
{
    top_ = bottom_ = 0;
    size_ = 0;
}

template<class T>
inline
List<T>::List( const T& thing )
{
    set_empty();
    add( thing, Cursor<T>( *this, bottom_ ) );
}

template<class T>
inline
List<T>::~List()
{
    Cursor<T> next(*this);
    for ( Cursor<T> c( *this ); c.ok(); c = next ) {
	next = c;
	next++;
	remove( c );
    }
}

template<class T>
inline void
List<T>::add( const T& thing, Cursor<T> after_me )
{
#if 0
    if ( after_me.ok() )
	after_me.pointer()->add( thing );
    else if ( size_ )
        bottom().pointer()->add( thing );
    else
        bottom_ = top_ = new Link<T>( thing );
#endif
    
    if (!size_) {		// not much choice if list is empty
        bottom_ = top_ = new Link<T>( thing );
    } else {			// add at aprioprate place
	Link<T> *p =  ( after_me.ok() ) ?
	    after_me.pointer() : bottom().pointer();
	p->add(thing);
	if (p == bottom_)	// adjust bottom_ if necessary.
	    bottom_ = p->next();
    }

    size_++;
}
/** 

  Procedure:
  \begin{itemize}
  \item if #after_me# is #ok()#, add after #after_me#, else
  \item if list !empty simply add to bottom, else
  \item list is empty: create first \Ref{Link} and initialize 
  #bottom_# and #top_#.
  \end{itemize}
*/

template<class T>
inline void
List<T>::insert( const T& thing, Cursor<T> before_me )
{
    if (!size_) {
	bottom_ = top_ = new Link<T>( thing );
    } else {
	Link<T> *p = 
	    (before_me.ok())?
	    before_me.pointer() : top().pointer();

	p->insert(thing);
	if (p == top_)
	    top_ = p->previous();
    }
	
    size_++;
#if 0 // rewrite hwn 16/9		
    if ( before_me.ok() )
        before_me.pointer()->insert( thing );
    else if ( size_ )
	top().pointer()->insert( thing );
    else
	bottom_ = top_ = new Link<T>( thing );
    size_++;
#endif
}

template<class T>
inline void
List<T>::remove( Cursor<T> me )
{
    if ( me.ok() ){
	Link<T> *lp = me.pointer();	
	lp->remove(*this);
	delete lp;
        size_--;
    }
}

template<class T>
inline int
List<T>::size() const
{ 
    return size_;
}



#endif