summaryrefslogtreecommitdiffstats
path: root/db/mork/src/morkDeque.cpp
blob: 3a380b5d86b75fff4b41caf5c11c06c95fca9d7a (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*************************************************************************
This software is part of a public domain IronDoc source code distribution,
and is provided on an "AS IS" basis, with all risks borne by the consumers
or users of the IronDoc software.  There are no warranties, guarantees, or
promises about quality of any kind; and no remedies for failure exist. 

Permission is hereby granted to use this IronDoc software for any purpose
at all, without need for written agreements, without royalty or license
fees, and without fees or obligations of any other kind.  Anyone can use,
copy, change and distribute this software for any purpose, and nothing is
required, implicitly or otherwise, in exchange for this usage.

You cannot apply your own copyright to this software, but otherwise you
are encouraged to enjoy the use of this software in any way you see fit.
However, it would be rude to remove names of developers from the code.
(IronDoc is also known by the short name "Fe" and a longer name "Ferrum",
which are used interchangeably with the name IronDoc in the sources.)
*************************************************************************/
/*
 * File:      morkDeque.cpp
 * Contains:  Ferrum deque (double ended queue (linked list))
 *
 * Copied directly from public domain IronDoc, with minor naming tweaks:
 * Designed and written by David McCusker, but all this code is public domain.
 * There are no warranties, no guarantees, no promises, and no remedies.
 */

#ifndef _MDB_
#include "mdb.h"
#endif

#ifndef _MORK_
#include "mork.h"
#endif

#ifndef _MORKDEQUE_
#include "morkDeque.h"
#endif

#ifndef _MORKNODE_
#include "morkNode.h"
#endif

#ifndef _MORKENV_
#include "morkEnv.h"
#endif

/*=============================================================================
 * morkNext: linked list node for very simple, singly-linked list
 */
 
morkNext::morkNext() : mNext_Link( 0 )
{
}

/*static*/ void*
morkNext::MakeNewNext(size_t inSize, nsIMdbHeap& ioHeap, morkEnv* ev)
{
  void* next = 0;
    ioHeap.Alloc(ev->AsMdbEnv(), inSize, (void**) &next);
    if ( !next )
      ev->OutOfMemoryError();

  return next;
}

/*static*/
void morkNext::ZapOldNext(morkEnv* ev, nsIMdbHeap* ioHeap)
{
  if ( ioHeap )
  {
      ioHeap->Free(ev->AsMdbEnv(), this);
  }
  else
    ev->NilPointerError();
}

/*=============================================================================
 * morkList: simple, singly-linked list
 */

morkList::morkList() : mList_Head( 0 ), mList_Tail( 0 )
{
}

void morkList::CutAndZapAllListMembers(morkEnv* ev, nsIMdbHeap* ioHeap)
// make empty list, zapping every member by calling ZapOldNext()
{
  if ( ioHeap )
  {
    morkNext* next = 0;
    while ( (next = this->PopHead()) != 0 )
      next->ZapOldNext(ev, ioHeap);
      
    mList_Head = 0;
    mList_Tail = 0;
  }
  else
    ev->NilPointerError();
}

void morkList::CutAllListMembers()
// just make list empty, dropping members without zapping
{
  while ( this->PopHead() )
    /* empty */;

  mList_Head = 0;
  mList_Tail = 0;
}

morkNext* morkList::PopHead() // cut head of list
{
  morkNext* outHead = mList_Head;
  if ( outHead ) // anything to cut from list?
  {
    morkNext* next = outHead->mNext_Link;
    mList_Head = next;
    if ( !next ) // cut the last member, so tail no longer exists?
      mList_Tail = 0;
      
    outHead->mNext_Link = 0; // nil outgoing node link; unnecessary, but tidy
  }
  return outHead;
}


void morkList::PushHead(morkNext* ioLink) // add to head of list
{
  morkNext* head = mList_Head; // old head of list
  morkNext* tail = mList_Tail; // old tail of list
  
  MORK_ASSERT( (head && tail) || (!head && !tail));
  
  ioLink->mNext_Link = head; // make old head follow the new link
  if ( !head ) // list was previously empty?
    mList_Tail = ioLink; // head is also tail for first member added

  mList_Head = ioLink; // head of list is the new link
}

void morkList::PushTail(morkNext* ioLink) // add to tail of list
{
  morkNext* head = mList_Head; // old head of list
  morkNext* tail = mList_Tail; // old tail of list
  
  MORK_ASSERT( (head && tail) || (!head && !tail));
  
  ioLink->mNext_Link = 0; 
  if ( tail ) 
  {
	  tail->mNext_Link = ioLink;
	  mList_Tail = ioLink;
  }
  else // list was previously empty?
	  mList_Head = mList_Tail = ioLink; // tail is also head for first member added
}

/*=============================================================================
 * morkLink: linked list node embedded in objs to allow insertion in morkDeques
 */
 
morkLink::morkLink() : mLink_Next( 0 ), mLink_Prev( 0 )
{
}

/*static*/ void*
morkLink::MakeNewLink(size_t inSize, nsIMdbHeap& ioHeap, morkEnv* ev)
{
  void* alink = 0;
    ioHeap.Alloc(ev->AsMdbEnv(), inSize, (void**) &alink);
    if ( !alink )
      ev->OutOfMemoryError();

  return alink;
}

/*static*/
void morkLink::ZapOldLink(morkEnv* ev, nsIMdbHeap* ioHeap)
{
  if ( ioHeap )
  {
      ioHeap->Free(ev->AsMdbEnv(), this);
  }
  else
    ev->NilPointerError();
}
  
/*=============================================================================
 * morkDeque: doubly linked list modeled after VAX queue instructions
 */

morkDeque::morkDeque()
{
  mDeque_Head.SelfRefer();
}


/*| RemoveFirst: 
|*/
morkLink*
morkDeque::RemoveFirst() /*i*/
{
  morkLink* alink = mDeque_Head.mLink_Next;
  if ( alink != &mDeque_Head )
  {
    (mDeque_Head.mLink_Next = alink->mLink_Next)->mLink_Prev = 
      &mDeque_Head;
    return alink;
  }
  return (morkLink*) 0;
}

/*| RemoveLast: 
|*/
morkLink*
morkDeque::RemoveLast() /*i*/
{
  morkLink* alink = mDeque_Head.mLink_Prev;
  if ( alink != &mDeque_Head )
  {
    (mDeque_Head.mLink_Prev = alink->mLink_Prev)->mLink_Next = 
      &mDeque_Head;
    return alink;
  }
  return (morkLink*) 0;
}

/*| At: 
|*/
morkLink*
morkDeque::At(mork_pos index) const /*i*/
  /* indexes are one based (and not zero based) */
{ 
  mork_num count = 0;
  morkLink* alink;
  for ( alink = this->First(); alink; alink = this->After(alink) )
  {
    if ( ++count == (mork_num) index )
      break;
  }
  return alink;
}

/*| IndexOf: 
|*/
mork_pos
morkDeque::IndexOf(const morkLink* member) const /*i*/
  /* indexes are one based (and not zero based) */
  /* zero means member is not in deque */
{ 
  mork_num count = 0;
  const morkLink* alink;
  for ( alink = this->First(); alink; alink = this->After(alink) )
  {
    ++count;
    if ( member == alink )
      return (mork_pos) count;
  }
  return 0;
}

/*| Length: 
|*/
mork_num
morkDeque::Length() const /*i*/
{ 
  mork_num count = 0;
  morkLink* alink;
  for ( alink = this->First(); alink; alink = this->After(alink) )
    ++count;
  return count;
}

/*| LengthCompare: 
|*/
int
morkDeque::LengthCompare(mork_num c) const /*i*/
{ 
  mork_num count = 0;
  const morkLink* alink;
  for ( alink = this->First(); alink; alink = this->After(alink) )
  {
    if ( ++count > c )
      return 1;
  }
  return ( count == c )? 0 : -1;
}