Skip to content

Commit

Permalink
removed unnecessary layer that kept sm_symbol.encode_id in all-ascii …
Browse files Browse the repository at this point in the history
…format. speedup
  • Loading branch information
reginaldford committed Mar 10, 2024
1 parent 2a7d6ab commit 5495702
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 50 deletions.
6 changes: 3 additions & 3 deletions src/main/object/sm_cx.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ bool sm_cx_let(sm_cx *self, sm_symbol *sym, sm_object *val) {
self->content = sm_new_node(NULL, NULL, 0LL, NULL);
current_node = self->content;
for (int i = 0; i < sym->code_id->size; i++) {
int index = sm_node_map_index((&sym->code_id->content)[i]);
int index = (&sym->code_id->content)[i];
int child_index = sm_node_child_index(current_node->map, index);
struct sm_node *next_node;
if (sm_node_map_get(current_node->map, index) == false) {
Expand Down Expand Up @@ -123,7 +123,7 @@ bool sm_cx_rm(sm_cx *self, sm_symbol *sym) {
int char_index = 0;
node_path[0] = curr_node;
for (; char_index < len && curr_node != NULL; char_index++) {
map_index = sm_node_map_index(needle[char_index]);
map_index = needle[char_index];
if (sm_node_map_get(curr_node->map, map_index) == false)
return false;
child_index = sm_node_map_left_count(curr_node->map, map_index);
Expand All @@ -142,7 +142,7 @@ bool sm_cx_rm(sm_cx *self, sm_symbol *sym) {
curr_node = node_path[i];
struct sm_node *curr_node_parent = node_path[i - 1];
if (curr_node->map == 0LL && curr_node->value == NULL) {
map_index = sm_node_map_index(needle[i - 1]);
map_index = needle[i - 1];
child_index = sm_node_map_left_count(curr_node_parent->map, map_index);
sm_node_map_set(&curr_node_parent->map, map_index, false);
curr_node_parent->children = sm_node_rm(curr_node_parent->children, curr_node);
Expand Down
44 changes: 2 additions & 42 deletions src/main/object/sm_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,6 @@ sm_node *sm_new_node(sm_object *value, struct sm_node *next, long long map,
return node;
}

// Return the sm_node child index correlating to this character
// The index is the ASCII value, the value is a number from 0 to 63.
// Input ASCII Group_Size output
// _ is 95 1 0
// 0-9 is 48-57 10 1-10
// A-Z is 65-90 26 11-36
// ' is 39 1 37
// a-z is 98-122 26 38-63
// Total: 64
int sm_node_map_index(char c) {
static const int result[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, 37,
-1, 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, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
return result[c];
}

// Inverse of sm_map_index
// Expects 0-63, else, returns NULL
// Returns a displayable character
char sm_node_bit_unindex(int i) {
static const int output[] = {95, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 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, 39, 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};
if (i < 64)
return output[i];
else
return '\0';
}

// Return the child node, specified by index
sm_node *sm_node_nth(sm_node *self, int index) {
sm_node *current = self;
Expand Down Expand Up @@ -133,7 +93,7 @@ sm_node *sm_node_subnode(sm_node *self, char *needle, int len) {
sm_node *curr_node = self;
int char_index = 0;
while (char_index < len && curr_node != NULL) {
int map_index = sm_node_map_index(needle[char_index]);
int map_index = needle[char_index];
uint64_t map = curr_node->map;
uint64_t bit = 1ULL << map_index;
if ((map & bit) == 0) {
Expand All @@ -156,7 +116,7 @@ sm_node *sm_node_parent_node(sm_node *self, char *needle, int len) {
int child_index = 0;
int char_index = 0;
for (; char_index < len && curr_node != NULL; char_index++) {
map_index = sm_node_map_index(needle[char_index]);
map_index = needle[char_index];
if (sm_node_map_get(curr_node->map, map_index) == false)
return NULL;
child_index = sm_node_map_left_count(curr_node->map, map_index);
Expand Down
6 changes: 2 additions & 4 deletions src/main/object/sm_symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ extern struct sm_heap *sms_symbol_heap;
extern struct sm_heap *sms_symbol_name_heap;
extern uint32_t sms_num_symbols;

// Encode the id of the symbol
// id is the array index of the symbol in the symbol heap
// Base 64 Encode the id of the symbol
sm_string *sm_symbol_encode_id(sm_symbol *sym) {
int32_t sym_id = sym - (sm_symbol *)sms_symbol_heap->storage;
// Turn sym_id into hex
char output_str[7];
uint8_t out_str_len = 0;
while (sym_id) {
char zeroTo63 = (char)(sym_id & 63);
output_str[out_str_len++] = sm_node_bit_unindex(zeroTo63);
output_str[out_str_len++] = (char)(sym_id & 63);
sym_id = sym_id >> 6;
}
output_str[out_str_len] = '\0';
Expand Down
2 changes: 1 addition & 1 deletion src/main/sm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern int yylineno;
void print_intro() {
printf("%s%sSymbolic Math System\n", sm_terminal_bg_color(SM_TERM_BLACK),
sm_terminal_fg_color(SM_TERM_B_BLUE));
printf("%sVersion 0.208%s\n", sm_terminal_fg_color(SM_TERM_B_WHITE), sm_terminal_reset());
printf("%sVersion 0.209%s\n", sm_terminal_fg_color(SM_TERM_B_WHITE), sm_terminal_reset());
}

// Initialize the heap, etc, if necessary
Expand Down

0 comments on commit 5495702

Please sign in to comment.