dynarray.c,C语言,动态数组,动态字符串列表
2012-04-15 11:39
375 查看
在android4.0的源码中看到的一个动态数组的实现,用来处理字串比较方便:system/core/toolbox/dynarray.c
dynarray.h
dynarray.c
dynarray.h
#ifndef DYNARRAY_H #define DYNARRAY_H #include <stddef.h> /* simple dynamic array of pointers */ typedef struct { int count; int capacity; void** items; } dynarray_t; #define DYNARRAY_INITIALIZER { 0, 0, NULL } void dynarray_init( dynarray_t *a ); void dynarray_done( dynarray_t *a ); void dynarray_append( dynarray_t *a, void* item ); /* Used to iterate over a dynarray_t * _array :: pointer to the array * _item_type :: type of objects pointed to by the array * _item :: name of a local variable defined within the loop * with type '_item_type' * _stmnt :: C statement that will be executed in each iteration. * * You case use 'break' and 'continue' within _stmnt * * This macro is only intended for simple uses. I.e. do not add or * remove items from the array during iteration. */ #define DYNARRAY_FOREACH_TYPE(_array,_item_type,_item,_stmnt) \ do { \ int _nn_##__LINE__ = 0; \ for (;_nn_##__LINE__ < (_array)->count; ++ _nn_##__LINE__) { \ _item_type _item = (_item_type)(_array)->items[_nn_##__LINE__]; \ _stmnt; \ } \ } while (0) #define DYNARRAY_FOREACH(_array,_item,_stmnt) \ DYNARRAY_FOREACH_TYPE(_array,void *,_item,_stmnt) /* Simple dynamic string arrays * * NOTE: A strlist_t owns the strings it references. */ typedef dynarray_t strlist_t; #define STRLIST_INITIALIZER DYNARRAY_INITIALIZER /* Used to iterate over a strlist_t * _list :: pointer to strlist_t object * _string :: name of local variable name defined within the loop with * type 'char*' * _stmnt :: C statement executed in each iteration * * This macro is only intended for simple uses. Do not add or remove items * to/from the list during iteration. */ #define STRLIST_FOREACH(_list,_string,_stmnt) \ DYNARRAY_FOREACH_TYPE(_list,char *,_string,_stmnt) void strlist_init( strlist_t *list ); /* note: strlist_done will free all the strings owned by the list */ void strlist_done( strlist_t *list ); /* append a new string made of the first 'slen' characters from 'str' * followed by a trailing zero. */ void strlist_append_b( strlist_t *list, const void* str, size_t slen ); /* append the copy of a given input string to a strlist_t */ void strlist_append_dup( strlist_t *list, const char *str); /* sort the strings in a given list (using strcmp) */ void strlist_sort( strlist_t *list ); #endif /* DYNARRAY_H */
dynarray.c
#include "dynarray.h" #include <stdlib.h> #include <string.h> #include <limits.h> void dynarray_init( dynarray_t *a ) { a->count = a->capacity = 0; a->items = NULL; } static void dynarray_reserve_more( dynarray_t *a, int count ) { int old_cap = a->capacity; int new_cap = old_cap; const int max_cap = INT_MAX/sizeof(void*); void** new_items; int new_count = a->count + count; if (count <= 0) return; if (count > max_cap - a->count) abort(); new_count = a->count + count; while (new_cap < new_count) { old_cap = new_cap; new_cap += (new_cap >> 2) + 4; if (new_cap < old_cap || new_cap > max_cap) { new_cap = max_cap; } } new_items = realloc(a->items, new_cap*sizeof(void*)); if (new_items == NULL) abort(); a->items = new_items; a->capacity = new_cap; } void dynarray_append( dynarray_t *a, void* item ) { if (a->count >= a->capacity) dynarray_reserve_more(a, 1); a->items[a->count++] = item; } void dynarray_done( dynarray_t *a ) { free(a->items); a->items = NULL; a->count = a->capacity = 0; } // string arrays void strlist_init( strlist_t *list ) { dynarray_init(list); } void strlist_append_b( strlist_t *list, const void* str, size_t slen ) { char *copy = malloc(slen+1); memcpy(copy, str, slen); copy[slen] = '\0'; dynarray_append(list, copy); } void strlist_append_dup( strlist_t *list, const char *str) { strlist_append_b(list, str, strlen(str)); } void strlist_done( strlist_t *list ) { STRLIST_FOREACH(list, string, free(string)); dynarray_done(list); } static int strlist_compare_strings(const void* a, const void* b) { const char *sa = *(const char **)a; const char *sb = *(const char **)b; return strcmp(sa, sb); } void strlist_sort( strlist_t *list ) { if (list->count > 0) { qsort(list->items, (size_t)list->count, sizeof(void*), strlist_compare_strings); } }
相关文章推荐
- redis 笔记01 简单动态字符串、链表、字典、跳跃表、整数集合、压缩列表
- JavaScript把字符串动态显示到下拉列表
- 字符串列表的C语言实现:c_strlist
- 《C语言及程序设计》实践项目——动态数组 ->【项目2-动态数组排序】
- 【C语言】快速排序优质算法的动态显示和字符串的快排
- C语言实现动态字符串,并且统计大小写
- 【C语言】快速排序优质算法的动态显示和字符串的快排
- 字符串列表的C语言实现:c_strlist
- 怎么将获得的字符串解析并动态显示在下拉列表
- C语言:动态内存malloc,calloc,realloc
- C语言-字符串逆序
- C语言字符串详解
- JS根据动态生成的字符串,验证是否存在对应function并执行
- C语言 字符串替换
- 关于NGUI的动态列表加入子项的注意点!!!
- Java:布尔值,字符范围,字符串声明,换行操作,常量定义与c语言的不同
- c语言字符串与整数之间的转换
- C语言中查找字符在字符串中出现的位置的方法
- redis 数据结构 简单动态字符串和链表
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结