@@ -410,7 +410,7 @@ cog_object* cog_hash(cog_object* obj) {
410
410
return cog_expect_type_fatal (cog_pop (), & cog_ot_int );
411
411
}
412
412
413
- static cog_object * _mktable (cog_object * key , cog_object * val , cog_object * left , cog_object * right ) {
413
+ static cog_object * _treenode (cog_object * key , cog_object * val , cog_object * left , cog_object * right ) {
414
414
cog_object * a = cog_make_obj (& cog_ot_list );
415
415
cog_object * b = cog_make_obj (& cog_ot_list );
416
416
cog_object * c = cog_make_obj (& cog_ot_list );
@@ -437,16 +437,20 @@ static cog_object* _wraptab(cog_object* tree) {
437
437
static cog_object * _iou_helper (cog_object * tree , cog_object * key , cog_object * val , int64_t hash ) {
438
438
if (!tree ) {
439
439
// we got to the end without finding a existing node, so add a new one
440
- return _mktable (key , val , NULL , NULL );
440
+ cog_printf ("DEBUG: New node with %O: %O\n" , key , val );
441
+ return _treenode (key , val , NULL , NULL );
441
442
}
442
443
bool is_left = hash & 1 ;
443
444
int64_t rest_hash = hash >> 1 ;
444
445
if (cog_equal (tree -> TKEY , key )) {
445
446
// update this node
446
- return _mktable (tree -> TKEY , val , tree -> TRIGHT , tree -> TLEFT );
447
+ cog_printf ("DEBUG: Update node with %O: %O\n" , key , val );
448
+ return _treenode (tree -> TKEY , val , tree -> TRIGHT , tree -> TLEFT );
447
449
}
448
450
// need to recurse
449
- cog_object * newnode = _mktable (tree -> TKEY , tree -> TVAL , tree -> TRIGHT , tree -> TLEFT );
451
+ printf ("->%s" , is_left ? "left" : "right" );
452
+ fflush (stdout );
453
+ cog_object * newnode = _treenode (tree -> TKEY , tree -> TVAL , tree -> TRIGHT , tree -> TLEFT );
450
454
if (is_left ) newnode -> TLEFT = _iou_helper (newnode -> TLEFT , key , val , rest_hash );
451
455
else newnode -> TRIGHT = _iou_helper (newnode -> TRIGHT , key , val , rest_hash );
452
456
return newnode ;
@@ -456,27 +460,29 @@ static cog_object* _rem_helper(cog_object* tree, cog_object* key, int64_t hash)
456
460
if (!tree ) return NULL ;
457
461
bool is_left = hash & 1 ;
458
462
int64_t rest_hash = hash >> 1 ;
459
- cog_object * newtab ;
463
+ cog_object * newtree ;
460
464
461
465
if (cog_equal (key , tree -> TKEY )) {
462
466
// this node is what needs to be deleted
463
- if (!tree -> TLEFT && !tree -> TRIGHT ) newtab = NULL ; // leaf node gets deleted
467
+ if (!tree -> TLEFT && !tree -> TRIGHT ) newtree = NULL ; // leaf node gets deleted
464
468
// else just pick which side to delete. I chose right first else left
465
- else if (tree -> TRIGHT ) newtab = _mktable (tree -> TRIGHT -> TKEY , tree -> TRIGHT -> TVAL , tree -> TLEFT , tree -> TRIGHT -> TRIGHT );
466
- else newtab = _mktable (tree -> TLEFT -> TKEY , tree -> TLEFT -> TVAL , tree -> TLEFT -> TLEFT , tree -> TRIGHT );
469
+ else if (tree -> TRIGHT ) newtree = _treenode (tree -> TRIGHT -> TKEY , tree -> TRIGHT -> TVAL , tree -> TLEFT , tree -> TRIGHT -> TRIGHT );
470
+ else newtree = _treenode (tree -> TLEFT -> TKEY , tree -> TLEFT -> TVAL , tree -> TLEFT -> TLEFT , tree -> TRIGHT );
467
471
} else {
468
472
// it's another node that needs to be deleted
469
- if (is_left ) newtab = _mktable (tree -> TKEY , tree -> TVAL , _rem_helper (tree -> TLEFT , key , rest_hash ), tree -> TRIGHT );
470
- else newtab = _mktable (tree -> TKEY , tree -> TVAL , tree -> TLEFT , _rem_helper (tree -> TRIGHT , key , rest_hash ));
473
+ if (is_left ) newtree = _treenode (tree -> TKEY , tree -> TVAL , _rem_helper (tree -> TLEFT , key , rest_hash ), tree -> TRIGHT );
474
+ else newtree = _treenode (tree -> TKEY , tree -> TVAL , tree -> TLEFT , _rem_helper (tree -> TRIGHT , key , rest_hash ));
471
475
}
472
- return newtab ;
476
+ return newtree ;
473
477
}
474
478
475
479
cog_object * cog_table_get (cog_object * tab , cog_object * key , bool * found ) {
476
480
assert (tab && tab -> type == & cog_ot_table );
477
481
cog_object * tree = tab -> next ; // get the internal tree
478
482
int64_t hash = cog_hash (key )-> as_int ;
483
+ cog_printf ("DEBUG: getting with hash %llX, tree is: %O\n" , hash , tree );
479
484
while (tree ) {
485
+ cog_printf ("DEBUG: looking at key %O\n" , tree -> TKEY );
480
486
if (cog_equal (tree -> TKEY , key )) {
481
487
* found = true;
482
488
return tree -> TVAL ;
@@ -485,13 +491,16 @@ cog_object* cog_table_get(cog_object* tab, cog_object* key, bool* found) {
485
491
hash = hash >> 1 ;
486
492
if (is_left ) tree = tree -> TLEFT ;
487
493
else tree = tree -> TRIGHT ;
494
+ printf ("%s->" , is_left ? "left" : "right" );
495
+ fflush (stdout );
488
496
}
489
497
* found = false;
490
498
return NULL ;
491
499
}
492
500
493
501
cog_object * cog_table_insert_or_update (cog_object * tab , cog_object * key , cog_object * val ) {
494
502
assert (tab && tab -> type == & cog_ot_table );
503
+ cog_printf ("DEBUG: adding key %O to table\n" , key );
495
504
return _wraptab (_iou_helper (tab -> next , key , val , cog_hash (key )-> as_int ));
496
505
}
497
506
@@ -797,7 +806,7 @@ cog_object* float_printself() {
797
806
cog_object * num = cog_pop ();
798
807
cog_pop (); // ignore cookie
799
808
char buffer [32 ];
800
- snprintf (buffer , sizeof (buffer ), "%lg" , num -> as_float );
809
+ snprintf (buffer , sizeof (buffer ), "%lg%s " , num -> as_float , floor ( num -> as_float ) == num -> as_float ? ".0" : "" );
801
810
cog_push (cog_string (buffer ));
802
811
return NULL ;
803
812
}
@@ -808,7 +817,7 @@ static cog_object* m_float_hash() {
808
817
cog_object * num = cog_pop ();
809
818
double val = num -> as_float ;
810
819
// floats hash to their int value if it's an int, otherwise the reinterpret_cast of their bits
811
- int64_t hash = val == (( double )(( int64_t ) val ) ) ? (int64_t )val : * (int64_t * )& val ;
820
+ int64_t hash = val == floor ( val ) ? (int64_t )val : * (int64_t * )& val ;
812
821
cog_push (cog_box_int (hash ));
813
822
return NULL ;
814
823
}
0 commit comments