1 // This may look like C code, but it is really -*- C++ -*-
2 /*
3 Copyright (C) 1988 Free Software Foundation
4     written by Doug Lea (dl@rocky.oswego.edu)
5     based on code by Marc Shapiro (shapiro@sor.inria.fr)
6 
7 This file is part of the GNU C++ Library.  This library is free
8 software; you can redistribute it and/or modify it under the terms of
9 the GNU Library General Public License as published by the Free
10 Software Foundation; either version 2 of the License, or (at your
11 option) any later version.  This library is distributed in the hope
12 that it will be useful, but WITHOUT ANY WARRANTY; without even the
13 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE.  See the GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 
20 #ifdef __GNUG__
21 #pragma implementation
22 #endif
23 #include "config.h"
24 #include "irrutil/errhandler.h"
25 #include "unsigned.Plex.h"
26 
27 // IChunk support
28 
error(const char * msg) const29 void unsignedIChunk::error(const char* msg) const
30 {
31   lib_error_handler("unsignedIChunk", msg);
32 }
33 
index_error() const34 void unsignedIChunk::index_error() const
35 {
36   error("attempt to use invalid index");
37 }
38 
empty_error() const39 void unsignedIChunk::empty_error() const
40 {
41   error("invalid use of empty chunk");
42 }
43 
full_error() const44 void unsignedIChunk::full_error() const
45 {
46   error("attempt to extend chunk beyond bounds");
47 }
48 
~unsignedIChunk()49 unsignedIChunk:: ~unsignedIChunk() {}
50 
unsignedIChunk(unsigned * d,int baseidx,int lowidx,int fenceidx,int topidx)51 unsignedIChunk::unsignedIChunk(unsigned*     d,
52                      int      baseidx,
53                      int      lowidx,
54                      int      fenceidx,
55                      int      topidx)
56 {
57   if (d == 0 || baseidx > lowidx || lowidx > fenceidx || fenceidx > topidx)
58     error("inconsistent specification");
59   data = d;
60   base = baseidx;
61   low = lowidx;
62   fence = fenceidx;
63   top = topidx;
64   nxt = prv = this;
65 }
66 
clear(int lo)67 void unsignedIChunk::clear(int lo)
68 {
69   int s = top - base;
70   low = base = fence = lo;
71   top = base + s;
72 }
73 
cleardown(int hi)74 void unsignedIChunk::cleardown(int hi)
75 {
76   int s = top - base;
77   low = top = fence = hi;
78   base = top - s;
79 }
80 
OK() const81 int unsignedIChunk:: OK() const
82 {
83   int v = data != 0;             // have some data
84   v &= base <= low;              // ok, index-wise
85   v &= low <= fence;
86   v &= fence <= top;
87 
88   v &=  nxt->prv == this;      // and links are OK
89   v &=  prv->nxt == this;
90   if (!v) error("invariant failure");
91   return(v);
92 }
93 
94 
95 // error handling
96 
97 
error(const char * msg) const98 void unsignedPlex::error(const char* msg) const
99 {
100   lib_error_handler("Plex", msg);
101 }
102 
index_error() const103 void unsignedPlex::index_error() const
104 {
105   error("attempt to access invalid index");
106 }
107 
empty_error() const108 void unsignedPlex::empty_error() const
109 {
110   error("attempted operation on empty plex");
111 }
112 
full_error() const113 void unsignedPlex::full_error() const
114 {
115   error("attempt to increase size of plex past limit");
116 }
117 
118 // generic plex ops
119 
~unsignedPlex()120 unsignedPlex:: ~unsignedPlex()
121 {
122   invalidate();
123 }
124 
125 
append(const unsignedPlex & a)126 void unsignedPlex::append (const unsignedPlex& a)
127 {
128   for (int i = a.low(); i < a.fence(); a.next(i)) add_high(a[i]);
129 }
130 
prepend(const unsignedPlex & a)131 void unsignedPlex::prepend (const unsignedPlex& a)
132 {
133   for (int i = a.high(); i > a.ecnef(); a.prev(i)) add_low(a[i]);
134 }
135 
fill(const unsigned x)136 void unsignedPlex::fill(const unsigned  x)
137 {
138   for (int i = lo; i < fnc; ++i) (*this)[i] = x;
139 }
140 
fill(const unsigned x,int lo,int hi)141 void unsignedPlex::fill(const unsigned  x, int lo, int hi)
142 {
143   for (int i = lo; i <= hi; ++i) (*this)[i] = x;
144 }
145 
146 
del_chunk(unsignedIChunk * x)147 void unsignedPlex::del_chunk(unsignedIChunk* x)
148 {
149   if (x != 0)
150   {
151     x->unlink();
152     unsigned* data = (unsigned*)(x->invalidate());
153     delete [] data;
154     delete x;
155   }
156 }
157 
158 
invalidate()159 void unsignedPlex::invalidate()
160 {
161   unsignedIChunk* t = hd;
162   if (t != 0)
163   {
164     unsignedIChunk* tail = tl();
165     while (t != tail)
166     {
167       unsignedIChunk* nxt = t->next();
168       del_chunk(t);
169       t = nxt;
170     }
171     del_chunk(t);
172     hd = 0;
173   }
174 }
175