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