sparse_matrix_t *sparse_matrix_new_shape(size_t m, size_t n) { sparse_matrix_t *matrix = calloc(1, sizeof(sparse_matrix_t)); if (matrix == NULL) return NULL; matrix->m = m; matrix->n = n; matrix->indptr = uint32_array_new_size(m + 1); if (matrix->indptr == NULL) { goto exit_sparse_matrix_created; } uint32_array_push(matrix->indptr, 0); matrix->indices = uint32_array_new(); if (matrix->indices == NULL) { goto exit_sparse_matrix_created; } matrix->data = double_array_new(); if (matrix->data == NULL) { goto exit_sparse_matrix_created; } return matrix; exit_sparse_matrix_created: sparse_matrix_destroy(matrix); return NULL; }
graph_t *graph_new_dims(graph_type_t type, uint32_t m, uint32_t n, size_t nnz, bool fixed_rows) { graph_t *graph = malloc(sizeof(graph_t)); graph->m = m; graph->fixed_rows = fixed_rows; graph->n = n; graph->type = type; graph->indptr = uint32_array_new_size(m + 1); if (graph->indptr == NULL) { graph_destroy(graph); return NULL; } if (!fixed_rows) { uint32_array_push(graph->indptr, 0); } if (nnz > 0) { graph->indices = uint32_array_new_size(nnz); } else { graph->indices = uint32_array_new(); } if (graph->indices == NULL) { graph_destroy(graph); return NULL; } return graph; }
string_tree_t *regex_string_tree(char *regex, size_t len) { uint8_t *char_ptr = (uint8_t *)regex; bool in_set = false; bool in_brackets = false; int32_t codepoint; int32_t last_codepoint = 0; ssize_t char_len; size_t bracket_start; size_t bracket_len; char temp_char[MAX_UTF8_CHAR_SIZE]; ssize_t temp_char_len; string_tree_t *tree = string_tree_new(); if (len == 0) { // Single token with zero-length string_tree_add_string_len(tree, regex, len); string_tree_finalize_token(tree); return tree; } uint32_array *char_set = uint32_array_new(); size_t idx = 0; int i, j; bool add_to_index = false; while (idx < len) { char_len = utf8proc_iterate(char_ptr, len, &codepoint); if (char_len <= 0) { uint32_array_destroy(char_set); string_tree_destroy(tree); return NULL; } if (!(utf8proc_codepoint_valid(codepoint))) { idx += char_len; char_ptr += char_len; continue; } add_to_index = true; if (codepoint == LSQUARE_CODEPOINT && last_codepoint != BACKSLASH_CODEPOINT) { log_debug("begin set\n"); in_set = true; codepoint = BEGIN_SET_CODEPOINT; uint32_array_clear(char_set); } else if (codepoint == RSQUARE_CODEPOINT && last_codepoint != BACKSLASH_CODEPOINT && in_set) { log_debug("end set"); for (j = 0; j < char_set->n; j++) { temp_char_len = utf8proc_encode_char(char_set->a[j], (uint8_t *)temp_char); log_debug("Adding string %.*s\n", (int)temp_char_len, temp_char); string_tree_add_string_len(tree, temp_char, temp_char_len); } string_tree_finalize_token(tree); uint32_array_clear(char_set); // Add a special codepoint to the sequence to distinguish from an escaped square bracket codepoint = END_SET_CODEPOINT; in_set = false; } else if (codepoint == LCURLY_CODEPOINT && last_codepoint != BACKSLASH_CODEPOINT) { in_brackets = true; bracket_start = idx + char_len; bracket_len = 0; add_to_index = false; } else if (codepoint == RCURLY_CODEPOINT && last_codepoint != BACKSLASH_CODEPOINT && in_brackets) { log_debug("Adding bracketed string: %.*s\n", (int) bracket_len, regex + bracket_start); string_tree_add_string_len(tree, regex + bracket_start, bracket_len); in_brackets = false; } else if ((codepoint == LPAREN_CODEPOINT || codepoint == RPAREN_CODEPOINT) && last_codepoint != BACKSLASH_CODEPOINT) { log_debug("group\n"); add_to_index = false; } else if (in_set) { log_debug("in set\n"); // Queue node, we'll add them to the trie uint32_array_push(char_set, codepoint); add_to_index = false; } else if (in_brackets) { add_to_index = false; bracket_len += char_len; } else if (codepoint == BACKSLASH_CODEPOINT && last_codepoint != BACKSLASH_CODEPOINT) { add_to_index = false; } log_debug("codepoint = %d\n", codepoint); if (add_to_index) { temp_char_len = utf8proc_encode_char(codepoint, (uint8_t *)temp_char); log_debug("char = %.*s\n", (int)temp_char_len, temp_char); string_tree_add_string_len(tree, temp_char, temp_char_len); string_tree_finalize_token(tree); } idx += char_len; char_ptr += char_len; } uint32_array_destroy(char_set); return tree; }