1 Star 3 Fork 1

黃正雄 / 思库可思 sqxclib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
SqPtrArray.md 3.32 KB
一键复制 编辑 原始数据 按行查看 历史
黃正雄 提交于 2024-01-29 08:15 . update documents and comments.

中文

SqPtrArray

SqPtrArray is pointer array. It is derived classes of SqArray.
SQ_TYPE_PTR_ARRAY is a built-in static constant type that can create SqPtrArray instances.

SqArray

└─── SqPtrArray

     └─── SqStrArray

SqArray and it's derived classes share data structures and code to reduce binary size.

Data members

Like SqArray, SqPtrArray has the same hidden members in front of the array.

SqPtrArray Structure Definition:

struct SqPtrArray
{
	void    **data;
	int       length;
};

There are hidden members in front of SqPtrArray::data :
capacity is the number of elements actually allocated in the array. (excluding the header in front of the array)
clearFunc is a function that clears elements in an array.

Below is the code to access these hidden members:

	// C functions
	capacity   = sq_ptr_array_capacity(array);
	clearFunc  = sq_ptr_array_clear_func(array);

	// C++ methods
	capacity   = array->capacity();
	clearFunc  = array->clearFunc();

Initialize

C function sq_ptr_array_init(), C++ constructor can initialize instance of SqPtrArray.

use C language

	// 'func' can be NULL if you don't need to free the element's memory.
	SqDestroyFunc  func = free;

	SqPtrArray     arrayLocal;
	sq_ptr_array_init(&arrayLocal, capacity, func);
	sq_ptr_array_final(&arrayLocal);

	SqPtrArray    *array;
	array = sq_ptr_array_new(capacity, func);
	sq_ptr_array_free(array);

use C++ language

	// 'func' can be NULL if you don't need to free the element's memory.
	SqDestroyFunc  func = free;

	// Sq::PtrArray has constructor and destructor
	Sq::PtrArray   arrayLocal;

	Sq::PtrArray  *array;
	array = new Sq::PtrArray(capacity, func);
	delete array;

Erase / Steal

SqPtrArray adds erase() function, which is different from SqArray.
erase() removes elements from array with calling the clear function.
steal() removes elements from array without calling the clear function.

use C language

	// erase elements
	sq_ptr_array_erase(array, index, n_elements);

	// steal elements
	sq_ptr_array_steal(array, index, n_elements);

use C++ language

	// erase elements
	array->erase(index, n_elements);

	// steal elements
	array->steal(index, n_elements);

Other functions and methods

User can use the same functions and methods as SqArray.
SqPtrArray still defines some sq_ptr_array_xxx() macros and functions for C language, which are correspond to sq_array_xxx() series.

Allocate

	void **memory;

	memory = sq_ptr_array_alloc(array, count);
	memory = sq_ptr_array_alloc_at(array, index, count);

Append

	void  *ptrs[3] = {NULL};

	sq_ptr_array_push(array, NULL);
	sq_ptr_array_append(array, ptrs, sizeof(ptrs) / sizeof(void*));

Insert

	int   index = 5;

	sq_ptr_array_push_to(array, index, NULL);
	sq_ptr_array_insert(array, index, ptrs, sizeof(ptrs) / sizeof(void*));

Sort

	int  compareFunc(const void **ptr1, const void **ptr2);

	sq_ptr_array_sort(array, (SqCompareFunc)compareFunc);

Find

	void **key;
	void **element;
	int    insertingIndex;

	element = sq_ptr_array_find(array, key, compareFunc);
	element = sq_ptr_array_find_sorted(array, key, compareFunc, &insertingIndex);
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/chhuang-one/sqxclib.git
git@gitee.com:chhuang-one/sqxclib.git
chhuang-one
sqxclib
思库可思 sqxclib
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891