2 Copyright (C) 2003 Rice1964
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #ifndef _SORTED_LIST_H_
20 #define _SORTED_LIST_H_
24 template<class Key, class Element>
34 CSortedList(int size=1000)
39 elements = new Element[size];
58 void add(Key key, Element ele)
67 if( curSize == maxSize )
69 // Need to increase maxSize
71 Element *oldelements = elements;
72 int oldmaxsize = maxSize;
75 keys = new Key[maxSize];
76 elements = new Element[maxSize];
77 std::memcpy(keys,oldkeys,oldmaxsize*sizeof(Key));
78 std::memcpy(elements,oldelements,oldmaxsize*sizeof(Element));
81 for( i=0; i<curSize; i++ )
90 for( int j=curSize; j>i; j-- )
93 elements[j] = elements[j-1];
101 Element operator[](int index)
103 if( index >= curSize )
108 return elements[index];
113 int index = Find(key);
114 return this->operator[](index);
124 int dwMax = curSize - 1;
132 dwRange = dwMax - dwMin;
133 dwIndex = dwMin + (dwRange/2);
135 if( keys[dwIndex] == key )
141 // If the range is 0, and we didn't match above, then it must be unmatched
145 // If lower, check from min..index
146 // If higher, check from index..max
147 if (key < keys[dwIndex])