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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*-----------------------------------------------------------------------
File : clb_ddarrays.c
Author: Stephan Schulz
Contents
Funktions realising the dynamic array type for doubles.
Copyright 1998, 1999 by the author.
This code is released under the GNU General Public Licence and
the GNU Lesser General Public License.
See the file COPYING in the main E directory for details..
Run "eprover -h" for contact information.
Changes
<1> Sun Aug 8 22:49:36 GMT 1999
Copied from clb_pdarrays.c
-----------------------------------------------------------------------*/
#include "clb_ddarrays.h"
/*---------------------------------------------------------------------*/
/* Global Variables */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Internal Functions */
/*---------------------------------------------------------------------*/
/*---------------------------------------------------------------------*/
/* Exported Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: DDArrayAlloc()
//
// Return an initialized dynamic array of size init_size where all
// elements are interpreted as pointers and initialized to NULL.
//
// Global Variables: -
//
// Side Effects : Memory Operations
//
/----------------------------------------------------------------------*/
DDArray_p DDArrayAlloc(long init_size, long grow)
{
DDArray_p handle = DDArrayCellAlloc();
long i;
assert(init_size > 0);
assert(grow > 0);
handle->size = init_size;
handle->grow = grow;
handle->array = SizeMalloc(handle->size*sizeof(double));
for(i=0; i<handle->size; i++)
{
handle->array[i] = 0.0;
}
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: DDArrayFree()
//
// Free a DDArray. Leaves elements untouched.
//
// Global Variables: -
//
// Side Effects : Memory Operations
//
/----------------------------------------------------------------------*/
void DDArrayFree(DDArray_p junk)
{
assert(junk);
assert(junk->size > 0);
assert(junk->array);
SizeFree(junk->array, junk->size*sizeof(double));
DDArrayCellFree(junk);
}
/*-----------------------------------------------------------------------
//
// Function: DDArayEnlarge()
//
// Enlarge array enough to accomodate idx.
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
void DDArayEnlarge(DDArray_p array, long idx)
{
double *tmp;
long old_size, i;
old_size = array->size;
tmp = array->array;
array->size = ((idx/array->grow)+1)*array->grow;
array->array = SizeMalloc(array->size * sizeof(double));
memcpy(array->array, tmp, old_size*sizeof(double));
SizeFree(tmp, old_size * sizeof(double));
for(i=old_size; i<array->size; i++)
{
array->array[i] = 0.0;
}
}
/*-----------------------------------------------------------------------
//
// Function: DDArrayDebugPrint()
//
// Print the array, only for debugging.
//
// Global Variables:
//
// Side Effects :
//
/----------------------------------------------------------------------*/
void DDArrayDebugPrint(FILE* out, DDArray_p array, long size)
{
long i;
for(i = 0; i<size; i++)
{
fprintf(out, " %5.3f ", DDArrayElement(array, i));
if(((i+1) % 10)==0)
{
fprintf(out, "\n");
}
}
fprintf(out, "\n");
}
/*-----------------------------------------------------------------------
//
// Function: DDArrayAdd()
//
// Add the first limit elements from new to the corresponding entries
// in collect. All entries are interpreted as numerical.
//
// Global Variables: -
//
// Side Effects : Changes collect.
//
/----------------------------------------------------------------------*/
void DDArrayAdd(DDArray_p collect, DDArray_p data, long limit)
{
long i;
double old, new;
assert(collect);
assert(data);
for(i=0; i<limit; i++)
{
old = DDArrayElement(collect, i);
new = DDArrayElement(data, i);
DDArrayAssign(collect, i, old+new);
}
}
/*-----------------------------------------------------------------------
//
// Function: DDArraySelectPart()
//
// Find a value d with at least part*(last+1) values >= d and
// (1-part)*(last+1) values <= d in array.
//
// Global Variables: -
//
// Side Effects : Changes order in array.
//
/----------------------------------------------------------------------*/
double DDArraySelectPart(DDArray_p array, double part, long size)
{
double pivot, tmp, *arr;
long i,j, start, end, rank1, rank2;
assert((0 <= part) && (part <= 1));
assert(size>0);
tmp = (size-1)*part; /* -1 due to C's indexing */
rank1 = ((long)tmp);
rank2 = ((long)(tmp+0.5));
start = 0;
end = size-1;
/* printf("# Rank1,2: %ld, %ld\n", rank1, rank2); */
assert(array->size >= size);
arr = array->array;
while(start!=end)
{
/* Pick a good pseudo-pivot to make worst case-behaviour less
likely */
pivot = (arr[start]+arr[(start+end)/2]+arr[end])/3.0;
/* printf("Pivot: %f\n", pivot); */
i=start;
j=end;
while(i != j)
{
/* printf("%ld, %ld\n", i, j); */
while((i<j) && (arr[i] <= pivot))
{
i++;
}
while((j>i) && (arr[j] > pivot))
{
j--;
}
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
/* printf("Hier - i= %ld\n",i);
DDArrayDebugPrint(stdout, array, size); */
if(i > rank1)
{
end = i-1;
}
else
{
start = i;
}
}
if(rank2!=rank1)
{
/* Now find the second value. We know that all values with index
> rank1 are >= arr[rank1] and that all values >
arr[rank1] have index > rank1 -> we only need to search for
the minimum of the second part of the array */
assert(rank1!=size-1); /* Should be impossible, otherwise part =
1.0 and rank1==rank2 */
tmp = arr[start+1];
for(i=start+1; i<size; i++)
{
tmp = MIN(tmp, arr[i]);
}
}
else
{
tmp = arr[start];
}
return (arr[start]+tmp)/2;
}
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|