int main(int argc, char *argv[]) { list_type type; type.dup = node_dup; type.free = node_free; type.compare = node_compare; list_t *list = list_create(&type); for (int i = 0; i < 10; ++i) { sds value = sdsempty(); value = sdscatprintf(value, "%d", i); list_add_node_tail(list, value); printf("add %s\n", value); sdsfree(value); } for (int i = 0; i < 10; ++i) { sds value = sdsempty(); value = sdscatprintf(value, "%d", 10 + i); list_add_node_head(list, value); printf("add %s\n", value); sdsfree(value); } list_node *node = list_index(list, 10); sds value = sdsempty(); value = sdscatprintf(value, "%d", 100); list_insert_node(list, node, value, 1); printf("insert %s\n", value); node = list_find(list, value); printf("search: %s\n", (char *)node->value); sdsfree(value); for (int i = -10; i < 10; ++i) { node = list_index(list, i); if (node) { printf("%d: %s\n", i, (char *)node->value); } } list_t *copy = list_dup(list); list_release(list); printf("len: %ld\n", list_len(copy)); list_rotate(copy); list_rotate(copy); list_rotate(copy); list_iter *iter = list_get_iterator(copy, LIST_START_HEAD); while ((node = list_next(iter)) != NULL) { printf("%s\n", (char *)node->value); list_del(copy, node); } list_release_iterator(iter); list_release(copy); printf("len: %ld\n", list_len(copy)); printf("head: %p, tail: %p\n", copy->head, copy->tail); return 0; }
int main() { printf("Testing lists...\n"); List *list = (List *) create_list(); assert(list != NULL); assert(list->data == NULL); assert(list->next == NULL); int w = 42; int x = 1; int y = 2; int z = 3; append_to_list(list, &x); assert(*(int *)(list->data) == 1); assert(list->next != NULL); append_to_list(list, &y); assert(*(int *)(list->next->data) == 2); assert(list->next->next != NULL); append_to_list(list, &z); assert(*(int *)(list->next->next->data) == 3); assert(list->next->next->next != NULL); assert(list->next->next->next->data == NULL); assert(list->next->next->next->next == NULL); list = (List *) push_to_list(list, &w); assert(*(int *)(list->data) == 42); assert(list->next != NULL); list = (List *) pop_from_list(list); assert(*(int *)(list->data) == 1); assert(list_length(list) == 3); assert(list_index(list, &y) == 1); assert(list_index(list, &w) == -1); list = remove_from_list(list, &x); assert(*(int *)(list->data) == 2); list = push_to_list(list, &x); list = remove_from_list(list, &y); assert(*(int *)(list->next->data) == 3); printf("List tests passed\n"); return 0; }
static int op_split_words(string_list *result, const string_list *values, const string_list *args) { string_list_item *item = NULL; char *intermediate, *intermediate_ptr = NULL; char *space_chr = NULL; int status = U1DB_OK; for (item = values->head; item != NULL; item = item->next) { intermediate = strdup(item->data); if (intermediate == NULL) return U1DB_NOMEM; intermediate_ptr = intermediate; while (intermediate_ptr != NULL) { space_chr = strchr(intermediate_ptr, ' '); if (space_chr != NULL) { *space_chr = '\0'; space_chr++; } if (list_index(result, intermediate_ptr) == -1) { if ((status = append(result, intermediate_ptr)) != U1DB_OK) { return status; } } intermediate_ptr = space_chr; } free(intermediate); } return status; }
LIST * order( FRAME * frame, int flags ) { LIST * arg = lol_get( frame->args, 0 ); LIST * result = L0; int src; LISTITER iter = list_begin( arg ); LISTITER const end = list_end( arg ); /* We need to create a graph of order dependencies between the passed * objects. We assume there are no duplicates passed to 'add_pair'. */ int length = list_length( arg ); int * * graph = ( int * * )BJAM_CALLOC( length, sizeof( int * ) ); int * order = ( int * )BJAM_MALLOC( ( length + 1 ) * sizeof( int ) ); for ( src = 0; iter != end; iter = list_next( iter ), ++src ) { /* For all objects this one depends upon, add elements to 'graph'. */ LIST * dependencies = var_get( frame->module, list_item( iter ) ); int index = 0; LISTITER dep_iter = list_begin( dependencies ); LISTITER const dep_end = list_end( dependencies ); graph[ src ] = ( int * )BJAM_CALLOC( list_length( dependencies ) + 1, sizeof( int ) ); for ( ; dep_iter != dep_end; dep_iter = list_next( dep_iter ) ) { int const dst = list_index( arg, list_item( dep_iter ) ); if ( dst != -1 ) graph[ src ][ index++ ] = dst; } graph[ src ][ index ] = -1; } topological_sort( graph, length, order ); { int index = length - 1; for ( ; index >= 0; --index ) { int i; LISTITER iter = list_begin( arg ); LISTITER const end = list_end( arg ); for ( i = 0; i < order[ index ]; ++i, iter = list_next( iter ) ); result = list_push_back( result, object_copy( list_item( iter ) ) ); } } /* Clean up */ { int i; for ( i = 0; i < length; ++i ) BJAM_FREE( graph[ i ] ); BJAM_FREE( graph ); BJAM_FREE( order ); } return result; }
STATIC mp_obj_t list_remove(mp_obj_t self_in, mp_obj_t value) { assert(MP_OBJ_IS_TYPE(self_in, &mp_type_list)); mp_obj_t args[] = {self_in, value}; args[1] = list_index(2, args); list_pop(2, args); return mp_const_none; }
LIST *order( PARSE *parse, FRAME *frame ) { LIST* arg = lol_get( frame->args, 0 ); LIST* tmp; LIST* result = 0; int src, dst; /* We need to create a graph of order dependencies between the passed objects. We assume that there are no duplicates passed to 'add_pair'. */ int length = list_length(arg); int** graph = (int**)calloc(length, sizeof(int*)); int* order = (int*)malloc((length+1)*sizeof(int)); if ( DEBUG_PROFILE ) profile_memory( length*sizeof(int*) + (length+1)*sizeof(int) ); for(tmp = arg, src = 0; tmp; tmp = tmp->next, ++src) { /* For all object this one depend upon, add elements to 'graph' */ LIST* dependencies = var_get(tmp->string); int index = 0; graph[src] = (int*)calloc(list_length(dependencies)+1, sizeof(int)); if ( DEBUG_PROFILE ) profile_memory( (list_length(dependencies)+1)*sizeof(int) ); for(; dependencies; dependencies = dependencies->next) { int dst = list_index(arg, dependencies->string); if (dst != -1) graph[src][index++] = dst; } graph[src][index] = -1; } topological_sort(graph, length, order); { int index = length-1; for(; index >= 0; --index) { int i; tmp = arg; for (i = 0; i < order[index]; ++i, tmp = tmp->next); result = list_new(result, tmp->string); } } /* Clean up */ { int i; for(i = 0; i < length; ++i) free(graph[i]); free(graph); free(order); } return result; }
int main(void) { struct arr_list *arr; int r; arr = create_arr_list(3); printf("list push: 5, 6, 7\n"); list_push(arr, 5); list_push(arr, 6); list_push(arr, 7); print_arr_list(arr); printf("list push: 8, will auto expand\n"); list_push(arr, 8); print_arr_list(arr); printf("list remove at 2\n"); list_removeat(arr, 2); print_arr_list(arr); printf("list pop \n"); list_pop(arr); print_arr_list(arr); printf("list insert 0\n"); list_insert(arr, 0, 3); print_arr_list(arr); r = list_index(arr, 3); printf("list index 3:%d\n", r); r = list_index(arr, 7); printf("list index 7:%d\n", r); printf("list remove 3\n"); list_remove(arr, 3); print_arr_list(arr); printf("list set index 0 = 3\n"); list_set(arr, 0, 3); print_arr_list(arr); free_arr_list(arr); return 0; }
LIST *order( FRAME *frame, int flags ) { LIST* arg = lol_get( frame->args, 0 ); LIST* tmp; LIST* result = 0; int src; /* We need to create a graph of order dependencies between the passed objects. We assume that there are no duplicates passed to 'add_pair'. */ int length = list_length(arg); int** graph = (int**)BJAM_CALLOC(length, sizeof(int*)); int* order = (int*)BJAM_MALLOC((length+1)*sizeof(int)); for(tmp = arg, src = 0; tmp; tmp = tmp->next, ++src) { /* For all object this one depend upon, add elements to 'graph' */ LIST* dependencies = var_get(tmp->value); int index = 0; graph[src] = (int*)BJAM_CALLOC(list_length(dependencies)+1, sizeof(int)); for(; dependencies; dependencies = dependencies->next) { int dst = list_index(arg, dependencies->value); if (dst != -1) graph[src][index++] = dst; } graph[src][index] = -1; } topological_sort(graph, length, order); { int index = length-1; for(; index >= 0; --index) { int i; tmp = arg; for (i = 0; i < order[index]; ++i, tmp = tmp->next); result = list_new(result, object_copy(tmp->value)); } } /* Clean up */ { int i; for(i = 0; i < length; ++i) BJAM_FREE(graph[i]); BJAM_FREE(graph); BJAM_FREE(order); } return result; }
// This is bad performance, keep same formal as ar2tree.108.c struct TreeNode *buildBSTree(struct ListNode *head, int len) { if (head == NULL || len == 0) { return NULL; } int idx = len / 2; // prefer this one, left compact // int idx = (numsSize - 1) / 2; // to match leetcode's result struct TreeNode *n = malloc(sizeof(struct TreeNode)); struct ListNode *ln = list_index(head, idx); n->val = ln->val; n->left = buildBSTree(head, idx); n->right = buildBSTree(ln->next, len - idx - 1); return n; }
int list_remove(struct arr_list *arr, int obj) { int i, index; index = list_index(arr, obj); if (index != -1) { for (i = index; i < arr->index - 1; i++) { arr->arr[i] = arr->arr[i + 1]; } arr->index--; } return index; }
int _tm_has( tm_obj a, tm_obj b ){ switch( a.type ){ case TM_LST:{ return ( list_index( get_list(a), b) != -1 ); } case TM_STR:{ if( b.type != TM_STR) return 0; return _str_find( a.value.str, b.value.str, 0) != -1; } case TM_DCT:{ tm_obj v; return ( dict_iget(get_dict(a), b, &v) ); } } return 0; }
/* parent has child * child in parent */ int obj_in(Object child, Object parent) { switch (TM_TYPE(parent)) { case TYPE_LIST: { return (list_index(GET_LIST(parent), child) != -1); } case TYPE_STR: { if (TM_TYPE(child) != TYPE_STR) return 0; return string_index(GET_STR_OBJ(parent), GET_STR_OBJ(child), 0) != -1; } case TYPE_DICT: { DictNode* node = dict_get_node(GET_DICT(parent), child); if (node == NULL) { return 0; } return 1; } } return 0; }
void write_quad(Quad *quad, Function *func) { QuadOperand *operand1 = quad->operand1; QuadOperand *operand2 = quad->operand2; QuadOperand *result = quad->result; fprintf(output, "; '%c' operation\n", quad->operator); switch(quad->operator) { case '+': { Variable *resvar = (Variable *) result->addr; switch(operand1->type) { case Constant: fprintf(output, "\tmovlw %i\n", operand1->value); break; case Pointer: { Variable *var = (Variable *) operand1->addr; switch(var->scope) { int offset; case Local: offset = list_index(func->symbol_table, var); fprintf(output, "\tmovf H'0c', 0\n"); fprintf(output, "\taddlw %i\n", offset); fprintf(output, "\tmovwf H'04'\n"); fprintf(output, "\tmovf H'00', 0\n"); break; case Global: offset = list_length(bytecode->globals) - list_index(bytecode->globals, var); fprintf(output, "\tmovf H'%x', 0\n", 0x4f - offset); break; } } } switch(operand2->type) { case Constant: fprintf(output, "\taddlw %i\n", operand2->value); fprintf(output, "\tmovwf H'0d'\n"); break; case Pointer: { fprintf(output, "\tmovwf H'0d'\n"); Variable *var = (Variable *) operand2->addr; switch(var->scope) { int offset; case Local: offset = list_index(func->symbol_table, var); fprintf(output, "\tmovf H'0c', 0\n"); fprintf(output, "\taddlw %i\n", offset); fprintf(output, "\tmovwf H'04'\n"); fprintf(output, "\tmovf H'00', 0\n"); break; case Global: offset = list_length(bytecode->globals) - list_index(bytecode->globals, var); fprintf(output, "\tmovf H'%x', 0\n", 0x4f - offset); break; } fprintf(output, "\taddwf H'0d'\n"); break; } } switch(resvar->scope) { int offset; case Local: offset = list_index(func->symbol_table, resvar); fprintf(output, "\tmovf H'0c', 0\n"); fprintf(output, "\taddlw %i\n", offset); fprintf(output, "\tmovwf H'04'\n"); fprintf(output, "\tmovf H'0d', 0\n"); fprintf(output, "\tmovwf H'00'\n"); break; case Global: offset = list_length(bytecode->globals) - list_index(bytecode->globals, resvar); fprintf(output, "\tmovlw H'%x'\n", 0x4f - offset); fprintf(output, "\tmovwf H'04'\n"); fprintf(output, "\tmovf H'0d', 0\n"); fprintf(output, "\tmovwf H'00'\n"); break; case Temporary: break; } break; } case 'c': if(!strncmp(quad->operand1->name, "printf", 5)) { List *characters = (List *) quad->operand2->addr; while(characters->data != NULL) { Variable *character = (Variable *) characters->data; switch(character->type) { case 'c': fprintf(output, "\tmovlw D'%i'\n", (uint8_t)character->value); fprintf(output, "\tmovwf H'06'\n"); break; case 'i': if(character->scope = Temporary) { fprintf(output, "\tmovf H'0d', 0\n"); fprintf(output, "\tmovwf H'06'\n"); break; } int offset; switch(character->scope) { case Local: offset = list_index(func->symbol_table, character); fprintf(output, "\tmovf H'0c', 0\n"); fprintf(output, "\taddlw %i\n", offset); break; case Global: offset = list_length(bytecode->globals) - list_index(bytecode->globals, character); fprintf(output, "\tmovf H'%x', 0\n", 0x4f - offset); } fprintf(output, "\tmovwf H'04'\n"); fprintf(output, "\tmovf H'00, 0\n"); fprintf(output, "\tmovwf H'06'\n"); break; } characters = characters->next; } } else { fprintf(output, "\tcall func_%s\n", quad->operand1->name); } break; default: fprintf(output, "\tINVALID OPERATOR %c\n", quad->operator); } }
int main(int argc, const char *argv[]) { CmdArgParser parser(argc, argv); parser.setHeader("Archive Data Tool version " ARCH_VERSION_TXT ", " EPICS_VERSION_STRING ", built " __DATE__ ", " __TIME__ "\n\n" ); parser.setArgumentsInfo("<index-file>"); CmdArgFlag help (parser, "help", "Show help"); CmdArgInt verbosity (parser, "verbose", "<level>", "Show more info"); CmdArgFlag info (parser, "info", "Simple archive info"); CmdArgFlag list_index (parser, "list", "List channel name info"); CmdArgString copy_index (parser, "copy", "<new index>", "Copy channels"); CmdArgString start_time (parser, "start", "<time>", "Format: \"mm/dd/yyyy[ hh:mm:ss[.nano-secs]]\""); CmdArgString end_time (parser, "end", "<time>", "(exclusive)"); CmdArgDouble file_limit (parser, "file_limit", "<MB>", "File Size Limit"); CmdArgString basename (parser, "basename", "<string>", "Basename for new data files"); CmdArgFlag enforce_off (parser, "append_off", "Enforce a final 'Archive_Off' value when copying data"); CmdArgString dir2index (parser, "dir2index", "<dir. file>", "Convert old directory file to index"); CmdArgString index2dir (parser, "index2dir", "<dir. file>", "Convert index to old directory file"); CmdArgInt RTreeM (parser, "M", "<1-100>", "RTree M value"); CmdArgFlag dump_blocks (parser, "blocks", "List channel's data blocks"); CmdArgFlag all_blocks (parser, "Blocks", "List all data blocks"); CmdArgString dotindex (parser, "dotindex", "<dot filename>", "Dump contents of RTree index into dot file"); CmdArgString channel_name(parser, "channel", "<name>", "Channel name"); CmdArgFlag hashinfo (parser, "hashinfo", "Show Hash table info"); CmdArgString seek_test (parser, "seek", "<time>", "Perform seek test"); CmdArgFlag test (parser, "test", "Perform some consistency tests"); try { // Defaults RTreeM.set(50); file_limit.set(100.0); // Get Arguments if (! parser.parse()) return -1; if (help || parser.getArguments().size() != 1) { parser.usage(); return -1; } // Consistency checks if ((dump_blocks || dotindex.get().length() > 0 || seek_test.get().length() > 0) && channel_name.get().length() <= 0) { fprintf(stderr, "Options 'blocks' and 'dotindex' require 'channel'.\n"); return -1; } verbose = verbosity; stdString index_name = parser.getArgument(0); DataWriter::file_size_limit = (unsigned long)(file_limit*1024*1024); if (file_limit < 10.0) fprintf(stderr, "file_limit values under 10.0 MB are not useful\n"); // Start/end time epicsTime *start = 0, *end = 0; stdString txt; if (start_time.get().length() > 0) { start = new epicsTime; string2epicsTime(start_time.get(), *start); if (verbose > 1) printf("Using start time %s\n", epicsTimeTxt(*start, txt)); } if (end_time.get().length() > 0) { end = new epicsTime(); string2epicsTime(end_time.get(), *end); if (verbose > 1) printf("Using end time %s\n", epicsTimeTxt(*end, txt)); } // Base name if (basename.get().length() > 0) DataWriter::data_file_name_base = basename.get(); if (enforce_off) do_enforce_off = true; // What's requested? if (info) list_names(index_name, false); else if (list_index) list_names(index_name, true); else if (copy_index.get().length() > 0) { copy(index_name, copy_index, RTreeM, start, end, channel_name); return 0; } else if (hashinfo) show_hash_info(index_name); else if (dir2index.get().length() > 0) { convert_dir_index(RTreeM, dir2index, index_name); return 0; } else if (index2dir.get().length() > 0) { convert_index_dir(index_name, index2dir); return 0; } else if (all_blocks) { dump_all_datablocks(index_name); return 0; } else if (dump_blocks) { dump_datablocks(index_name, channel_name); return 0; } else if (dotindex.get().length() > 0) { dot_index(index_name, channel_name, dotindex); return 0; } else if (seek_test.get().length() > 0) { seek_time(index_name, channel_name, seek_test); return 0; } else if (test) { return check(index_name) ? 0 : -1; } else { parser.usage(); return -1; } } catch (GenericException &e) { fprintf(stderr, "Error:\n%s\n", e.what()); } return 0; }
int list_stdlib_bsearch(list_t *l, void *key, int (*cmp)(const void *a, const void *b)) { void *p= bsearch(key,l->d, l->q,l->s,cmp); if(p==NULL)return -1; return list_index(l,p); }