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 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
/*
Written by Pieter J. Schoenmakers <tiggr@ics.ele.tue.nl>
Copyright (C) 1996 Pieter J. Schoenmakers.
This file is part of TOM. TOM is distributed under the terms of the
TOM License, a copy of which can be found in the TOM distribution; see
the file LICENSE.
$Id: array.c,v 1.72 1998/08/19 08:04:32 tiggr Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "trt.h"
#include <tom/tom-r.h>
#define CONTENTS(X) ((ET *) ((X)->contents))
#define VOID_CONTENTS(X) ((X)->contents)
__inline__ void
trt_array_resize (struct _es_i_tom_Array *a, struct _es_i_tom_MutableArray *m,
int elt_size, int num)
{
if (m->capacity - a->length < num)
{
if (!m->capacity)
m->capacity = 4;
while (m->capacity - a->length < num)
m->capacity *= 2;
a->contents = xrealloc (a->contents, m->capacity * elt_size);
}
}
/* Raise a condition iff the INDEX is equal to or larger than the LENGTH. */
static __inline__ void
check_index (tom_object self, tom_int index, tom_int length)
{
if ((unsigned) index >= (unsigned) length)
trt_raise (0, self, 0, c_tom_Conditions_program_condition,
"bad index %d for array bound %d", (int) index, (int) length);
}
static void
array_remove_elements (tom_object self, tom_int start, tom_int length, int size)
{
struct _es_i_tom_Array *this;
struct _es_i_tom_MutableArray *that;
this = trt_ext_address (self, _ei_i_tom_Array);
that = trt_ext_address (self, _ei_i_tom_MutableArray);
start = TRT_SEND (_II_, self, SEL (_ii__adjustRange__ii_),
start, length, &length);
if (length)
{
memmove ((char *) this->contents + start * size,
(char *) this->contents + (start + length) * size,
(this->length - (start + length)) * size);
this->length -= length;
}
}
static __inline__ tom_object
array_with_elts (tom_object self, selector cmd, va_list ap,
enum trt_type_encoding te, int elt_size)
{
int i, n = cmd->in->num;
void *contents = xmalloc (n * elt_size);
tom_object inst;
for (i = 0; i < n; i++)
switch (te)
{
case TRT_TE_REFERENCE:
switch (cmd->in->args[i])
{
case TRT_TE_REFERENCE:
((tom_object *) contents)[i] = va_arg (ap, tom_object);
break;
default:
fail_arg:
unimplemented (__FUNCTION__ ": unhandled arg type %d",
cmd->in->args[i]);
break;
}
break;
case TRT_TE_INT:
switch (cmd->in->args[i])
{
case TRT_TE_INT:
((tom_int *) contents)[i] = va_arg (ap, tom_int);
break;
default:
goto fail_arg;
}
break;
default:
unimplemented (__FUNCTION__ ": unhandled array type %d", te);
break;
}
inst = TRT_SEND ((reference_imp), self, SEL (r_alloc));
inst = TRT_SEND (_PI_, inst, SEL (r_initWith_i_at_p), (tom_int) n, contents);
return inst;
}
#define ARRAY Array
/* Element type. */
#define ET tom_byte
/* Element type description. */
#define ETD TRT_TE_BYTE
/* Element prefix. */
#define EP byte
/* Capitalized element prefix. */
#define CP Byte
#define F b
#include "arraydef.c"
#undef ET
#undef ETD
#undef EP
#undef CP
#undef F
#define ET tom_char
#define ETD TRT_TE_CHAR
#define EP char
#define CP Char
#define F c
#include "arraydef.c"
#undef ET
#undef ETD
#undef EP
#undef CP
#undef F
#undef STRING_METHODS
#define ET tom_int
#define ETD TRT_TE_INT
#define EP int
#define CP Int
#define F i
#include "arraydef.c"
#undef ET
#undef ETD
#undef EP
#undef CP
#undef F
#define ET tom_long
#define ETD TRT_TE_LONG
#define EP long
#define CP Long
#define F l
#include "arraydef.c"
#undef ET
#undef ETD
#undef EP
#undef CP
#undef F
#define ET tom_float
#define ETD TRT_TE_FLOAT
#define EP float
#define CP Float
#define F f
#include "arraydef.c"
#undef ET
#undef ETD
#undef EP
#undef CP
#undef F
#define ET tom_double
#define ETD TRT_TE_DOUBLE
#define EP double
#define CP Double
#define F d
#include "arraydef.c"
#undef ET
#undef ETD
#undef EP
#undef CP
#undef F
#define NO_INIT_WITH_ENUMERATOR
#define ET selector
#define ETD TRT_TE_SELECTOR
#define EP selector
#define CP Selector
#define F s
#include "arraydef.c"
#undef ET
#undef ETD
#undef EP
#undef CP
#undef F
#define ET tom_byte
#define ETD TRT_TE_BOOLEAN
#define EP boolean
#define CP Boolean
#define F o
#include "arraydef.c"
#undef ET
#undef ETD
#undef EP
#undef CP
#undef F
#undef NO_INIT_WITH_ENUMERATOR
#define ET tom_object
#define ETD TRT_TE_REFERENCE
#define EP object
#define CP Object
#define F r
#define DO_ARRAY_METHODS
#include "arraydef.c"
#undef ETD
#undef EP
#undef CP
#undef F
/* Subsidiary for the Indexed elements and elements: methods. */
static void
x_elements (tom_object self, selector cmd, builtin_return_type *rt,
tom_int start, tom_int num, tom_int length, va_list ap)
{
int i;
BZERO (rt, sizeof (*rt));
if (num != cmd->out->num)
fatal ("elements count mismatch return=%d actual=%d", cmd->out->num, num);
if (start < 0 || start + num > length)
fatal ("index range (%d, %d) does not fit array length %d",
start, num, length);
for (i = 0; i < num; i++)
{
void *p = i ? va_arg (ap, void *) : 0;
switch (cmd->out->args[i])
{
case TRT_TE_BOOLEAN:
case TRT_TE_BYTE:
{
tom_byte v = TRT_SEND ((byte_imp), self, SEL (b_at_i), i + start);
if (i)
*((tom_byte *) p) = v;
else
rt->i.i = v;
}
break;
case TRT_TE_CHAR:
{
tom_char v = TRT_SEND ((char_imp), self, SEL (c_at_i), i + start);
if (i)
*((tom_char *) p) = v;
else
rt->i.i = v;
}
break;
case TRT_TE_INT:
{
tom_int v = TRT_SEND ((int_imp), self, SEL (i_at_i), i + start);
if (i)
*((tom_int *) p) = v;
else
rt->i.i = v;
}
break;
case TRT_TE_LONG:
{
tom_long v = TRT_SEND ((long_imp), self, SEL (l_at_i), i + start);
if (i)
*((tom_long *) p) = v;
else
rt->l.l = v;
}
break;
case TRT_TE_FLOAT:
{
tom_float v = TRT_SEND ((float_imp), self, SEL (f_at_i), i + start);
if (i)
*((tom_float *) p) = v;
else
RETURN_SET_FLOAT (rt, v);
}
break;
case TRT_TE_DOUBLE:
{
tom_double v = TRT_SEND ((double_imp), self, SEL (d_at_i),
i + start);
if (i)
*((tom_double *) p) = v;
else
RETURN_SET_DOUBLE (rt, v);
}
break;
default:
fatal ("elements: unhandled type %d", cmd->out->args[i]);
break;
}
}
}
void
i_tom_Array_v_dealloc (tom_object self, selector cmd)
{
struct _es_i_tom_Array *this = trt_ext_address (self, _ei_i_tom_Array);
xfree (this->contents);
}
GENERIC_RETURN_TYPE
i_tom_Indexed_x_elements (tom_object self, selector cmd, ...)
{
tom_int length = TRT_SEND ((int_imp), self, SEL (i_length));
builtin_return_type rt;
va_list ap;
va_start (ap, cmd);
x_elements (self, cmd, &rt, 0, length, length, ap);
va_end (ap);
APPLY_ARGS_RETURN (&rt);
}
GENERIC_RETURN_TYPE
i_tom_Indexed_x_elements__ii_ (tom_object self, selector cmd,
tom_int start, tom_int num, ...)
{
tom_int length = TRT_SEND ((int_imp), self, SEL (i_length));
builtin_return_type rt;
va_list ap;
va_start (ap, num);
x_elements (self, cmd, &rt, start, num, length, ap);
va_end (ap);
APPLY_ARGS_RETURN (&rt);
}
tom_char
i_tom_ByteArray_c_at_i (tom_object self, selector cmd, tom_int index)
{
struct _es_i_tom_Array *this = trt_ext_address (self, _ei_i_tom_Array);
if ((unsigned) index >= (unsigned) this->length)
fatal ("bad index %d for array bound %d", (int) index, (int) this->length);
return ((tom_byte *) VOID_CONTENTS (this))[index];
}
tom_byte
i_tom_CharArray_b_at_i (tom_object self, selector cmd, tom_int index)
{
struct _es_i_tom_Array *this = trt_ext_address (self, _ei_i_tom_Array);
if ((unsigned) index >= (unsigned) this->length)
fatal ("bad index %d for array bound %d", (int) index, (int) this->length);
return ((tom_char *) VOID_CONTENTS (this))[index];
}
tom_int
i_tom_ByteArray_i_hash (tom_object self, selector cmd)
{
struct _es_i_tom_Array *this = trt_ext_address (self, _ei_i_tom_Array);
int h = 0, i, n = this->length > 32 ? 32 : this->length;
/* ZZZ A ByteString should hash in Unicode characters, according to its
mapping. */
for (i = 0; i < n; i++)
h = (h << 3) ^ ((tom_byte *) this->contents)[i];
return h;
}
tom_int
i_tom_IntArray_i_hash (tom_object self, selector cmd)
{
struct _es_i_tom_Array *this = trt_ext_address (self, _ei_i_tom_Array);
int h = 0, i, n = this->length > 32 ? 32 : this->length;
/* XXX Is this good hashing? */
for (i = 0; i < n; i++)
h = (h << 3) ^ ((tom_int *) this->contents)[i];
return h;
}
tom_int
i_tom_ByteString_i_hashRange__ii_ (tom_object self, selector cmd,
tom_int start, tom_int len)
{
struct _es_i_tom_Array *this = trt_ext_address (self, _ei_i_tom_Array);
int h = 0, i, n;
start = TRT_SEND (, self, SEL (_ii__adjustRange__ii_), start, len, &len);
n = len > 32 ? 32 : len;
/* ZZZ A ByteString should hash in Unicode characters, according to its
mapping. */
for (i = 0; i < n; i++)
h = (h << 3) ^ ((tom_byte *) this->contents)[start + i];
return h;
}
tom_int
i_tom_MutableByteArray_i_readRange__ii__fromByteArray_r_to_i
(tom_object self, selector cmd, tom_int start, tom_int length,
tom_object other, tom_int position)
{
struct _es_i_tom_Array *a;
struct _es_i_tom_MutableArray *m;
void *elts;
a = trt_ext_address (self, _ei_i_tom_Array);
m = trt_ext_address (self, _ei_i_tom_MutableArray);
if (position < 0 || position > a->length)
ABORT ();
elts = TRT_SEND ((pointer_imp), other, SEL (_pi__pointerToElements__ii_),
start, length, &length);
if (position + length > m->capacity)
trt_array_resize (a, m, sizeof (tom_byte), position + length - a->length);
memcpy ((char *) a->contents + position, elts, length * sizeof (tom_byte));
if (position + length > a->length)
a->length = position + length;
return length;
}
tom_object
byte_string_with_string (char *s, int n)
{
tom_object str = TRT_SEND ((reference_imp), _mr_c_tom_ByteString,
SEL (r_alloc));
s = memcpy (xmalloc (n), s, n);
return TRT_SEND ((reference_imp), str, SEL (r_init__pi_), s, n);
}
tom_object
byte_string_with_c_string (char *s)
{
return byte_string_with_string (s, strlen (s));
}
char *
c_string_with_length (char *buf, int len)
{
char *s = xmalloc (len + 1);
memcpy (s, buf, len);
s[len] = 0;
return s;
}
char *
c_string_with_tom_string (tom_object string)
{
tom_int l;
char *s;
s = TRT_SEND ((pointer_imp), string, SEL (_pi__byteStringContents), &l);
s = memcpy (xmalloc (1 + l), s, l);
s[l] = 0;
return s;
}
tom_object
c_tom_State_r_name (c_tom_State self, selector cmd)
{
return byte_string_with_string (self->c_tom_State.info.name.s,
self->c_tom_State.info.name.len);
}
#if 0
/* XXX Is this ever used? Should it? Can it be removed?
Sat May 18 22:37:05 1996, tiggr@tricky.es.ele.tue.nl */
i_tom_ByteString
new_byte_string (struct _es_i_tom_Array **this, tom_int len)
{
i_tom_ByteString str
= (void *) TRT_SEND ((reference_imp), _mr_c_tom_ByteString, SEL (r_alloc));
struct _es_i_tom_Array *that
= trt_ext_address ((tom_object) str, _ei_i_tom_Array);
that->length = len;
that->contents = xmalloc (that->length);
*this = that;
return str;
}
#endif
tom_byte
i_tom_ByteString_o_equalByteString_r (tom_object self, selector cmd,
tom_object o)
{
struct _es_i_tom_Array *this = trt_ext_address (self, _ei_i_tom_Array);
tom_int len;
void *s = TRT_SEND ((pointer_imp), o, SEL (_pi__byteStringContents), &len);
return this->length == len && !memcmp (this->contents, s, len);
}
tom_byte
i_tom_CharString_o_equalCharString_r (tom_object self, selector cmd,
tom_object o)
{
ABORT ();
}
tom_object
i_tom_MutableByteString_r_substring__ii_
(tom_object self, selector cmd, tom_int start, tom_int len)
{
tom_object str = TRT_SEND ((reference_imp), self->isa, SEL (r_alloc));
struct _es_i_tom_Array *this;
start = TRT_SEND ((int_imp), self, SEL (_ii__adjustRange__ii_),
start, len, &len);
this = trt_ext_address (self, _ei_i_tom_Array);
return TRT_SEND ((reference_imp), str, SEL (r_init__pi_),
memcpy (xmalloc (len * sizeof (tom_byte)),
(tom_byte *) this->contents + start, len), len);
}
tom_object
i_tom_CharString_r_substring__ii_
(tom_object self, selector cmd, tom_int start, tom_int len)
{
tom_object str = TRT_SEND ((reference_imp), self->isa, SEL (r_alloc));
struct _es_i_tom_Array *this;
start = TRT_SEND ((int_imp), self, SEL (_ii__adjustRange__ii_),
start, len, &len);
this = trt_ext_address (self, _ei_i_tom_Array);
return TRT_SEND ((reference_imp), str, SEL (r_init__pi_),
memcpy (xmalloc (len * sizeof (tom_char)),
(tom_char *) this->contents + start,
len * sizeof (tom_char)), len);
}
tom_object
i_tom_CharString_r_mutableSubstring__ii_
(tom_object self, selector cmd, tom_int start, tom_int len)
{
tom_object str = TRT_SEND ((reference_imp),
_mr_c_tom_MutableCharString, SEL (r_alloc));
struct _es_i_tom_Array *this;
start = TRT_SEND ((int_imp), self, SEL (_ii__adjustRange__ii_),
start, len, &len);
this = trt_ext_address (self, _ei_i_tom_Array);
return TRT_SEND ((reference_imp), str, SEL (r_init__pi_),
memcpy (xmalloc (len * sizeof (tom_char)),
(tom_char *) this->contents + start,
len * sizeof (tom_char)), len);
}
tom_object
i_tom_ObjectArray_r_deepen_i_mutably__o (tom_object self, selector cmd,
tom_int level, tom_byte mutably)
{
struct _es_i_tom_Array *this = trt_ext_address (self, _ei_i_tom_Array);
int i;
if (!level)
return self;
for (i = 0; i < this->length; i++)
{
tom_object o = TRT_SEND ((reference_imp), ((void **) this->contents)[i],
mutably ? SEL (r_mutableCopy) : SEL (r_copy));
((void **) this->contents)[i] = TRT_SEND ((reference_imp), o,
SEL (r_deepen_i_mutably__o),
level - 1, mutably);
}
return self;
}
void
i_tom_MutableObjectArray_v_insert_r_at_i (tom_object self, selector cmd,
tom_object object, tom_int index)
{
struct _es_i_tom_MutableArray *that;
struct _es_i_tom_Array *this;
this = trt_ext_address (self, _ei_i_tom_Array);
that = trt_ext_address (self, _ei_i_tom_MutableArray);
check_index (self, index, this->length + 1);
trt_array_resize (this, that, sizeof (void *), 1);
if (index != this->length)
memmove ((void **) this->contents + index + 1,
(void **) this->contents + index,
sizeof (void *) * (this->length - index));
((void **) this->contents)[index] = trt_assign_object_var (self, object);
this->length++;
}
|