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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
/* sequence.c -- Routines for dealing with sequences
*
* Copyright (c) 1994-2008 Carnegie Mellon University. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The name "Carnegie Mellon University" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For permission or any legal
* details, please contact
* Carnegie Mellon University
* Center for Technology Transfer and Enterprise Creation
* 4615 Forbes Avenue
* Suite 302
* Pittsburgh, PA 15213
* (412) 268-7393, fax: (412) 268-7395
* innovation@andrew.cmu.edu
*
* 4. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by Computing Services
* at Carnegie Mellon University (http://www.cmu.edu/computing/)."
*
* CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
* THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <config.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <sysexits.h>
#include "seqset.h"
#include "string.h"
#include "util.h"
#include "xmalloc.h"
#define SETGROWSIZE 30
struct seq_range {
unsigned low;
unsigned high;
};
#define RANGE_SIZE sizeof(struct seq_range)
struct seqset {
struct seq_range *set;
size_t len;
size_t alloc;
unsigned current;
unsigned prev;
unsigned maxval;
int flags;
};
/*
* Allocate and return a new seqset object.
*
* `maxval' is the maximum insertable value, currently used only to
* expand the `*' syntax when parsing sequences from a string.
*
* `flags' is either
*
* SEQ_SPARSE - the behaviour you expected
*
* SEQ_MERGE - assumes that seqset_add() is going to be called
* with monotonically increasing numbers, and treats
* interior ranges of numbers which were not explicitly
* excluded (with ismember=0) as if they had been
* included. Used to reduce fragmentation in SEEN lists.
*/
EXPORTED seqset_t *seqset_init(unsigned maxval, int flags)
{
seqset_t *seq = xzmalloc(sizeof(seqset_t));
/* make sure flags are sane - be explicit about what
* sort of list we're building */
if (flags != SEQ_SPARSE && flags != SEQ_MERGE)
fatal("invalid flags", EX_SOFTWARE);
seq->maxval = maxval;
seq->flags = flags;
return seq;
}
/*
* Add a number `num' to the sequence set `seq'. The `ismember'
* argument is normally 1, but affects the result for SEQ_MERGE
* sequences. Currently assumes that it will be called in
* monotonically increasing order of `num's.
*/
EXPORTED void seqset_add(seqset_t *seq, unsigned num, int ismember)
{
if (!seq) return;
/* there are some cases where we want to make sure something is added
* as an initial value and then re-add it again later, so if we get
* the same number multiple times, that's OK */
if (ismember && num == seq->prev && seq->len && seq->set[seq->len-1].high == num)
return;
if (num > seq->prev) {
if (!ismember) {
seq->prev = num;
return;
}
/* as if the previous number was given to us */
if (seq->flags & SEQ_SPARSE)
seq->prev = num - 1;
}
/* do we need to add a new set? */
if (!seq->set || seq->set[seq->len-1].high < seq->prev || num <= seq->prev) {
if (seq->len == seq->alloc) {
seq->alloc += SETGROWSIZE;
seq->set = xrealloc(seq->set, seq->alloc * RANGE_SIZE);
}
seq->set[seq->len].low = num;
seq->len++;
}
/* update the final high value */
seq->set[seq->len-1].high = num;
seq->prev = num;
}
/*
* Remove a number `num' from the sequence set `seq'.
*/
EXPORTED void seqset_remove(seqset_t *seq, unsigned num)
{
if (!seqset_ismember(seq, num)) return;
struct seq_range *cur = &seq->set[seq->current];
if (cur->low == cur->high) {
/* Single value - remove from the set */
memmove(cur, cur+1, (seq->len - seq->current - 1) * RANGE_SIZE);
seq->len--;
}
else if (num == cur->low) {
/* Increment the low value */
cur->low = num + 1;
}
else if (num == cur->high) {
/* Decrement the high value */
cur->high = num - 1;
}
else {
/* Split the range - insert a new one after current */
if (seq->len == seq->alloc) {
seq->alloc += SETGROWSIZE;
seq->set = xrealloc(seq->set, seq->alloc * RANGE_SIZE);
}
cur = &seq->set[seq->current]; // xrealloc may have changed ptr location
memmove(cur+1, cur, (seq->len - seq->current) + RANGE_SIZE);
seq->len++;
cur->high = num - 1;
(cur+1)->low = num + 1;
}
}
/* read the final number from a sequence string and return it */
EXPORTED unsigned seq_lastnum(const char *list)
{
const char *tail;
uint32_t retval = 0;
/* empty */
if (!list) return 0;
if (!list[0]) return 0;
/* find the end of the string */
tail = list + strlen(list);
/* work back until first non-digit */
while (tail > list && cyrus_isdigit(tail[-1]))
tail--;
/* read the number */
if (parseuint32(tail, NULL, &retval))
retval = 0;
return retval;
}
/***************** SEQSET STUFF ******************/
/* Comparator function that sorts ranges by the low value,
and coalesces intersecting ranges to have the same high value */
static int comp_rangesort(const void *v1, const void *v2)
{
struct seq_range *r1 = (struct seq_range *) v1;
struct seq_range *r2 = (struct seq_range *) v2;
int ret = r1->low - r2->low;
if (ret) return ret;
return r1->high - r2->high;
}
static void seqset_simplify(seqset_t *seq)
{
unsigned out = 0;
unsigned i;
/* nothing to simplify */
if (!seq->len)
return;
/* Sort the ranges using our special comparator */
qsort(seq->set, seq->len, sizeof(struct seq_range), comp_rangesort);
/* Merge intersecting/adjacent ranges */
for (i = 1; i < seq->len; i++) {
if (seq->set[out].high + 1 < seq->set[i].low) {
/* these are disjoint */
out++;
if (out != i)
seq->set[out] = seq->set[i];
}
else if (seq->set[out].high < seq->set[i].high) {
seq->set[out].high = seq->set[i].high;
}
}
/* final length */
seq->len = out+1;
}
static int read_num(const char **input, unsigned maxval, unsigned *res)
{
const char *ptr = *input;
if (*ptr == '*') {
*res = maxval ? maxval : UINT_MAX;
ptr++;
*input = ptr;
return 0;
}
else if (cyrus_isdigit((int) *ptr)) {
*res = 0;
while (cyrus_isdigit((int) *ptr)) {
*res = (*res)*10 + *ptr - '0';
ptr++;
}
*input = ptr;
return 0;
}
/* not expected */
return -1;
}
/*
* Parse a sequence into an array of sorted & merged ranges.
*/
EXPORTED seqset_t *seqset_parse(const char *sequence,
seqset_t *set,
unsigned maxval)
{
unsigned start = 0, end = 0;
/* short circuit no sequence */
if (!sequence) return NULL;
if (!set) set = seqset_init(maxval, SEQ_SPARSE);
while (*sequence) {
if (read_num(&sequence, maxval, &start))
fatal("invalid sequence", EX_SOFTWARE);
if (*sequence == ':') {
sequence++;
if (read_num(&sequence, maxval, &end))
fatal("invalid sequence", EX_SOFTWARE);
}
else
end = start;
if (start > end) {
unsigned i = end;
end = start;
start = i;
}
if (set->len == set->alloc) {
set->alloc += SETGROWSIZE;
set->set = xrealloc(set->set, set->alloc * sizeof(struct seq_range));
}
set->set[set->len].low = start;
set->set[set->len].high = end;
set->len++;
if (*sequence == ',')
sequence++;
/* could test for invalid chars here, but the number parser next
* time through will grab them, so no need */
}
seqset_simplify(set);
return set;
}
/* Comparator function that checks if r1 is a subset of r2 */
static int comp_subset(const void *v1, const void *v2)
{
struct seq_range *r1 = (struct seq_range *) v1;
struct seq_range *r2 = (struct seq_range *) v2;
if (r1->low < r2->low) return -1;
if (r1->high > r2->high) return 1;
return 0;
}
/*
* Return nonzero iff 'num' is included in 'sequence'
*/
EXPORTED int seqset_ismember(const seqset_t *seq, unsigned num)
{
struct seq_range key = {num, num};
struct seq_range *found;
/* Short circuit no list! */
if (!seq) return 0;
if (!seq->len) return 0;
/* Short circuit if we're outside all ranges */
if ((num < seq->set[0].low) || (num > seq->set[seq->len-1].high)) {
return 0;
}
seqset_t *backdoor = (seqset_t *)seq;
/* Move one set ahead if necessary (avoids bsearch in the common case of
incrementing through the list) */
if (num > backdoor->set[backdoor->current].high) {
if (backdoor->current + 1 >= backdoor->len)
return 0; /* no more sequences! */
if (num < backdoor->set[backdoor->current+1].low)
return 0; /* in the gap still */
backdoor->current++; /* move ahead */
}
/* maybe we're in this range */
if (num >= backdoor->set[backdoor->current].low &&
num <= backdoor->set[backdoor->current].high)
return 1;
/* Fall back to full search */
found = bsearch(&key, backdoor->set, backdoor->len,
sizeof(struct seq_range), comp_subset);
if (found) {
/* track the range we found ourselves in */
backdoor->current = found - backdoor->set;
return 1;
}
return 0;
}
/*
* Return the first number in the sequence, or 0
* if the sequence is empty.
*/
EXPORTED unsigned seqset_first(const seqset_t *seq)
{
if (!seq) return 0;
return (seq->len ? seq->set[0].low : 0);
}
/*
* Return the last number in the sequence, or 0
* if the sequence is empty.
*/
EXPORTED unsigned seqset_last(const seqset_t *seq)
{
if (!seq) return 0;
return (seq->len ? seq->set[seq->len-1].high : 0);
}
/* NOTE: this assumes normalised, and also assumes that '1' is
* the first element */
EXPORTED unsigned seqset_firstnonmember(const seqset_t *seq)
{
if (!seq) return 1;
if (!seq->len) return 1;
if (seq->set[0].low != 1) return 1;
return seq->set[0].high + 1;
}
/*
* Iteration interface for sequences. Returns the next number
* in the sequence, or 0 if the end of the sequence has been
* reached.
* Interferes with the state used for seqset_add() so don't mix
* adding and iterating.
*/
EXPORTED unsigned seqset_getnext(const seqset_t *seq)
{
unsigned num;
unsigned i;
/* no sequence, there's no next value */
if (!seq) return 0;
/* finished? */
if (seq->prev == UINT_MAX) return 0;
seqset_t *backdoor = (seqset_t *)seq;
num = backdoor->prev + 1;
for (i = backdoor->current; i < backdoor->len; i++) {
if (num < backdoor->set[i].low)
num = backdoor->set[i].low;
if (num <= backdoor->set[i].high) {
backdoor->current = i;
backdoor->prev = num;
return num;
}
}
backdoor->prev = UINT_MAX;
return 0;
}
EXPORTED void seqset_reset(const seqset_t *seq)
{
if (!seq) return;
seqset_t *backdoor = (seqset_t *)seq;
backdoor->prev = 0;
backdoor->current = 0;
}
/*
* Merge the numbers in seqset `src' into seqset `dst'.
*/
/* NOTE - not sort safe! */
EXPORTED void seqset_join(seqset_t *dst, const seqset_t *src)
{
if (!src) return;
if (dst->len + src->len > dst->alloc) {
dst->alloc = dst->len + src->len;
dst->set =
xrealloc(dst->set, dst->alloc * sizeof(struct seq_range));
}
/* call them char * so the maths works out right */
memcpy((char *)dst->set + dst->len * sizeof(struct seq_range),
(char *)src->set, src->len * sizeof(struct seq_range));
dst->len += src->len;
seqset_simplify(dst);
}
static void format_num(struct buf *buf, unsigned i)
{
if (i == UINT_MAX)
buf_putc(buf, '*');
else
buf_printf(buf, "%u", i);
}
/*
* Format the seqset `seq' as a string. Returns a newly allocated
* string which must be free()d by the caller.
*/
EXPORTED char *seqset_cstring(const seqset_t *seq)
{
struct buf buf = BUF_INITIALIZER;
unsigned i;
if (!seq) return NULL;
if (!seq->len) return NULL;
for (i = 0; i < seq->len; i++) {
/* join with comma if not the first item */
if (i) buf_putc(&buf, ',');
/* single value only */
if (seq->set[i].low == seq->set[i].high)
format_num(&buf, seq->set[i].low);
/* value range */
else {
format_num(&buf, seq->set[i].low);
buf_putc(&buf, ':');
format_num(&buf, seq->set[i].high);
}
}
return buf_release(&buf);
}
/*
* Duplicate the given seqset.
*/
EXPORTED seqset_t *seqset_dup(const seqset_t *src)
{
if (!src) return NULL;
seqset_t *dst = (seqset_t *)xmemdup(src, sizeof(struct seqset));
dst->set = (struct seq_range *)xmemdup(src->set,
src->alloc * sizeof(struct seq_range));
return dst;
}
/*
* Free the given seqset (and any others chained to it)
*/
EXPORTED void seqset_free(seqset_t **l)
{
if (!*l) return;
free((*l)->set);
free(*l);
*l = NULL;
}
|