Hash :
fd3b6ed3
Author :
Thomas de Grivel
Date :
2024-04-10T15:20:17
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 638 639 640 641 642
/* c3
* Copyright 2022-2024 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software granted the above
* copyright notice and this permission paragraph are included in all
* copies and substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#include "c3.h"
sw data_buf_inspect (const s_sym *type, s_buf *buf, const void *data)
{
const s_struct_type *st;
if (type == &g_sym_Array ||
sym_is_array_type(type))
return buf_inspect_array(buf, data);
if (type == &g_sym_Bool)
return buf_inspect_bool(buf, data);
if (type == &g_sym_Call)
return buf_inspect_call(buf, data);
if (type == &g_sym_Cfn)
return buf_inspect_cfn(buf, data);
if (type == &g_sym_Character)
return buf_inspect_character(buf, data);
if (type == &g_sym_F32)
return buf_inspect_f32(buf, data);
if (type == &g_sym_F64)
return buf_inspect_f64(buf, data);
if (type == &g_sym_Fact)
return buf_inspect_fact(buf, data);
if (type == &g_sym_Fn)
return buf_inspect_fn(buf, data);
if (type == &g_sym_Ident)
return buf_inspect_ident(buf, data);
if (type == &g_sym_Integer)
return buf_inspect_integer(buf, data);
if (type == &g_sym_List)
return buf_inspect_list(buf, data);
if (type == &g_sym_Ptag)
return buf_inspect_ptag(buf, data);
if (type == &g_sym_Ptr)
return buf_inspect_ptr(buf, data);
if (type == &g_sym_PtrFree)
return buf_inspect_ptr_free(buf, data);
if (type == &g_sym_Quote)
return buf_inspect_quote(buf, data);
if (type == &g_sym_S8)
return buf_inspect_s8(buf, data);
if (type == &g_sym_S16)
return buf_inspect_s16(buf, data);
if (type == &g_sym_S32)
return buf_inspect_s32(buf, data);
if (type == &g_sym_S64)
return buf_inspect_s64(buf, data);
if (type == &g_sym_Str)
return buf_inspect_str(buf, data);
if (type == &g_sym_Struct)
return buf_inspect_struct(buf, data);
if (type == &g_sym_Sw)
return buf_inspect_sw(buf, data);
if (type == &g_sym_Sym)
return buf_inspect_sym(buf, data);
if (type == &g_sym_Tuple)
return buf_inspect_tuple(buf, data);
if (type == &g_sym_U8)
return buf_inspect_u8(buf, data);
if (type == &g_sym_U16)
return buf_inspect_u16(buf, data);
if (type == &g_sym_U32)
return buf_inspect_u32(buf, data);
if (type == &g_sym_U64)
return buf_inspect_u64(buf, data);
if (type == &g_sym_Uw)
return buf_inspect_uw(buf, data);
if (type == &g_sym_Var)
return buf_inspect_var(buf, data);
if (type == &g_sym_Void)
return buf_inspect_void(buf, data);
st = struct_type_find(type);
if (st) {
s_struct s = {0};
s.type = st;
s.data = (void *) data;
return buf_inspect_struct(buf, &s);
}
err_write_1("data_buf_inspect: unknown type: ");
err_inspect_sym(&type);
err_write_1("\n");
assert(! "data_buf_inspect: unknown type");
return -1;
}
sw data_buf_inspect_size (const s_sym *type, const void *data)
{
const s_struct_type *st;
if (type == &g_sym_Array ||
sym_is_array_type(type))
return buf_inspect_array_size(data);
if (type == &g_sym_Bool)
return buf_inspect_bool_size(data);
if (type == &g_sym_Call)
return buf_inspect_call_size(data);
if (type == &g_sym_Cfn)
return buf_inspect_cfn_size(data);
if (type == &g_sym_Character)
return buf_inspect_character_size(data);
if (type == &g_sym_F32)
return buf_inspect_f32_size(data);
if (type == &g_sym_F64)
return buf_inspect_f64_size(data);
if (type == &g_sym_Fact)
return buf_inspect_fact_size(data);
if (type == &g_sym_Fn)
return buf_inspect_fn_size(data);
if (type == &g_sym_Ident)
return buf_inspect_ident_size(data);
if (type == &g_sym_Integer)
return buf_inspect_integer_size(data);
if (type == &g_sym_List)
return buf_inspect_list_size(data);
if (type == &g_sym_Ptag)
return buf_inspect_ptag_size(data);
if (type == &g_sym_Ptr)
return buf_inspect_ptr_size(data);
if (type == &g_sym_PtrFree)
return buf_inspect_ptr_free_size(data);
if (type == &g_sym_Quote)
return buf_inspect_quote_size(data);
if (type == &g_sym_S8)
return buf_inspect_s8_size(data);
if (type == &g_sym_S16)
return buf_inspect_s16_size(data);
if (type == &g_sym_S32)
return buf_inspect_s32_size(data);
if (type == &g_sym_S64)
return buf_inspect_s64_size(data);
if (type == &g_sym_Str)
return buf_inspect_str_size(data);
if (type == &g_sym_Struct)
return buf_inspect_struct_size(data);
if (type == &g_sym_Sw)
return buf_inspect_sw_size(data);
if (type == &g_sym_Sym)
return buf_inspect_sym_size(data);
if (type == &g_sym_Tuple)
return buf_inspect_tuple_size(data);
if (type == &g_sym_U8)
return buf_inspect_u8_size(data);
if (type == &g_sym_U16)
return buf_inspect_u16_size(data);
if (type == &g_sym_U32)
return buf_inspect_u32_size(data);
if (type == &g_sym_U64)
return buf_inspect_u64_size(data);
if (type == &g_sym_Uw)
return buf_inspect_uw_size(data);
if (type == &g_sym_Var)
return buf_inspect_var_size(data);
if (type == &g_sym_Void)
return buf_inspect_void_size(data);
st = struct_type_find(type);
if (st) {
s_struct s = {0};
s.type = st;
s.data = (void *) data;
return buf_inspect_struct_size(&s);
}
err_write_1("data_buf_inspect_size: unknown type: ");
err_inspect_sym(&type);
err_write_1("\n");
assert(! "data_buf_inspect_size: unknown type");
return -1;
}
bool data_clean (const s_sym *type, void *data)
{
const s_struct_type *st;
assert(type);
if (type == &g_sym_Array ||
sym_is_array_type(type)) {
array_clean(data);
return true;
}
if (type == &g_sym_Bool) {
return true;
}
if (type == &g_sym_Call) {
call_clean(data);
return true;
}
if (type == &g_sym_Cfn) {
cfn_clean(data);
return true;
}
if (type == &g_sym_Character) {
return true;
}
if (type == &g_sym_F32) {
return true;
}
if (type == &g_sym_F64) {
return true;
}
if (type == &g_sym_Fact) {
return true;
}
if (type == &g_sym_Fn) {
fn_clean(data);
return true;
}
if (type == &g_sym_Ident) {
return true;
}
if (type == &g_sym_Integer) {
integer_clean(data);
return true;
}
if (type == &g_sym_List) {
list_f_clean(data);
return true;
}
if (type == &g_sym_Map) {
map_clean(data);
return true;
}
if (type == &g_sym_Ptag) {
return true;
}
if (type == &g_sym_Ptr) {
return true;
}
if (type == &g_sym_PtrFree) {
ptr_free_clean(data);
return true;
}
if (type == &g_sym_Quote) {
quote_clean(data);
return true;
}
if (type == &g_sym_S8) {
return true;
}
if (type == &g_sym_S16) {
return true;
}
if (type == &g_sym_S32) {
return true;
}
if (type == &g_sym_S64) {
return true;
}
if (type == &g_sym_Str) {
str_clean(data);
return true;
}
if (type == &g_sym_Struct) {
struct_clean(data);
return true;
}
if (type == &g_sym_Sw) {
return true;
}
if (type == &g_sym_Sym) {
return true;
}
if (type == &g_sym_Tuple) {
tuple_clean(data);
return true;
}
if (type == &g_sym_U8) {
return true;
}
if (type == &g_sym_U16) {
return true;
}
if (type == &g_sym_U32) {
return true;
}
if (type == &g_sym_U64) {
return true;
}
if (type == &g_sym_Uw) {
return true;
}
if (type == &g_sym_Var) {
return true;
}
if (type == &g_sym_Void) {
return true;
}
st = struct_type_find(type);
if (st) {
s_struct s = {0};
s.type = st;
s.data = data;
struct_clean(&s);
return true;
}
err_write_1("data_clean: unknown type: ");
err_inspect_sym(&type);
err_write_1("\n");
assert(! "data_clean: unknown type");
return false;
}
bool data_compare (const s_sym *type, const void *a, const void *b)
{
const s_struct_type *st;
if (type == &g_sym_Array ||
sym_is_array_type(type))
return compare_array(a, b);
if (type == &g_sym_Bool)
return compare_bool(*(bool *) a, *(bool *) b);
if (type == &g_sym_Call)
return compare_call(a, b);
if (type == &g_sym_Cfn)
return compare_cfn(a, b);
if (type == &g_sym_Character)
return compare_character(*(character *) a, *(character *) b);
if (type == &g_sym_F32)
return compare_f32(*(f64 *) a, *(f64 *) b);
if (type == &g_sym_F64)
return compare_f64(*(f64 *) a, *(f64 *) b);
if (type == &g_sym_Fact)
return compare_fact(a, b);
if (type == &g_sym_Fn)
return compare_fn(a, b);
if (type == &g_sym_Ident)
return compare_ident(a, b);
if (type == &g_sym_Integer)
return compare_integer(a, b);
if (type == &g_sym_List)
return compare_list(a, b);
if (type == &g_sym_Ptag)
return compare_ptag(a, b);
if (type == &g_sym_Ptr)
return compare_ptr(a, b);
if (type == &g_sym_PtrFree)
return compare_ptr(a, b);
if (type == &g_sym_Quote)
return compare_quote(a, b);
if (type == &g_sym_S8)
return compare_s8(*(s8 *) a, *(s8 *) b);
if (type == &g_sym_S16)
return compare_s16(*(s16 *) a, *(s16 *) b);
if (type == &g_sym_S32)
return compare_s32(*(s32 *) a, *(s32 *) b);
if (type == &g_sym_S64)
return compare_s64(*(s64 *) a, *(s64 *) b);
if (type == &g_sym_Str)
return compare_str(a, b);
if (type == &g_sym_Struct)
return compare_struct(a, b);
if (type == &g_sym_Sw)
return compare_sw(*(sw *) a, *(sw *) b);
if (type == &g_sym_Sym)
return compare_sym(a, b);
if (type == &g_sym_Tuple)
return compare_tuple(a, b);
if (type == &g_sym_U8)
return compare_u8(*(u8 *) a, *(u8 *) b);
if (type == &g_sym_U16)
return compare_u16(*(u16 *) a, *(u16 *) b);
if (type == &g_sym_U32)
return compare_u32(*(u32 *) a, *(u32 *) b);
if (type == &g_sym_U64)
return compare_u64(*(u64 *) a, *(u64 *) b);
if (type == &g_sym_Uw)
return compare_uw(*(uw *) a, *(uw *) b);
if (type == &g_sym_Var)
return compare_ptr(a, b);
if (type == &g_sym_Void)
return 0;
st = struct_type_find(type);
if (st) {
s_struct sa = {0};
s_struct sb = {0};
sa.type = st;
sa.data = (void *) a;
sb.type = st;
sb.data = (void *) a;
return compare_struct(&sa, &sb);
}
err_write_1("data_compare: unknown type: ");
err_inspect_sym(&type);
err_write_1("\n");
assert(! "data_compare: unknown type");
return false;
}
bool data_hash_update (const s_sym *type, t_hash *hash, const void *data)
{
const s_struct_type *st;
if (type == &g_sym_Array ||
sym_is_array_type(type))
return hash_update_array(hash, data);
if (type == &g_sym_Bool)
return hash_update_bool(hash, data);
if (type == &g_sym_Call)
return hash_update_call(hash, data);
if (type == &g_sym_Cfn)
return hash_update_cfn(hash, data);
if (type == &g_sym_Character)
return hash_update_character(hash, data);
if (type == &g_sym_F32)
return hash_update_f32(hash, data);
if (type == &g_sym_F64)
return hash_update_f64(hash, data);
if (type == &g_sym_Fact)
return hash_update_fact(hash, data);
if (type == &g_sym_Fn)
return hash_update_fn(hash, data);
if (type == &g_sym_Ident)
return hash_update_ident(hash, data);
if (type == &g_sym_Integer)
return hash_update_integer(hash, data);
if (type == &g_sym_List)
return hash_update_list(hash, data);
if (type == &g_sym_Ptag)
return hash_update_ptag(hash, data);
if (type == &g_sym_Ptr)
return hash_update_ptr(hash, data);
if (type == &g_sym_PtrFree)
return hash_update_ptr_free(hash, data);
if (type == &g_sym_Quote)
return hash_update_quote(hash, data);
if (type == &g_sym_S8)
return hash_update_s8(hash, data);
if (type == &g_sym_S16)
return hash_update_s16(hash, data);
if (type == &g_sym_S32)
return hash_update_s32(hash, data);
if (type == &g_sym_S64)
return hash_update_s64(hash, data);
if (type == &g_sym_Str)
return hash_update_str(hash, data);
if (type == &g_sym_Struct)
return hash_update_struct(hash, data);
if (type == &g_sym_Sw)
return hash_update_sw(hash, data);
if (type == &g_sym_Sym)
return hash_update_sym(hash, data);
if (type == &g_sym_Tuple)
return hash_update_tuple(hash, data);
if (type == &g_sym_U8)
return hash_update_u8(hash, data);
if (type == &g_sym_U16)
return hash_update_u16(hash, data);
if (type == &g_sym_U32)
return hash_update_u32(hash, data);
if (type == &g_sym_U64)
return hash_update_u64(hash, data);
if (type == &g_sym_Uw)
return hash_update_uw(hash, data);
if (type == &g_sym_Var)
return hash_update_var(hash, data);
if (type == &g_sym_Void)
return hash_update_void(hash, data);
st = struct_type_find(type);
if (st) {
s_struct s = {0};
s.type = st;
s.data = (void *) data;
return hash_update_struct(hash, &s);
}
err_write_1("data_hash_update: unknown type: ");
err_inspect_sym(&type);
err_write_1("\n");
assert(! "data_hash_update: unknown type");
return false;
}
void * data_init_cast (void *data, const s_sym *type, const s_tag *tag)
{
const s_struct_type *st;
if (type == &g_sym_Array ||
sym_is_array_type(type))
return array_init_cast(data, type, tag);
if (type == &g_sym_Bool)
return bool_init_cast(data, type, tag);
if (type == &g_sym_Call)
return call_init_cast(data, type, tag);
if (type == &g_sym_Cfn)
return cfn_init_cast(data, type, tag);
if (type == &g_sym_Character)
return character_init_cast(data, type, tag);
if (type == &g_sym_F32)
return f32_init_cast(data, type, tag);
if (type == &g_sym_F64)
return f64_init_cast(data, type, tag);
if (type == &g_sym_Fact)
return fact_init_cast(data, type, tag);
if (type == &g_sym_Fn)
return fn_init_cast(data, type, tag);
if (type == &g_sym_Ident)
return ident_init_cast(data, type, tag);
if (type == &g_sym_Integer)
return integer_init_cast(data, type, tag);
if (type == &g_sym_List)
return list_init_cast(data, type, tag);
if (type == &g_sym_Ptag)
return ptag_init_cast(data, type, tag);
if (type == &g_sym_Ptr)
return ptr_init_cast(data, type, tag);
if (type == &g_sym_PtrFree)
return ptr_free_init_cast(data, type, tag);
if (type == &g_sym_Quote)
return quote_init_cast(data, type, tag);
if (type == &g_sym_S8)
return s8_init_cast(data, type, tag);
if (type == &g_sym_S16)
return s16_init_cast(data, type, tag);
if (type == &g_sym_S32)
return s32_init_cast(data, type, tag);
if (type == &g_sym_S64)
return s64_init_cast(data, type, tag);
if (type == &g_sym_Str)
return str_init_cast(data, type, tag);
if (type == &g_sym_Sw)
return sw_init_cast(data, type, tag);
if (type == &g_sym_Sym)
return sym_init_cast(data, type, tag);
if (type == &g_sym_Tuple)
return tuple_init_cast(data, type, tag);
if (type == &g_sym_U8)
return u8_init_cast(data, type, tag);
if (type == &g_sym_U16)
return u16_init_cast(data, type, tag);
if (type == &g_sym_U32)
return u32_init_cast(data, type, tag);
if (type == &g_sym_U64)
return u64_init_cast(data, type, tag);
if (type == &g_sym_Uw)
return uw_init_cast(data, type, tag);
if (type == &g_sym_Var)
return var_init_cast(data, type, tag);
if (type == &g_sym_Void)
return data;
/*
if (sym_is_array_type(type)) {
*/
st = struct_type_find(type);
if (st) {
s_struct s = {0};
s.type = st;
s.data = data;
return struct_init_cast(&s, type, tag);
}
err_write_1("data_init_cast: unknown type: ");
err_inspect_sym(&type);
err_write_1("\n");
assert(! "data_init_cast: unknown type");
return NULL;
}
void * data_init_copy (const s_sym *type, void *data, const void *src)
{
const s_struct_type *st;
if (type == &g_sym_Array ||
sym_is_array_type(type))
return array_init_copy(data, src);
if (type == &g_sym_Bool)
return bool_init_copy(data, src);
if (type == &g_sym_Call)
return call_init_copy(data, src);
if (type == &g_sym_Cfn)
return cfn_init_copy(data, src);
if (type == &g_sym_Character)
return character_init_copy(data, src);
if (type == &g_sym_F32)
return f32_init_copy(data, src);
if (type == &g_sym_F64)
return f64_init_copy(data, src);
if (type == &g_sym_Fact)
return fact_init_copy(data, src);
if (type == &g_sym_Fn)
return fn_init_copy(data, src);
if (type == &g_sym_Ident)
return ident_init_copy(data, src);
if (type == &g_sym_Integer)
return integer_init_copy(data, src);
if (type == &g_sym_List)
return list_init_copy(data, src);
if (type == &g_sym_Ptag)
return ptag_init_copy(data, src);
if (type == &g_sym_Ptr)
return ptr_init_copy(data, src);
if (type == &g_sym_PtrFree)
return ptr_free_init_copy(data, src);
if (type == &g_sym_Quote)
return quote_init_copy(data, src);
if (type == &g_sym_S8)
return s8_init_copy(data, src);
if (type == &g_sym_S16)
return s16_init_copy(data, src);
if (type == &g_sym_S32)
return s32_init_copy(data, src);
if (type == &g_sym_S64)
return s64_init_copy(data, src);
if (type == &g_sym_Str)
return str_init_copy(data, src);
if (type == &g_sym_Struct)
return struct_init_copy(data, src);
if (type == &g_sym_Sw)
return sw_init_copy(data, src);
if (type == &g_sym_Sym)
return sym_init_copy(data, src);
if (type == &g_sym_Tuple)
return tuple_init_copy(data, src);
if (type == &g_sym_U8)
return u8_init_copy(data, src);
if (type == &g_sym_U16)
return u16_init_copy(data, src);
if (type == &g_sym_U32)
return u32_init_copy(data, src);
if (type == &g_sym_U64)
return u64_init_copy(data, src);
if (type == &g_sym_Uw)
return uw_init_copy(data, src);
if (type == &g_sym_Var)
return data;
if (type == &g_sym_Void)
return data;
st = struct_type_find(type);
if (st) {
s_struct s = {0};
s_struct t = {0};
t.type = s.type = st;
s.data = data;
t.data = (void *) src;
return struct_init_copy(&s, &t);
}
err_write_1("data_init_copy: unknown type: ");
err_inspect_sym(&type);
err_write_1("\n");
assert(! "data_init_copy: unknown type");
return NULL;
}