void case_list_iter() { struct list *list = list(); char *s1 = "s1", *s2 = "s2", *s3 = "s3"; list_push(list, s1); list_push(list, s2); list_push(list, s3); struct list_iter *iter = list_iter(list); assert(iter->list == list); assert(iter->node == list->head); assert(list_iter_next(iter) == s1); assert(list_iter_next(iter) == s2); assert(list_iter_next(iter) == s3); list_iter_seek_tail(iter); assert(list_iter_prev(iter) == s3); assert(list_iter_prev(iter) == s2); assert(list_iter_prev(iter) == s1); list_iter_free(iter); int i = 0; struct list_node *node; list_each(list, node) { char *s = node->data; assert(s[1] - 49 == i); i += 1; }
/* ============================================================================= * net_findDescendants * -- Contents of bitmapPtr set to 1 if descendants, else 0 * -- Returns false if id is not root node (i.e., has cycle back id) * ============================================================================= */ bool_t net_findDescendants (net_t* netPtr, long id, bitmap_t* descendantBitmapPtr, queue_t* workQueuePtr) { bool_t status; vector_t* nodeVectorPtr = netPtr->nodeVectorPtr; assert(descendantBitmapPtr->numBit == vector_getSize(nodeVectorPtr)); bitmap_clearAll(descendantBitmapPtr); queue_clear(workQueuePtr); { net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id); list_t* childIdListPtr = nodePtr->childIdListPtr; list_iter_t it; list_iter_reset(&it, childIdListPtr); while (list_iter_hasNext(&it, childIdListPtr)) { long childId = (long)list_iter_next(&it, childIdListPtr); status = bitmap_set(descendantBitmapPtr, childId); assert(status); status = queue_push(workQueuePtr, (void*)childId); assert(status); } } while (!queue_isEmpty(workQueuePtr)) { long childId = (long)queue_pop(workQueuePtr); if (childId == id) { queue_clear(workQueuePtr); return FALSE; } net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, childId); list_t* grandChildIdListPtr = nodePtr->childIdListPtr; list_iter_t it; list_iter_reset(&it, grandChildIdListPtr); while (list_iter_hasNext(&it, grandChildIdListPtr)) { long grandChildId = (long)list_iter_next(&it, grandChildIdListPtr); if (!bitmap_isSet(descendantBitmapPtr, grandChildId)) { status = bitmap_set(descendantBitmapPtr, grandChildId); assert(status); status = queue_push(workQueuePtr, (void*)grandChildId); assert(status); } } } return TRUE; }
/* ============================================================================= * net_findAncestors * -- Contents of bitmapPtr set to 1 if ancestor, else 0 * -- Returns false if id is not root node (i.e., has cycle back id) * ============================================================================= */ bool_t net_findAncestors (net_t* netPtr, long id, bitmap_t* ancestorBitmapPtr, queue_t* workQueuePtr) { bool_t status; vector_t* nodeVectorPtr = LocalLoad(&netPtr->nodeVectorPtr); assert(LocalLoad(&ancestorBitmapPtr->numBit) == vector_getSize(nodeVectorPtr)); bitmap_clearAll(ancestorBitmapPtr); queue_clear(workQueuePtr); { net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id); list_t* parentIdListPtr = LocalLoad(&nodePtr->parentIdListPtr); list_iter_t it; list_iter_reset(&it, parentIdListPtr); while (list_iter_hasNext(&it, parentIdListPtr)) { long parentId = (long)list_iter_next(&it, parentIdListPtr); status = bitmap_set(ancestorBitmapPtr, parentId); assert(status); status = queue_push(workQueuePtr, (void*)parentId); assert(status); } } while (!queue_isEmpty(workQueuePtr)) { long parentId = (long)queue_pop(workQueuePtr); if (parentId == id) { queue_clear(workQueuePtr); return FALSE; } net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, parentId); list_t* grandParentIdListPtr = LocalLoad(&nodePtr->parentIdListPtr); list_iter_t it; list_iter_reset(&it, grandParentIdListPtr); while (list_iter_hasNext(&it, grandParentIdListPtr)) { long grandParentId = (long)list_iter_next(&it, grandParentIdListPtr); if (!bitmap_isSet(ancestorBitmapPtr, grandParentId)) { status = bitmap_set(ancestorBitmapPtr, grandParentId); assert(status); status = queue_push(workQueuePtr, (void*)grandParentId); assert(status); } } } return TRUE; }
boxdir * boxtree_add_folder(const char * path, const char * id, jobj * folder) { boxdir * aDir; boxfile * aFile, * part; list_iter it, pit; jobj * obj, *item; char * type; aDir = boxdir_create(); aDir->id = strdup(id); if(options.verbose) syslog(LOG_DEBUG, "Adding %s", path); obj = jobj_get(folder, "entries"); it = list_get_iter(obj->children); for(; it; it = list_iter_next(it)) { item = list_iter_getval(it); aFile = obj_to_file(item); type = jobj_getval(item, "type"); if(!strcmp(type,"folder")) list_append(aDir->folders, aFile); else { if(options.splitfiles && ends_with(aFile->name, PART_SUFFIX)) list_insert_sorted_comp(aDir->pieces, aFile, filename_compare); else list_append(aDir->files, aFile); } free(type); } if(options.splitfiles) { it = list_get_iter(aDir->files); pit = list_get_iter(aDir->pieces); for(; pit; pit = list_iter_next(pit)) { part = (boxfile*)list_iter_getval(pit); find_file_for_part(part->name, &it); if(it) { aFile = (boxfile*)list_iter_getval(it); aFile->size+=part->size; } else { syslog(LOG_WARNING, "Stale file part %s found", part->name ); it = list_get_iter(aDir->files); } } } xmlHashAddEntry(allDirs, path, aDir); return aDir; }
int main ( int argc, char *argv[] ) { List* list = list_create ( sizeof ( int ),EqualInt ); int t; ListNode* n; int i; for ( i=0; i<10; ++i ) list_push_back ( list,createInt ( &t,rand() ) ); /*list_foreach(list,printInt);*/ ListIter iter = list_get_iter ( list ); while ( list_iter_hasNext ( iter ) ) { printInt ( list_iter_next ( iter ) ); } printf ( "\n" ); list_pop_back ( list ); list_foreach ( list,incInt ); list_foreach ( list,printInt ); printf ( "\n" ); n = list_find_first_node ( list,createInt ( &t,846930887 ) ); list_erase_node ( list,n ); list_foreach ( list,printInt ); list_delete ( list ); return 0; }
/* ============================================================================= * hashtable_iter_next * ============================================================================= */ void* hashtable_iter_next (hashtable_iter_t* itPtr, hashtable_t* hashtablePtr) { long bucket; long numBucket = hashtablePtr->numBucket; list_t** buckets = hashtablePtr->buckets; list_iter_t it = itPtr->it; void* dataPtr = NULL; for (bucket = itPtr->bucket; bucket < numBucket; /* inside body */) { list_t* chainPtr = hashtablePtr->buckets[bucket]; if (list_iter_hasNext(&it, chainPtr)) { pair_t* pairPtr = (pair_t*)list_iter_next(&it, chainPtr); dataPtr = pairPtr->secondPtr; break; } /* May use dummy bucket; see allocBuckets() */ list_iter_reset(&it, buckets[++bucket]); } itPtr->bucket = bucket; itPtr->it = it; return dataPtr; }
/* ============================================================================= * isCycle * ============================================================================= */ static bool_t isCycle (vector_t* nodeVectorPtr, net_node_t* nodePtr) { switch (nodePtr->mark) { case NET_NODE_MARK_INIT: { nodePtr->mark = NET_NODE_MARK_TEST; list_t* childIdListPtr = nodePtr->childIdListPtr; list_iter_t it; list_iter_reset(&it, childIdListPtr); while (list_iter_hasNext(&it, childIdListPtr)) { long childId = (long)list_iter_next(&it, childIdListPtr); net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, childId); if (isCycle(nodeVectorPtr, childNodePtr)) { return TRUE; } } break; } case NET_NODE_MARK_TEST: return TRUE; case NET_NODE_MARK_DONE: return FALSE; break; default: assert(0); } nodePtr->mark = NET_NODE_MARK_DONE; return FALSE; }
const char * errors_describe (intmax_t errnum){ // {{{ err_item key; err_item *item; err_item *dynamic_list; void *iter = NULL; uintmax_t errnum_pos = -errnum; uintmax_t errnum_emod = errnum_pos / ESTEP; if(errnum_emod == 0){ if( (key.errmsg = strerror(errnum_pos)) != NULL) return key.errmsg; }else if(errnum_emod < DYNAMIC_EMODULE_START){ // core errors list key.errnum = errnum; if((item = bsearch(&key, errs_list, errs_list_nelements, errs_list_size, &err_bsearch_int)) != NULL ) return item->errmsg; }else{ // dynamic errs list while( (dynamic_list = list_iter_next(&dynamic_errors, &iter)) != NULL){ for(item = dynamic_list; item->errnum != 0; item++){ if(errnum == item->errnum) return item->errmsg; } } } return "unknown error"; } // }}}
static void printHashtable (hashtable_t* hashtablePtr) { long i; hashtable_iter_t it; printf("["); hashtable_iter_reset(&it, hashtablePtr); while (hashtable_iter_hasNext(&it, hashtablePtr)) { printf("%li ", *((long*)(hashtable_iter_next(&it, hashtablePtr)))); } puts("]"); /* Low-level to see structure */ for (i = 0; i < hashtablePtr->numBucket; i++) { list_iter_t it; printf("%2li: [", i); list_iter_reset(&it, hashtablePtr->buckets[i]); while (list_iter_hasNext(&it, hashtablePtr->buckets[i])) { void* pairPtr = list_iter_next(&it, hashtablePtr->buckets[i]); printf("%li ", *(long*)(((pair_t*)pairPtr)->secondPtr)); } puts("]"); } }
void list_dump(list *l, const char * fmt) { list_iter it = list_get_iter(l); printf("list size is %d\n", list_size(l)); while(it) { printf(fmt, list_iter_getval(it)); it = list_iter_next(it); } }
/* boxtree_* functions and helpers used at mount time to fill the allDirs hash */ void find_file_for_part(const char * pname, list_iter * it) { boxfile * f; while(*it) { f = (boxfile*) list_iter_getval(*it); if(!strncmp(f->name, pname, strlen(f->name))) break; *it = list_iter_next(*it); } }
void game_lifespan_checks(game_t *gm) { list_iter_t iter; player_t *pl; list_iter_init(&iter, gm->ga_players, FWD); while ((pl = list_iter_next(&iter))) { player_lifespan_check(pl, gm->ga_freq); } }
static void printList (list_t* listPtr) { list_iter_t it; printf("["); list_iter_reset(&it, listPtr); while (list_iter_hasNext(&it, listPtr)) { printf("%li ", *((long*)(list_iter_next(&it, listPtr)))); } puts("]"); }
void case_list_iter() { struct list *list = list(); char *s1 = "s1", *s2 = "s2", *s3 = "s3"; list_push(list, s1); list_push(list, s2); list_push(list, s3); struct list_iter *iter = list_iter(list); assert(iter->list == list); assert(iter->node == list->head); assert(list_iter_next(iter) == s1); assert(list_iter_next(iter) == s2); assert(list_iter_next(iter) == s3); list_iter_seek_tail(iter); assert(list_iter_prev(iter) == s3); assert(list_iter_prev(iter) == s2); assert(list_iter_prev(iter) == s1); list_iter_free(iter); list_free(list); }
jobj * jobj_array_item(const jobj * obj, int at) { list_iter it; int i; if(obj->type != T_ARR) return NULL; if(at >= list_size(obj->children)) return NULL; it = list_get_iter(obj->children); for(i=0; i<at; ++i) it = list_iter_next(it); return (jobj*) list_iter_getval(it); }
static struct hashmap_pair *_hashmap_list_find(hashmap_t map, list_t list, int key){ list_iter_t it = list_iter_create(list); struct hashmap_pair *p, *ret = NULL; while ((p = list_iter_next(it)) != NULL) { if (map->eq(p->key, key)) { ret = p; break; } } list_iter_destroy(&it); return ret; }
player_t *game_find_pl(game_t *gm, unsigned int id) { list_iter_t it; player_t *pl = NULL; for (list_iter_init(&it, gm->ga_players, FWD); (pl = list_iter_access(&it)); list_iter_next(&it)) { if (pl->p_id == id) return (pl); } return (pl); }
static ssize_t data_format_t_convert_to(data_t *src, fastcall_convert_to *fargs){ // {{{ ssize_t ret; format_t value; uintmax_t transfered; keypair_t *kp; void *iter = NULL; char *string = "(unknown)"; if(fargs->dest == NULL || src->ptr == NULL) return -EINVAL; value = *(format_t *)src->ptr; switch(fargs->format){ case FORMAT(config):; case FORMAT(clean):; case FORMAT(human):; // find in static keys first for(kp = &formats[0]; kp->key_str != NULL; kp++){ if(kp->key_val == value){ string = kp->key_str; goto found; } } #ifdef RESOLVE_DYNAMIC_KEYS // find in dynamic keys list_rdlock(&dynamic_formats); while( (kp = list_iter_next(&dynamic_formats, &iter)) != NULL){ if(kp->key_val == value){ string = kp->key_str; list_unlock(&dynamic_formats); goto found; } } list_unlock(&dynamic_formats); #endif found:; fastcall_write r_write = { { 5, ACTION_WRITE }, 0, string, strlen(string) }; ret = data_query(fargs->dest, &r_write); transfered = r_write.buffer_size; break; default: return -ENOSYS; } if(fargs->header.nargs >= 5) fargs->transfered = transfered; return ret; } // }}}
list_iter boxpath_next_part(boxpath * bpath, list_iter it) { boxfile * part; it = list_iter_next(it); if(it) { part = (boxfile*) list_iter_getval(it); if(strncmp(bpath->file->name, part->name, strlen(bpath->file->name))) return NULL; } return it; }
struct list_iter list_erase(struct list *l,struct list_iter it) { assert(l); struct list_iter it_next = list_iter_next(it); struct node *n = it.n; struct node *P = n->pre; struct node *N = n->next; P->next = N; N->pre = P; free(n); --l->size; return it_next; }
static void printTable (table_t* tablePtr) { long i; for (i = 0; i < tablePtr->numBucket; i++) { list_iter_t it; printf("%2i: [", i); list_iter_reset(&it, tablePtr->buckets[i]); while (list_iter_hasNext(&it, tablePtr->buckets[i])) { printf("%li ", *(long*)list_iter_next(&it, tablePtr->buckets[i])); } puts("]"); } }
void hashmap_destroy(hashmap_t *map){ assert(map != NULL); assert((*map) != NULL); for (int i = 0; i < (*map)->capacity; i++) { list_iter_t it = list_iter_create((*map)->lists[i]); struct hashmap_pair *p; while ((p = list_iter_next(it)) != NULL) { free(p); } list_iter_destroy(&it); list_destroy((*map)->lists + i); } free((*map)->lists); free((*map)); *map = NULL; }
void jobj_free(jobj * obj) { list_iter it; if(obj->type = T_VAL) { free(obj->value); return; } it = list_get_iter(obj->children); for(; it; it = list_iter_next(it)) { jobj_free(list_iter_getval(it)); } list_free(obj->children); free(obj); }
jobj * jobj_get(const jobj * obj, const char * key) { list_iter it; if(obj->type == T_VAL) return NULL; it = list_get_iter(obj->children); jobj * item; for(; it; it = list_iter_next(it)) { item = list_iter_getval(it); if(!strcmp(item->key, key)) return item; } return NULL; }
list_iter boxpath_first_part(boxpath * bpath) { list_iter it; boxfile * part; if(!boxpath_getfile(bpath)) return NULL; it = list_get_iter(bpath->dir->pieces); while(it && (filename_compare(bpath->file, list_iter_getval(it)) > 0)) it = list_iter_next(it); if(it) { part = (boxfile*) list_iter_getval(it); if(strncmp(bpath->base, part->name, strlen(bpath->base))) return NULL; } return it; }
/* ============================================================================= * net_hasEdge * ============================================================================= */ bool_t net_hasEdge (net_t* netPtr, long fromId, long toId) { vector_t* nodeVectorPtr = netPtr->nodeVectorPtr; net_node_t* childNodePtr = (net_node_t*)vector_at(nodeVectorPtr, toId); list_t* parentIdListPtr = childNodePtr->parentIdListPtr; list_iter_t it; list_iter_reset(&it, parentIdListPtr); while (list_iter_hasNext(&it, parentIdListPtr)) { long parentId = (long)list_iter_next(&it, parentIdListPtr); if (parentId == fromId) { return TRUE; } } return FALSE; }
boxfile * obj_to_file(jobj * obj) { list_iter it; jobj * item; boxfile * f = (boxfile*) malloc(sizeof(boxfile)); memset(f, 0, sizeof(boxfile)); it = list_get_iter(obj->children); for(; it; it = list_iter_next(it)) { item = list_iter_getval(it); if(!strcmp(item->key,"id")) f->id = strdup(item->value); else if(!strcmp(item->key, "size")) f->size = atoll(item->value); else if(!strcmp(item->key, "name")) f->name = strdup(item->value); else if(!strcmp(item->key, "created_at")) f->ctime = unix_time(item->value); else if(!strcmp(item->key, "modified_at")) f->mtime = unix_time(item->value); } return f; }
int boxpath_getfile(boxpath * bpath) { list_iter it; boxfile * aFile; /* check for obvious cases */ if(!bpath) return FALSE; if(bpath->file) return TRUE; it = list_get_iter(bpath->is_dir ? bpath->dir->folders : bpath->dir->files); for(; it; it = list_iter_next(it)) { aFile = (boxfile*)list_iter_getval(it); if(!strcmp(aFile->name, bpath->base)) { bpath->file = aFile; return TRUE; } } return FALSE; }
/* ============================================================================= * net_isPath * ============================================================================= */ bool_t net_isPath (net_t* netPtr, long fromId, long toId, bitmap_t* visitedBitmapPtr, queue_t* workQueuePtr) { bool_t status; vector_t* nodeVectorPtr = netPtr->nodeVectorPtr; assert(visitedBitmapPtr->numBit == vector_getSize(nodeVectorPtr)); bitmap_clearAll(visitedBitmapPtr); queue_clear(workQueuePtr); status = queue_push(workQueuePtr, (void*)fromId); assert(status); while (!queue_isEmpty(workQueuePtr)) { long id = (long)queue_pop(workQueuePtr); if (id == toId) { queue_clear(workQueuePtr); return TRUE; } status = bitmap_set(visitedBitmapPtr, id); assert(status); net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, id); list_t* childIdListPtr = nodePtr->childIdListPtr; list_iter_t it; list_iter_reset(&it, childIdListPtr); while (list_iter_hasNext(&it, childIdListPtr)) { long childId = (long)list_iter_next(&it, childIdListPtr); if (!bitmap_isSet(visitedBitmapPtr, childId)) { status = queue_push(workQueuePtr, (void*)childId); assert(status); } } } return FALSE; }
// Move a dir to another path in the tree, // recursively updating all the child entries in allDirs void boxtree_movedir(const char * from, const char * to) { boxdir * aDir = xmlHashLookup(allDirs, from); list_iter it; char * newfrom, * newto, *name; if(!aDir) { syslog(LOG_ERR, "no such directory %s", from); return; } for (it=list_get_iter(aDir->folders); it; it = list_iter_next(it)) { name = ((boxfile*)list_iter_getval(it))->name; newfrom = pathappend(from, name); newto = pathappend(to, name); boxtree_movedir(newfrom, newto); free(newfrom); free(newto); } //LOCKDIR(aDir); xmlHashRemoveEntry(allDirs, from, NULL); xmlHashAddEntry(allDirs, to, aDir); //UNLOCKDIR(aDir); }