Hash values aren't printed as "hash" anymore
This commit is contained in:
parent
f7afaf34aa
commit
701b9aad40
14
object.c
14
object.c
@ -88,7 +88,19 @@ void printObject(Value value) {
|
|||||||
printf("]");
|
printf("]");
|
||||||
break;
|
break;
|
||||||
case OBJ_HASH:
|
case OBJ_HASH:
|
||||||
printf("hash");
|
printf("{");
|
||||||
|
Table* hash=AS_HASH(value);
|
||||||
|
Entry** entries=getEntries(hash);
|
||||||
|
for (int i=0;entries[i]!=NULL;i++) {
|
||||||
|
Entry* entry=entries[i];
|
||||||
|
printValue(entry->key);
|
||||||
|
printf("=>");
|
||||||
|
printValue(entry->value);
|
||||||
|
if (entries[i+1]!=NULL) {
|
||||||
|
printf(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("}");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
26
table.c
26
table.c
@ -93,7 +93,7 @@ bool tableSet(Table* table, Value key, Value value) {
|
|||||||
return isNewKey;
|
return isNewKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tableDelete(Table* table,Value key) {
|
bool tableDelete(Table* table, Value key) {
|
||||||
if (table->count == 0) return false;
|
if (table->count == 0) return false;
|
||||||
|
|
||||||
// Find the entry.
|
// Find the entry.
|
||||||
@ -137,3 +137,27 @@ ObjString* tableFindString(Table* table, const char* chars, int length, uint32_t
|
|||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int tableLength(Table* table) {
|
||||||
|
int length = 0;
|
||||||
|
for (int i = 0; i < table->capacity; i++) {
|
||||||
|
Entry entry = table->entries[i];
|
||||||
|
if (!IS_EMPTY(entry.key)) length++;
|
||||||
|
}
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
Entry** getEntries(Table* table) {
|
||||||
|
int length = tableLength(table);
|
||||||
|
Entry** entries = malloc(sizeof(Entry*)*(length+1));
|
||||||
|
entries[length]=NULL;
|
||||||
|
int j=0;
|
||||||
|
for (int i=0; i < table->capacity; i++) {
|
||||||
|
Entry* entry = &table->entries[i];
|
||||||
|
if (!IS_EMPTY(entry->key)) {
|
||||||
|
entries[j]=entry;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
2
table.h
2
table.h
@ -22,5 +22,5 @@ bool tableSet(Table* table, Value key, Value value);
|
|||||||
bool tableDelete(Table* table, Value key);
|
bool tableDelete(Table* table, Value key);
|
||||||
void tableAddAll(Table* from, Table* to);
|
void tableAddAll(Table* from, Table* to);
|
||||||
ObjString* tableFindString(Table* table, const char* chars, int length, uint32_t hash);
|
ObjString* tableFindString(Table* table, const char* chars, int length, uint32_t hash);
|
||||||
|
Entry** getEntries(Table* table);
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user