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 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
|
#include "gis.h"
#include <stdlib.h>
#define INCR 10
#define SHIFT 6
static int NCATS = 1<<SHIFT;
#define NODE struct Cell_stats_node
static int next_node (struct Cell_stats *);
static int init_node(NODE *,int,int);
/*!
* \brief initialize cell stats
*
* This routine, which must be called first, initializes the Cell_stats
* structure <b>s.</b>
*
* \param s
* \return int
*/
/*!
* \brief
*
* Set the count for NULL-values to zero.
*
* \return int
*/
int G_init_cell_stats(struct Cell_stats *s)
{
s->N = 0;
s->tlen = INCR;
s->node = (NODE *) G_malloc (s->tlen * sizeof (NODE));
s->null_data_count = 0;
return 1;
}
/*!
* \brief add data to cell stats
*
* The <b>n</b> CELL values in the <b>data</b>
* array are inserted (and counted) in the Cell_stats structure <b>s.</b>
*
* \param data
* \param n
* \param s
* \return int
*/
/*!
* \brief
*
* Look for NULLs and update the
* NULL-value count.
*
* \return int
*/
int G_update_cell_stats (CELL *cell, int n, struct Cell_stats *s)
{
CELL cat;
register int p,q;
int idx, offset;
int N;
register NODE *node, *pnode;
register NODE *new_node;
if (n <= 0) return 1;
node = s->node;
/* first non-null node is special case */
if ((N = s->N) == 0)
{
cat = *cell++;
while(G_is_c_null_value(&cat))
{
s->null_data_count++;
cat = *cell++;
n--;
}
if(n>0) /* if there are some non-null cells */
{
N = 1;
if (cat < 0)
{
idx = -((-cat) >> SHIFT) - 1;
offset = cat + ((-idx) << SHIFT) - 1;
}
else
{
idx = cat >> SHIFT;
offset = cat - (idx << SHIFT);
}
fflush(stderr);
init_node (&node[1], idx, offset);
node[1].right = 0;
n--;
}
}
while (n-- > 0)
{
cat = *cell++;
if(G_is_c_null_value(&cat))
{
s->null_data_count++;
continue;
}
if (cat < 0)
{
idx = -((-cat) >> SHIFT) - 1;
offset = cat + ((-idx) << SHIFT) - 1;
}
else
{
idx = cat >> SHIFT;
offset = cat - (idx << SHIFT);
}
q = 1;
while (q > 0)
{
pnode = &node[p = q];
if (pnode->idx == idx)
{
pnode->count[offset]++;
break;
}
if (pnode->idx > idx)
q = pnode->left; /* go left */
else
q = pnode->right; /* go right */
}
if (q > 0)
continue; /* found */
/* new node */
N++;
/* grow the tree? */
if (N >= s->tlen)
{
node = (NODE *) G_realloc ((char *) node, sizeof(NODE) * (s->tlen += INCR));
pnode = &node[p]; /* realloc moves node, must reassign pnode */
}
/* add node to tree */
init_node (new_node = &node[N], idx, offset);
if (pnode->idx > idx)
{
new_node->right = -p; /* create thread */
pnode->left = N; /* insert left */
}
else
{
new_node->right = pnode->right; /* copy right link/thread */
pnode->right = N; /* add right */
}
} /* while n-- > 0 */
s->N = N;
s->node = node;
return 0;
}
static int init_node(NODE *node,int idx,int offset)
{
register long *count;
register int i;
count = node->count = (long *) G_calloc (i = NCATS, sizeof(long));
while(i--)
*count++ = 0;
node->idx = idx;
node->count[offset] = 1;
node->left = 0;
return 0;
}
/*!
* \brief random query of cell stats
*
* This routine allows a random query of the
* Cell_stats structure <b>s.</b> The <b>count</b> associated with the
* raster value <b>cat</b> is set. The routine returns 1 if <b>cat</b> was
* found in the structure, 0 otherwise.
*
* \param cat
* \param count
* \param s
* \return int
*/
/*!
* \brief
*
* Allow finding the count for the
* NULL-value
*
* \return int
*/
int G_find_cell_stat (
CELL cat,
long *count,
struct Cell_stats *s)
{
register int q;
register int idx;
int offset;
*count = 0;
if(G_is_c_null_value(&cat))
{
*count = s->null_data_count;
return (*count != 0);
}
if (s->N <= 0)
return 0;
/*
if (cat < 0)
{
idx = -(-cat/NCATS) - 1;
offset = cat - idx*NCATS - 1;
}
else
{
idx = cat/NCATS;
offset = cat - idx*NCATS;
}
*/
if (cat < 0)
{
idx = -((-cat) >> SHIFT) - 1;
offset = cat + ((-idx) << SHIFT) - 1;
}
else
{
idx = cat >> SHIFT;
offset = cat - (idx << SHIFT);
}
q = 1;
while (q > 0)
{
if (s->node[q].idx == idx)
{
*count = s->node[q].count[offset];
return (*count != 0);
}
if (s->node[q].idx > idx)
q = s->node[q].left; /* go left */
else
q = s->node[q].right; /* go right */
}
return 0;
}
/*!
* \brief reset/rewind cell stats
*
* The structure <b>s</b> is rewound (i.e., positioned at the first
* raster category) so that sorted sequential retrieval can begin.
*
* \param s
* \return int
*/
int G_rewind_cell_stats (struct Cell_stats *s)
{
int q;
if (s->N <= 0)
return 1;
/* start at root and go all the way to the left */
s->curp = 1;
while (q = s->node[s->curp].left)
s->curp = q;
s->curoffset = -1;
return 0;
}
static int next_node (struct Cell_stats *s)
{
int q;
/* go to the right */
s->curp = s->node[s->curp].right;
if (s->curp == 0) /* no more */
return 0;
if (s->curp < 0) /* thread. stop here */
{
s->curp = -(s->curp) ;
return 1;
}
while (q = s->node[s->curp].left) /* now go all the way left */
s->curp = q;
return 1;
}
/*!
* \brief retrieve sorted cell stats
*
* Retrieves the next <b>cat,count</b>
* combination from the structure <b>s.</b> Returns 0 if there are no more
* items, non-zero if there are more.
* For example:
*
\code
struct Cell_stats s;
CELL cat;
long count;
// updating <b>s</b> occurs here
G_rewind_cell_stats(&s);
while (G_next_cell_stat(&cat,&count,&s)
fprintf(stdout, "%ld %ld\n", (long) cat, count);
\endcode
*
* \param cat
* \param count
* \param s
* \return int
*/
/*!
* \brief
*
* Do not return a record for the
* NULL-value
*
* \return int
*/
int G_next_cell_stat (
CELL *cat,
long *count,
struct Cell_stats *s)
{
int idx;
/* first time report stats for null */
/* decided not to return stats for null in this function
static int null_reported = 0;
if(!null_reported && s->null_data_count > 0)
{
*count = s->null_data_count;
G_set_c_null_value(&cat,1);
null_reported = 1;
return 1;
}
*/
if (s->N <= 0)
return 0;
for(;;)
{
s->curoffset++;
if (s->curoffset >= NCATS)
{
if(!next_node(s))
return 0;
s->curoffset = -1;
continue;
}
if (*count = s->node[s->curp].count[s->curoffset])
{
idx = s->node[s->curp].idx;
/*
if (idx < 0)
*cat = idx*NCATS + s->curoffset+1;
else
*cat = idx*NCATS + s->curoffset;
*/
if (idx < 0)
*cat = -((-idx)<<SHIFT) + s->curoffset+1;
else
*cat = (idx<<SHIFT) + s->curoffset;
return 1;
}
}
}
/*!
* \brief
*
* Get a number of null values from stats structure. Note: when reporting
* values which appear in a map using G_next_cell_stats(), to get stats for
* null, call G_get_stats_for_null_value() first, since
* G_next_cell_stats() does not report stats for null.
*
* \param count
* \param s
* \return int
*/
int G_get_stats_for_null_value (long *count, struct Cell_stats *s)
{
*count = s->null_data_count;
return 1;
}
/*!
* \brief free cell stats
*
* The memory associated with structure <b>s</b> is freed. This routine may be
* called any time after calling<i>G_init_cell_stats.</i>
*
* \param s
* \return int
*/
int G_free_cell_stats (struct Cell_stats *s)
{
int i;
for (i = 1; i <= s->N; i++)
free (s->node[i].count);
free (s->node);
return 0;
}
|