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
|
#ifndef __ordlist_h
#define __ordlist_h
/*
ordlist.h : This file is part of pstoedit
simple template for a sorted list. I didn't want to use STL
because not all compilers support it yet.
Copyright (C) 1993 - 2009 Wolfgang Glunz, wglunz35_AT_pstoedit.net
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef cppcomp_h
#include "cppcomp.h"
#endif
#ifndef assert
#include <assert.h>
#endif
#include I_stdlib
#include I_iostream
//lint -sem(ordlist*::insert,custodial(1))
//
// Telem is the type under which an element is stored, so either T or T &
//
template <class T,class Telem,class COMPARATOR>
class ordlist {
private:
class ordlistElement {
public:
ordlistElement(const T& e,ordlistElement * n): next(n),elem(e) {}
ordlistElement* next;
Telem elem;
};
public:
ordlist():
first(0),
l_size(0),
lastaccessptr((new ordlistElement *)),
lastaccessindexptr((new unsigned int)) {}
// initialize the Refs with objects on the heap, because we need
// to modify these from within const functions
// (these act as a sort of cache, but they don't influence constness)
~ordlist() {
clear();
delete (lastaccessptr); lastaccessptr=0;
delete (lastaccessindexptr);lastaccessindexptr=0;
first = 0;
}
void clear() {
ordlistElement * cur = first;
while(cur != 0) { ordlistElement * next = cur-> next; delete cur; cur = next; }
l_size = 0;
first = 0;
(*lastaccessptr) = 0;
(*lastaccessindexptr) = 0;
}
void insert(const T& elem) {
if (first == 0) {
first = new ordlistElement(elem,0);
} else {
if (COMPARATOR::compare(first->elem , elem) ) {
ordlistElement * newelem = new ordlistElement(elem,first);
first = newelem;
} else {
ordlistElement * next = first->next;
ordlistElement * current = first;
while (current != 0) {
if ( (next == 0) || COMPARATOR::compare(next->elem , elem) ) {
ordlistElement * newelem = new ordlistElement(elem,next);
current->next = newelem;
break;
}
current = next;
next = next->next;
}
}
}
l_size++;
*lastaccessptr = first;
*lastaccessindexptr = 0;
}
const T &operator[](size_t i) const {
ordlistElement * start = 0;
unsigned int ind = 0;
if (i < size() ) {
if (i == (*lastaccessindexptr) ) {
// cerr << "returning from last " << endl;
return (*lastaccessptr)->elem;
} else if (i < (*lastaccessindexptr) ) {
// cerr << "returning via search from start " << endl;
start = first;
ind = 0;
} else {
// cerr << "returning via forward search" << endl;
start = (*lastaccessptr);
ind = (*lastaccessindexptr);
}
assert(start);
while(ind < i) { start = start->next; ind++;}
// we need to cast away const for caching
(*lastaccessptr) = start;
(*lastaccessindexptr) = i;
// ((ordlist<T,Telem,COMPARATOR> *)this)->lastaccess = start;
// ((ordlist<T,Telem,COMPARATOR> *)this)->lastaccessindex = i;
return start->elem;
} else {
cerr << "illegal index " << i << " max : " << size() << endl;
assert(i < size());
}
return start->elem; // never reached, just to keep compiler quiet
}
unsigned int size() const { return l_size;}
#ifdef TEST
void dump() const {
int i = 0;
ordlistElement * cur = first;
while(cur != 0) { cout << '[' << i << "] :" << cur->elem << endl; i++; cur = cur->next; }
}
#endif
private:
ordlistElement * first;
unsigned int l_size ;
// helpers for faster sequential access via operator[]
// these remember the position of the last access
ordlistElement **lastaccessptr;
unsigned int * lastaccessindexptr ;
// inhibitors: (may not be called)
ordlist(const ordlist<T,Telem,COMPARATOR> &);
const ordlist<T,Telem,COMPARATOR> & operator=(const ordlist<T,Telem,COMPARATOR> &);
};
#ifdef TEST
template <class T>
class GreaterThan {
public:
static bool compare(const T & o1, const T & o2) { return o1 > o2 ;}
};
template <class T>
class LessThan {
public:
static bool compare(const T & o1, const T & o2) { return o1 < o2 ;}
};
int main()
{
{
ordlist<int,int,LessThan<int> > oli;
oli.insert(5);
oli.insert(7);
oli.insert(1);
oli.insert(2);
oli.insert(9);
oli.insert(10);
oli.insert(41);
oli.insert(0);
cout << oli.size() << endl;
cout << oli[0] << endl;
cout << " begin of Dump " << endl;
oli.dump();
cout << " end of Dump " << endl;
for (unsigned int i = 0; i < oli.size(); i++ ) {
cout << oli[i] << " "<< endl;
}
cout << oli[3] << endl;
}
{
ordlist<int,const int&,LessThan<int> > oli;
cout << " begin of Dump " << endl;
oli.insert(1);
oli.dump();
cout << " end of Dump " << endl;
for (unsigned int i = 0; i < oli.size(); i++ ) {
cout << oli[i] << " "<< endl;
}
}
{
ordlist<int,int,LessThan<int> > oli;
cout << " begin of Dump " << endl;
oli.dump();
cout << " end of Dump " << endl;
for (unsigned int i = 0; i < oli.size(); i++ ) {
assert(oli.size() == 0);
}
// test for assertion
// cout << oli[3] << endl;
}
#if 0
// shouldn't compile due to inhibitors
{
ordlist<float,float,LessThan<int> > ol1;
ordlist<float,float,LessThan<int> > ol2;
ol1 = ol2;
ordlist<float,float,LessThan<int> > ol3 = ol1;
}
#endif
return 0;
}
#endif
// include guard
#endif
|