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 #ifndef _unsignedXPlex_h
21 #ifdef __GNUG__
22 #pragma interface
23 #endif
24 #define _unsignedXPlex_h 1
25 
26 #include "unsigned.Plex.h"
27 
28 class unsignedXPlex: public unsignedPlex
29 {
30   unsignedIChunk*       ch;           // cached chunk
31 
32   void             make_initial_chunks(int up = 1);
33 
34   void             cache(int idx) const;
35   void             cache(const unsigned* p) const;
36 
37   unsigned*             dopred(const unsigned* p) const;
38   unsigned*             dosucc(const unsigned* p) const;
39 
40   inline void             set_cache(const unsignedIChunk* t) const; // logically,
41                                                // not physically const
42 public:
43                    unsignedXPlex();                 // set low = 0;
44                                                // fence = 0;
45                                                // csize = default
46 
47                    unsignedXPlex(int ch_size);      // low = 0;
48                                                // fence = 0;
49                                                // csize = ch_size
50 
51                    unsignedXPlex(int lo,            // low = lo;
52                             int ch_size);      // fence=lo
53                                                // csize = ch_size
54 
55                    unsignedXPlex(int lo,            // low = lo
56                             int hi,            // fence = hi+1
57                             const unsigned  initval,// fill with initval,
58                             int ch_size = 0);  // csize= ch_size
59                                                // or fence-lo if 0
60 
61                    unsignedXPlex(const unsignedXPlex&);
62 
63   void             operator= (const unsignedXPlex&);
64 
65 // virtuals
66 
67 
68   inline unsigned&             high_element ();
69   inline unsigned&             low_element ();
70 
71   inline const unsigned&       high_element () const;
72   inline const unsigned&       low_element () const;
73 
74   inline Pix              first() const;
75   inline Pix              last() const;
76   inline void             prev(Pix& ptr) const;
77   inline void             next(Pix& ptr) const;
78   int              owns(Pix p) const;
79   inline unsigned&             operator () (Pix p);
80   inline const unsigned&       operator () (Pix p) const;
81 
82   inline int              low() const;
83   inline int              high() const;
84   inline int              valid(int idx) const;
85   inline void             prev(int& idx) const;
86   inline void             next(int& x) const;
87   inline unsigned&             operator [] (int index);
88   inline const unsigned&       operator [] (int index) const;
89 
90   inline int              Pix_to_index(Pix p) const;
91   inline Pix              index_to_Pix(int idx) const;
92 
93   inline int              can_add_high() const;
94   inline int              can_add_low() const;
95   inline int              full() const;
96 
97   int              add_high(const unsigned  elem);
98   int              del_high ();
99   int              add_low (const unsigned  elem);
100   int              del_low ();
101 
102   void             fill(const unsigned  x);
103   void             fill(const unsigned  x, int from, int to);
104   void             clear();
105 
106   int              OK () const;
107 
108 };
109 
110 
prev(int & idx)111 inline void unsignedXPlex::prev(int& idx) const
112 {
113   --idx;
114 }
115 
next(int & idx)116 inline void unsignedXPlex::next(int& idx) const
117 {
118   ++idx;
119 }
120 
full()121 inline  int unsignedXPlex::full () const
122 {
123   return 0;
124 }
125 
can_add_high()126 inline int unsignedXPlex::can_add_high() const
127 {
128   return 1;
129 }
130 
can_add_low()131 inline int unsignedXPlex::can_add_low() const
132 {
133   return 1;
134 }
135 
valid(int idx)136 inline  int unsignedXPlex::valid (int idx) const
137 {
138   return idx >= lo && idx < fnc;
139 }
140 
low()141 inline int unsignedXPlex::low() const
142 {
143   return lo;
144 }
145 
high()146 inline int unsignedXPlex::high() const
147 {
148   return fnc - 1;
149 }
150 
151 inline unsigned& unsignedXPlex:: operator [] (int idx)
152 {
153   if (!ch->actual_index(idx)) cache(idx);
154   return *(ch->pointer_to(idx));
155 }
156 
157 inline const unsigned& unsignedXPlex:: operator [] (int idx) const
158 {
159   if (!ch->actual_index(idx)) cache(idx);
160   return *((const unsigned*)(ch->pointer_to(idx)));
161 }
162 
low_element()163 inline  unsigned& unsignedXPlex::low_element ()
164 {
165   if (empty()) index_error();
166   return *(hd->pointer_to(lo));
167 }
168 
low_element()169 inline  const unsigned& unsignedXPlex::low_element () const
170 {
171   if (empty()) index_error();
172   return *((const unsigned*)(hd->pointer_to(lo)));
173 }
174 
high_element()175 inline  unsigned& unsignedXPlex::high_element ()
176 {
177   if (empty()) index_error();
178   return *(tl()->pointer_to(fnc - 1));
179 }
180 
high_element()181 inline const unsigned& unsignedXPlex::high_element () const
182 {
183   if (empty()) index_error();
184   return *((const unsigned*)(tl()->pointer_to(fnc - 1)));
185 }
186 
Pix_to_index(Pix px)187 inline  int unsignedXPlex::Pix_to_index(Pix px) const
188 {
189   unsigned* p = (unsigned*)px;
190   if (!ch->actual_pointer(p)) cache(p);
191   return ch->index_of(p);
192 }
193 
index_to_Pix(int idx)194 inline  Pix unsignedXPlex::index_to_Pix(int idx) const
195 {
196   if (!ch->actual_index(idx)) cache(idx);
197   return (Pix)(ch->pointer_to(idx));
198 }
199 
first()200 inline Pix unsignedXPlex::first() const
201 {
202   return Pix(hd->unsignedIChunk::first_pointer());
203 }
204 
last()205 inline Pix unsignedXPlex::last() const
206 {
207   return Pix(tl()->unsignedIChunk::last_pointer());
208 }
209 
prev(Pix & p)210 inline void unsignedXPlex::prev(Pix& p) const
211 {
212   Pix q = Pix(ch->unsignedIChunk::pred((unsigned*) p));
213   p = (q == 0)? Pix(dopred((const unsigned*) p)) : q;
214 }
215 
next(Pix & p)216 inline void unsignedXPlex::next(Pix& p) const
217 {
218   Pix q = Pix(ch->unsignedIChunk::succ((unsigned*) p));
219   p = (q == 0)? Pix(dosucc((const unsigned*)p)) : q;
220 }
221 
operator()222 inline unsigned& unsignedXPlex:: operator () (Pix p)
223 {
224   return *((unsigned*)p);
225 }
226 
operator()227 inline const unsigned& unsignedXPlex:: operator () (Pix p) const
228 {
229   return *((const unsigned*)p);
230 }
231 
set_cache(const unsignedIChunk * t)232 inline void unsignedXPlex::set_cache(const unsignedIChunk* t) const
233 {
234   ((unsignedXPlex*)(this))->ch = (unsignedIChunk*)t;
235 }
236 
237 #endif
238