void list_remove(struct List *list, int index){ int i; for( i=index; i<list->length-1; i++ ){ list->tokens[i] = list->tokens[i+1]; } list_resize(list,list->length-1); }
void set_symbol_from_string(caValue* val, caValue* str) { // Find this name as an existing builtin symbol. int foundBuiltin = builtin_symbol_from_string(as_cstring(str)); if (foundBuiltin != -1) { set_symbol(val, foundBuiltin); return; } // Find this name as an existing runtime symbol. caValue* foundRuntime = hashtable_get(g_runtimeSymbolMap, str); if (foundRuntime != NULL) { copy(foundRuntime, val); return; } // Create a new runtime symbol. caValue* newRuntime = hashtable_insert(g_runtimeSymbolMap, str); int index = g_nextRuntimeSymbol++; set_symbol(newRuntime, index); set_symbol(val, index); list_resize(g_runtimeSymbolTable, index+1); set_value(list_get(g_runtimeSymbolTable, index), str); }
void list_resize(caValue* list, int size) { ca_assert(list->value_type->storageType == name_StorageTypeList); ListData* data = (ListData*) list->value_data.ptr; data = list_resize(data, size); list->value_data.ptr = data; }
void list_push_back(struct list *li, void *elem) { if (li->num_of_elements == li->size) { list_resize(li, li->size * 2); } li->elements[li->num_of_elements++] = elem; }
// Inserts Token* BEFORE given index. void list_insert(struct List *list, struct Token *t, int index){ list_resize(list,list->length+1); int i; for( i=list->length-1; i>=index; i-- ){ list->tokens[i] = list->tokens[i-1]; } list->tokens[index] = t; }
/* Converts a null-termiated C style string into a list of characters (a Crema string) @param s Pointer to a C character array @return Returns the list_t equivalent of the given C string */ string_t * str_from_cstring(char * s) { string_t * str = list_create(sizeof(char)); list_resize(str, strlen(s) + 1); strncpy(str->arr, s, strlen(s)); str->len = strlen(s); str_insert(str, strlen(s), '\0'); return str; }
static unsigned int add_str(ops_list_t *list,char *str) { if (list->size==list->used) if (!list_resize(list)) return 0; list->strings[list->used]=str; list->used++; return 1; }
/* リストの末尾に要素を追加する */ value* list_push(value* list, value* item) { if (list == NULL) { fprintf(stderr, "List is null.\n"); return NULL; } list = list_resize(list, list->size + 1); int last_index = list_last(list); list->a[last_index] = item; return list; }
void byte_array_resize(struct byte_array* ba, uint32_t size) { if (!(size = list_resize(ba->size, size))) // didn't resize return; assert_message(ba->current >= ba->data, "byte_array corrupt"); uint32_t delta = (uint32_t)(ba->current - ba->data); ba->data = (uint8_t*)realloc(ba->data, size); assert_message(ba->data, "could not reallocate data"); ba->current = ba->data + delta; ba->size = size; //DEBUGPRINT("byte_array_resize %p->%p\n", ba, ba->data); }
/* リストの末尾から要素を取り出す */ value* list_pop(value* list) { if (!list->size) { fprintf(stderr, "List has no elements.\n"); return NULL; } value* res = list_ref(list, list_last(list)); if (res) { res = value_copy(res); } list_resize(list, list_last(list)); return res; }
/* Concatenates two lists together (string or array) @param list1 The first list @param list2 The second list to be concatenated onto the first list */ void list_concat(list_t * list1, list_t * list2) { int i; if (list1->elem_sz != list2->elem_sz) { return; } list_resize(list1, list1->cap + list2->len); for (i = 0; i < list2->len; i++) { list_append(list1, list_retrieve(list2, i)); } }
/* リストの先頭に要素を挿入する */ value* list_unshift(value* list, value* item) { if (list == NULL) { fprintf(stderr, "List is null.\n"); return NULL; } list = list_resize(list, list->size + 1); for (int i=list->size - 1; i > 0; i--) { list->a[i] = list->a[i - 1]; } list->a[0] = item; return list; }
void List__extend(caStack* stack) { caValue* out = circa_output(stack, 1); copy(circa_input(stack, 0), out); caValue* additions = circa_input(stack, 1); int oldLength = list_length(out); int additionsLength = list_length(additions); list_resize(out, oldLength + additionsLength); for (int i = 0; i < additionsLength; i++) copy(list_get(additions, i), list_get(out, oldLength + i)); }
/* Appends a new element onto the given list, increasing the size of the list by one @param list Pointer to a list @param elem Pointer to and element to be appended onto the list */ void list_append(list_t * list, void * elem) { if (list == NULL) { return; } // use len+1 to save space for a terminating entry (e.g. '\0') if (list->len+1 >= list->cap) { list_resize(list, list->cap + DEFAULT_RESIZE_AMT); } list->len++; list_insert(list, list->len - 1, elem); }
void tv_initialize(Type* type, caValue* value) { ca_assert(value->value_data.ptr == NULL); // If the parameter has a fixed size list, then initialize to that. if (list_type_has_specific_size(&type->parameter)) { caValue* typeList = list_get_type_list_from_type(type); int count = list_length(typeList); list_resize(value, count); for (int i=0; i < count; i++) make(as_type(list_get(typeList, i)), list_get(value, i)); } }
void array_resize(struct array *a, uint32_t size) { if (!(size = list_resize(a->size, size))) // didn't resize return; uint32_t delta = (uint32_t)(a->current - a->data); a->data = (void**)realloc(a->data, size * sizeof(void*)); null_check(a->data); //DEBUGPRINT("array_resize %p->%p -- %d to %d\n", a, a->data, a->size, size); if (size > a->size) memset(&a->data[a->size], 0, (size - a->size) * sizeof(void*)); a->size = size; a->current = a->data + delta; return; }
/* リストの先頭から要素を取り出す */ value* list_shift(value* list) { if (!list->size) { fprintf(stderr, "List has no elements.\n"); return NULL; } value* res = list_ref(list, 0); if (res) { res = value_copy(res); } for (int i=0; i<list->size - 1; i++) { list->a[i] = list->a[i + 1]; } list_resize(list, list_last(list)); return res; }
/* Generates a linear sequence of int values in the range of start to end, and returns them as an array @param start An integer number to begin a sequence at @param end An integer number to end the sequence at */ list_t * crema_seq(int64_t start, int64_t end) { list_t * l; int64_t i; if (end <= start) { return NULL; } l = int_list_create(); list_resize(l, end - start); for (i = start; i <= end; i++) { int_list_append(l, i); } return l; }
void list_remove_nulls(ListData** dataPtr) { if (*dataPtr == NULL) return; *dataPtr = list_touch(*dataPtr); ListData* data = *dataPtr; int numRemoved = 0; for (int i=0; i < data->count; i++) { if (is_null(&data->items[i])) numRemoved++; else swap(&data->items[i - numRemoved], &data->items[i]); } *dataPtr = list_resize(*dataPtr, data->count - numRemoved); }
/* Returns a sub-string of a given string, given by a start index within the string, and a length of the substring. @param str The string to be operated on @param start The starting index of the substring @param len The number of characters after 'start' to be included in the substring @return The substring */ string_t * str_substr(string_t * str, unsigned int start, unsigned int len) { string_t * nstr = list_create(sizeof(char)); if (start >= str->len) return NULL; if (len > str->len || len == 0) len = str->len - start; if (start == 0 && (len == 0 || len == str->len)) return str; if (start >= str->len) return NULL; list_resize(nstr, len + 1); strncpy(nstr->arr, (char*)str->arr + start, len); nstr->len = len; str_insert(nstr, len, '\0'); return nstr; }
void list_add(list_t *list, void *item) { list_resize(list); list->items[list->length++] = item; }
void list_pop(caValue* list) { list_resize(list, list_length(list) - 1); }
void resize(caValue* list, int newSize) { ca_assert(is_list(list)); set_pointer(list, list_resize((ListData*) get_pointer(list), newSize)); }
void List::resize(int newSize) { list_resize(this, newSize); }
void List::clear() { list_resize(this, 0); }
void list_push(list* l, void* e){ if(l->size + 1 >= l->capacity) list_resize(l, l->capacity * 2 + 1); l->data[l->size++] = e; }
void list_add(struct List *list, struct Token *t){ list_resize(list,list->length+1); list->tokens[list->length-1] = t; }
void for_loop_finish_iteration(Stack* stack, bool enableLoopOutput) { INCREMENT_STAT(LoopFinishIteration); Frame* frame = top_frame(stack); Branch* contents = frame->branch; // Find list length caValue* listInput = get_frame_register(frame, 0); // Increment the loop index caValue* index = get_top_register(stack, for_loop_find_index(contents)); set_int(index, as_int(index) + 1); // Preserve list output if (enableLoopOutput && frame->exitType != name_Discard) { caValue* outputIndex = get_frame_register(frame, for_loop_find_output_index(contents)); Term* outputPlaceholder = get_output_placeholder(contents, 0); caValue* outputList = get_frame_register(frame, outputPlaceholder); caValue* outputValue = find_stack_value_for_term(stack, outputPlaceholder->input(0), 0); if (!is_list(outputList)) set_list(outputList); list_touch(outputList); copy(outputValue, list_get(outputList, as_int(outputIndex))); INCREMENT_STAT(LoopWriteOutput); // Advance output index set_int(outputIndex, as_int(outputIndex) + 1); } // Check if we are finished if (as_int(index) >= list_length(listInput) || frame->exitType == name_Break || frame->exitType == name_Return) { // Possibly truncate output list, in case any elements were discarded. if (enableLoopOutput) { caValue* outputIndex = get_frame_register(frame, for_loop_find_output_index(contents)); Term* outputPlaceholder = get_output_placeholder(contents, 0); caValue* outputList = get_frame_register(frame, outputPlaceholder); list_resize(outputList, as_int(outputIndex)); } else { Term* outputPlaceholder = get_output_placeholder(contents, 0); caValue* outputList = get_frame_register(frame, outputPlaceholder); set_list(outputList, 0); } finish_frame(stack); return; } // If we're not finished yet, copy rebound outputs back to inputs. for (int i=1;; i++) { Term* input = get_input_placeholder(contents, i); if (input == NULL) break; Term* output = get_output_placeholder(contents, i); copy(get_frame_register(frame, output), get_frame_register(frame, input)); INCREMENT_STAT(Copy_LoopCopyRebound); } // Return to start of loop body frame->pc = 0; frame->nextPc = 0; frame->exitType = name_None; }
caValue* set_list(caValue* value, int size) { set_list(value); list_resize(value, size); return value; }
void list_insert(list_t *list, int index, void *item) { list_resize(list); memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index)); list->length++; list->items[index] = item; }