00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <stdlib.h>
00047 #include "UT_RefFixArray.h"
00048
00049 template <class utRef, unsigned long FixedSize>
00050 UT_RefFixArray<utRef,FixedSize>::~UT_RefFixArray() {}
00051
00052 template <class utRef, unsigned long FixedSize>
00053 long
00054 UT_RefFixArray<utRef,FixedSize>::insert(const utRef& t,
00055 unsigned short duplicates)
00056 {
00057 long idx = -1;
00058 if (nbrEntries == FixedSize) return -1;
00059 if (duplicates && ((idx = find(t)) != -1)) return (unsigned long)idx;
00060
00061 arr[nbrEntries] = t;
00062 return nbrEntries++;
00063 }
00064
00065 template <class utRef, unsigned long FixedSize>
00066 long
00067 UT_RefFixArray<utRef,FixedSize>::insert(const utRef& t, unsigned long index,
00068 unsigned short duplicates)
00069 {
00070 if (index == nbrEntries) insert(t, duplicates);
00071 long idx = -1;
00072 return (duplicates && ((idx=find(t)) != -1)) ? (unsigned long)idx
00073 : insertAt(t, index);
00074 }
00075
00076 template <class utRef, unsigned long FixedSize>
00077 long
00078 UT_RefFixArray<utRef,FixedSize>::insert(const utRef& t,
00079 int (*compare)(const void *t1, const void *t2))
00080 {
00081 long idx = -1;
00082 if (nbrEntries == FixedSize) return -1;
00083 if ((idx=find(t, compare)) != -1) return (unsigned long)idx;
00084
00085 arr[nbrEntries] = t;
00086 return nbrEntries++;
00087 }
00088
00089 template <class utRef, unsigned long FixedSize>
00090 long
00091 UT_RefFixArray<utRef,FixedSize>::insert(const utRef& t, unsigned long index,
00092 int (*compare)(const void *t1, const void *t2))
00093 {
00094 if (index == nbrEntries) insert(t, compare);
00095 long idx = -1;
00096 return ((idx=find(t, compare)) != -1) ? (unsigned long)idx
00097 : insertAt(t, index);
00098 }
00099
00100 template <class utRef, unsigned long FixedSize>
00101 long
00102 UT_RefFixArray<utRef,FixedSize>::remove(const utRef& t,
00103 unsigned short duplicates)
00104 {
00105 long idx = find(t);
00106 if (idx < 0) return -1;
00107 removeAt((unsigned long)idx, duplicates);
00108 return idx;
00109 }
00110
00111 template <class utRef, unsigned long FixedSize>
00112 void
00113 UT_RefFixArray<utRef,FixedSize>::removeAt(unsigned long idx,
00114 unsigned short duplicates)
00115 {
00116 if (duplicates)
00117 {
00118 const utRef &rem = arr[idx];
00119 long *gapArr = new long[nbrEntries];
00120 UT_ASSERT(gapArr);
00121 long gapCnt = 0;
00122 for (unsigned long i = 0; i < nbrEntries; i++)
00123 if (arr[i] == rem) gapArr[gapCnt++] = i;
00124 for (long g = gapCnt-1; g >= 0; g--, nbrEntries--)
00125 {
00126 unsigned long rmIdx = gapArr[g];
00127 if (rmIdx != nbrEntries)
00128 memmove(&arr[rmIdx], &arr[rmIdx+1],
00129 (int) ((nbrEntries-rmIdx-1)*sizeof(utRef)));
00130 }
00131 delete [] gapArr;
00132 }
00133 else if (idx != --nbrEntries)
00134 memmove(&arr[idx], &arr[idx+1], (int)((nbrEntries-idx)*sizeof(utRef)));
00135 }
00136
00137 template <class utRef, unsigned long FixedSize>
00138 long
00139 UT_RefFixArray<utRef,FixedSize>::remove(const utRef& t,
00140 int (*compare)(const void *t1, const void *t2),
00141 unsigned short duplicates)
00142 {
00143 long idx = find(t, compare);
00144 if (idx < 0) return -1;
00145 removeAt((unsigned long)idx, compare, duplicates);
00146 return idx;
00147 }
00148
00149 template <class utRef, unsigned long FixedSize>
00150 void
00151 UT_RefFixArray<utRef,FixedSize>::removeAt(unsigned long idx,
00152 int (*compare)(const void *t1, const void *t2),
00153 unsigned short duplicates)
00154 {
00155 if (duplicates)
00156 {
00157 const utRef &rem = arr[idx];
00158 long *gapArr = new long[nbrEntries];
00159 UT_ASSERT(gapArr);
00160 long gapCnt = 0;
00161 for (unsigned long i = 0; i < nbrEntries; i++)
00162 if (!compare(&arr[i],&rem)) gapArr[gapCnt++] = i;
00163 for (long g = gapCnt-1; g >= 0; g--, nbrEntries--)
00164 {
00165 unsigned long rmIdx = gapArr[g];
00166 if (rmIdx != nbrEntries)
00167 memmove(&arr[rmIdx], &arr[rmIdx+1],
00168 (int) ((nbrEntries-rmIdx)*sizeof(utRef)));
00169 }
00170 delete [] gapArr;
00171 }
00172 else if (idx != --nbrEntries)
00173 memmove(&arr[idx], &arr[idx+1], (int)((nbrEntries-idx)*sizeof(utRef)));
00174 }
00175
00176 template <class utRef, unsigned long FixedSize>
00177 int
00178 UT_RefFixArray<utRef,FixedSize>::shift(unsigned long srcIdx,
00179 unsigned long destIdx,
00180 unsigned long howMany)
00181 {
00182 if (srcIdx == destIdx) return 0;
00183 unsigned long sIdx = srcIdx + howMany - 1;
00184 unsigned long dIdx = destIdx + howMany - 1;
00185 if (sIdx>=FixedSize || dIdx>=FixedSize) return -1;
00186 if (dIdx>=nbrEntries || (sIdx==nbrEntries-1 && sIdx > dIdx))
00187 nbrEntries = dIdx+1;
00188
00189 memmove(&arr[destIdx], &arr[srcIdx], (int) (howMany*sizeof(utRef)));
00190 return 0;
00191 }
00192
00193 template <class utRef, unsigned long FixedSize>
00194 long
00195 UT_RefFixArray<utRef,FixedSize>::find(const utRef &t) const
00196 {
00197 long idx;
00198 for (idx = 0; idx < nbrEntries; idx++)
00199 if (arr[idx] == t) return idx;
00200
00201 return (long)-1;
00202 }
00203
00204 template <class utRef, unsigned long FixedSize>
00205 long
00206 UT_RefFixArray<utRef,FixedSize>::find(const utRef &t,
00207 int (*compare)(const void *t1, const void *t2)) const
00208 {
00209 if (!nbrEntries) return 0;
00210 utRef *match = (utRef*) bsearch((const void*)&t, arr, (int)nbrEntries,
00211 sizeof(utRef), compare);
00212 return (match) ? match-arr : (long)-1;
00213 }
00214
00215 template <class utRef, unsigned long FixedSize>
00216 void
00217 UT_RefFixArray<utRef,FixedSize>::sort(int (*compare)(const void*, const void*))
00218 {
00219 qsort(arr, (int)nbrEntries, sizeof(utRef), compare);
00220 }
00221
00222 template <class utRef, unsigned long FixedSize>
00223 unsigned long
00224 UT_RefFixArray<utRef,FixedSize>::apply(int (*apFct)(utRef &t, void *d), void *d)
00225 {
00226 unsigned long i;
00227 for (i = 0; i < nbrEntries; i++)
00228 if (apFct(arr[i],d)) break;
00229 return i;
00230 }
00231