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
|
/* struct::set - critcl - layer 0 declarations
* Tcl_ObjType 'set'.
*/
#include <string.h>
#include "s.h"
/* .................................................. */
static void free_rep (Tcl_Obj* obj);
static void dup_rep (Tcl_Obj* obj, Tcl_Obj* dup);
static void string_rep (Tcl_Obj* obj);
static int from_any (Tcl_Interp* ip, Tcl_Obj* obj);
static
Tcl_ObjType s_type = {
"tcllib::struct::set/critcl::set",
free_rep,
dup_rep,
string_rep,
from_any
};
/* .................................................. */
int
s_get (Tcl_Interp* interp, Tcl_Obj* o, SPtr* sStar)
{
if (o->typePtr != &s_type) {
int res = from_any (interp, o);
if (res != TCL_OK) {
return res;
}
}
*sStar = (SPtr) o->internalRep.otherValuePtr;
return TCL_OK;
}
Tcl_Obj*
s_new (SPtr s)
{
Tcl_Obj* o = Tcl_NewObj();
Tcl_InvalidateStringRep(o);
o->internalRep.otherValuePtr = s;
o->typePtr = &s_type;
return o;
}
Tcl_ObjType*
s_stype (void)
{
return &s_type;
}
Tcl_ObjType*
s_ltype (void)
{
static Tcl_ObjType* l;
if (l == NULL) {
l = Tcl_GetObjType ("list");
}
return l;
}
/* .................................................. */
static void
free_rep (Tcl_Obj* o)
{
s_free ((SPtr) o->internalRep.otherValuePtr);
o->internalRep.otherValuePtr = NULL;
}
static void
dup_rep (Tcl_Obj* obj, Tcl_Obj* dup)
{
SPtr s = s_dup ((SPtr) obj->internalRep.otherValuePtr);
dup->internalRep.otherValuePtr = s;
dup->typePtr = &s_type;
}
static void
string_rep (Tcl_Obj* obj)
{
SPtr s = (SPtr) obj->internalRep.otherValuePtr;
int numElems = s->el.numEntries;
/* iterate hash table and generate list-like string rep */
# define LOCAL_SIZE 20
int localFlags[LOCAL_SIZE], *flagPtr;
int localLen [LOCAL_SIZE], *lenPtr;
register int i;
char *elem, *dst;
int length;
Tcl_HashSearch hs;
Tcl_HashEntry* he;
/*
* Convert each key of the hash to string form and then convert it to
* proper list element form, adding it to the result buffer. */
/*
* Pass 1: estimate space, gather flags.
*/
if (numElems <= LOCAL_SIZE) {
flagPtr = localFlags;
lenPtr = localLen;
} else {
flagPtr = (int *) ckalloc((unsigned) numElems*sizeof(int));
lenPtr = (int *) ckalloc((unsigned) numElems*sizeof(int));
}
obj->length = 1;
for(i = 0, he = Tcl_FirstHashEntry(&s->el, &hs);
he != NULL;
he = Tcl_NextHashEntry(&hs), i++) {
elem = Tcl_GetHashKey (&s->el, he);
lenPtr [i] = strlen (elem);
obj->length += Tcl_ScanCountedElement(elem, lenPtr[i],
&flagPtr[i]) + 1;
}
/*
* Pass 2: copy into string rep buffer.
*/
obj->bytes = ckalloc((unsigned) obj->length);
dst = obj->bytes;
for(i = 0, he = Tcl_FirstHashEntry(&s->el, &hs);
he != NULL;
he = Tcl_NextHashEntry(&hs), i++) {
elem = Tcl_GetHashKey (&s->el, he);
dst += Tcl_ConvertCountedElement(elem, lenPtr[i],
dst, flagPtr[i]);
*dst = ' ';
dst++;
}
if (flagPtr != localFlags) {
ckfree((char *) flagPtr);
ckfree((char *) lenPtr);
}
if (dst == obj->bytes) {
*dst = 0;
} else {
dst--;
*dst = 0;
}
obj->length = dst - obj->bytes;
}
static int
from_any (Tcl_Interp* ip, Tcl_Obj* obj)
{
/* Go through an intermediate list rep.
*/
int lc, i, new;
Tcl_Obj** lv;
Tcl_ObjType* oldTypePtr;
SPtr s;
if (Tcl_ListObjGetElements (ip, obj, &lc, &lv) != TCL_OK) {
return TCL_ERROR;
}
/*
* Remember the old type after the conversion to list, or we will try to
* free a list intrep using the free-proc of whatever type the word had
* before. For example 'parsedvarname'. That would be bad. Segfault like
* bad.
*/
oldTypePtr = obj->typePtr;
/* Now, if the value was pure we forcibly generate the string-rep, to
* capture the existing semantics of the value. Because we now enter the
* realm of unordered, and the actual value may not be. If so, then not
* having the string-rep will later cause the generation of an arbitrarily
* ordered string-rep when the value is shimmered to some other type. This
* is most visible for lists, which are ordered. A shimmer list->set->list
* may reorder the elements if we do not capture their order in the
* string-rep.
*
* See test case -15.0 in sets.testsuite demonstrating this.
* Disable the Tcl_GetString below and see the test fail.
*/
Tcl_GetString (obj);
/* Gen hash table from list */
s = (SPtr) ckalloc (sizeof (S));
Tcl_InitHashTable(&s->el, TCL_STRING_KEYS);
for (i=0; i < lc; i++) {
(void) Tcl_CreateHashEntry(&s->el,
Tcl_GetString (lv[i]), &new);
}
/*
* Free the old internalRep before setting the new one. We do this as
* late as possible to allow the conversion code, in particular
* Tcl_ListObjGetElements, to use that old internalRep.
*/
if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
oldTypePtr->freeIntRepProc(obj);
}
obj->internalRep.otherValuePtr = s;
obj->typePtr = &s_type;
return TCL_OK;
}
/* .................................................. */
int
s_size (SPtr a)
{
return a->el.numEntries;
}
int
s_empty (SPtr a)
{
return (a->el.numEntries == 0);
}
void
s_free (SPtr a)
{
Tcl_DeleteHashTable(&a->el);
ckfree ((char*) a);
}
SPtr
s_dup (SPtr a)
{
SPtr s = (SPtr) ckalloc (sizeof (S));
Tcl_InitHashTable(&s->el, TCL_STRING_KEYS);
if (!a) return s;
s_add (s, a, NULL);
return s;
}
int
s_contains (SPtr a, const char* item)
{
return Tcl_FindHashEntry (&a->el, item) != NULL;
}
SPtr
s_difference (SPtr a, SPtr b)
{
int new;
Tcl_HashSearch hs;
Tcl_HashEntry* he;
CONST char* key;
SPtr s;
/* a - nothing = a. Just duplicate */
if (!b->el.numEntries) {
return s_dup (a);
}
s = (SPtr) ckalloc (sizeof (S));
Tcl_InitHashTable(&s->el, TCL_STRING_KEYS);
/* nothing - b = nothing */
if (!a->el.numEntries) return s;
/* Have to get it the hard way, no shortcut */
for(he = Tcl_FirstHashEntry(&a->el, &hs);
he != NULL;
he = Tcl_NextHashEntry(&hs)) {
key = Tcl_GetHashKey (&a->el, he);
if (Tcl_FindHashEntry (&b->el, key) != NULL) continue;
/* key is in a, not in b <=> in (a-b) */
(void*) Tcl_CreateHashEntry(&s->el, key, &new);
}
return s;
}
SPtr
s_intersect (SPtr a, SPtr b)
{
int new;
Tcl_HashSearch hs;
Tcl_HashEntry* he;
CONST char* key;
SPtr s = (SPtr) ckalloc (sizeof (S));
Tcl_InitHashTable(&s->el, TCL_STRING_KEYS);
/* Shortcut when we know that the result is empty */
if (!a->el.numEntries) return s;
if (!b->el.numEntries) return s;
/* Ensure that we iterate over the smaller of the two sets */
if (b->el.numEntries < a->el.numEntries) {
SPtr t = a ; a = b ; b = t;
}
for(he = Tcl_FirstHashEntry(&a->el, &hs);
he != NULL;
he = Tcl_NextHashEntry(&hs)) {
key = Tcl_GetHashKey (&a->el, he);
if (Tcl_FindHashEntry (&b->el, key) == NULL) continue;
/* key is in a, in b <=> in (a*b) */
(void*) Tcl_CreateHashEntry(&s->el, key, &new);
}
return s;
}
SPtr
s_union (SPtr a, SPtr b)
{
int new;
Tcl_HashSearch hs;
Tcl_HashEntry* he;
CONST char* key;
SPtr s = (SPtr) ckalloc (sizeof (S));
Tcl_InitHashTable(&s->el, TCL_STRING_KEYS);
s_add (s, a, NULL);
s_add (s, b, NULL);
return s;
}
void
s_add (SPtr a, SPtr b, int* newPtr)
{
int new, nx = 0;
Tcl_HashSearch hs;
Tcl_HashEntry* he;
CONST char* key;
if (b->el.numEntries) {
for(he = Tcl_FirstHashEntry(&b->el, &hs);
he != NULL;
he = Tcl_NextHashEntry(&hs)) {
key = Tcl_GetHashKey (&b->el, he);
(void*) Tcl_CreateHashEntry(&a->el, key, &new);
if (new) {nx = 1;}
}
}
if(newPtr) {*newPtr = nx;}
}
void
s_add1 (SPtr a, const char* item)
{
int new;
(void*) Tcl_CreateHashEntry(&a->el, item, &new);
}
void
s_subtract (SPtr a, SPtr b, int* delPtr)
{
int new;
Tcl_HashSearch hs;
Tcl_HashEntry* he, *dhe;
CONST char* key;
int dx = 0;
if (b->el.numEntries) {
for(he = Tcl_FirstHashEntry(&b->el, &hs);
he != NULL;
he = Tcl_NextHashEntry(&hs)) {
key = Tcl_GetHashKey (&b->el, he);
dhe = Tcl_FindHashEntry(&a->el, key);
if (!dhe) continue;
/* Key is known, to be removed */
dx = 1;
Tcl_DeleteHashEntry (dhe);
}
}
if(delPtr) {*delPtr = dx;}
}
void
s_subtract1 (SPtr a, const char* item)
{
Tcl_HashEntry* he;
he = Tcl_FindHashEntry(&a->el, item);
if (!he) return;
Tcl_DeleteHashEntry (he);
}
int
s_equal (SPtr a, SPtr b)
{
/* (a == b) <=> (|a| == |b| && (a-b) = {})
*/
int res = 0;
if (s_size (a) == s_size(b)) {
SPtr t = s_difference (a, b);
res = s_empty (t);
s_free (t);
}
return res;
}
int
s_subsetof (SPtr a, SPtr b)
{
/* (a <= b) <=> (|a| <= |b| && (a-b) = {})
*/
int res = 0;
if (s_size (a) <= s_size(b)) {
SPtr t = s_difference (a, b);
res = s_empty (t);
s_free (t);
}
return res;
}
/* .................................................. */
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|