-
Quick sort: tb_quick_sort -
Heap sorting: tb_heap_sort -
Insert Sort: tb_bubble_sort -
Bubble sort: tb_insert_sort
-
For containers with random iteration characteristics, use quick sorting to optimize -
For containers with random iteration characteristics and super large scale, heap sorting is adopted -
Bubble sort containers that can only iterate linearly
//Initialize a vector. The element type is tb_long_t, and the full 256 elements will grow automatically tb_vector_ref_t vector = tb_vector_init(256, tb_item_func_long()); if (vector) { //Insert some elements tb_vector_insert_tail(vector, (tb_cpointer_t)10); tb_vector_insert_tail(vector, (tb_cpointer_t)2); tb_vector_insert_tail(vector, (tb_cpointer_t)5); tb_vector_insert_tail(vector, (tb_cpointer_t)6); tb_vector_insert_tail(vector, (tb_cpointer_t)9); //Sort all. The second parameter is the comparator function. By default, the container built-in comparator is used tb_sort_all(vector, tb_null); //Release vector tb_vector_exit(vector); }
-
Linear lookup: tb_find -
Reverse linear lookup: tb_rfind -
Binary search: tb_binary_find
//Initialize a vector. The element type is tb_long_t, and the full 256 elements will grow automatically tb_vector_ref_t vector = tb_vector_init(256, tb_item_func_long()); if (vector) { //Insert some ordered elements tb_vector_insert_tail(vector, (tb_cpointer_t)1); tb_vector_insert_tail(vector, (tb_cpointer_t)2); tb_vector_insert_tail(vector, (tb_cpointer_t)4); tb_vector_insert_tail(vector, (tb_cpointer_t)6); tb_vector_insert_tail(vector, (tb_cpointer_t)9); //The binary search method is used to quickly find elements, and the algorithm complexity is O (log2) tb_size_t itor = tb_binary_find_all(vector, (tb_cpointer_t)5); if (itor != tb_iterator_tail(vector)) { //Get element value: 5 tb_size_t value = tb_iterator_item(vector, itor); } //Release vector tb_vector_exit(vector); }
//Compare functions in descending order static tb_long_t your_comp_func(tb_iterator_ref_t iterator, tb_cpointer_t ltem, tb_cpointer_t rtem) { return ((tb_long_t)ltem < (tb_long_t)rtem? 1 : ((tb_long_t)ltem > (tb_long_t)rtem? - 1 : 0)); } //Initialize a vector. The element type is tb_long_t, and the full 256 elements will grow automatically tb_vector_ref_t vector = tb_vector_init(256, tb_item_func_long()); if (vector) { //Insert some ordered elements tb_vector_insert_tail(vector, (tb_cpointer_t)1); tb_vector_insert_tail(vector, (tb_cpointer_t)2); tb_vector_insert_tail(vector, (tb_cpointer_t)4); tb_vector_insert_tail(vector, (tb_cpointer_t)6); tb_vector_insert_tail(vector, (tb_cpointer_t)9); //Use binary search method to quickly find elements and specify your own comparator function tb_size_t itor = tb_binary_find_all_if(vector, your_comp_func, tb_null); if (itor != tb_iterator_tail(vector)) { //Get element value: 5 tb_size_t value = tb_iterator_item(vector, itor); } //Release vector tb_vector_exit(vector); }
//Define the data structure of a character set operation typedef struct __tb_charset_t { tb_size_t type; tb_char_t const* name; tb_long_t (*get)(tb_static_stream_ref_t sstream, tb_bool_t be, tb_uint32_t* ch); tb_long_t (*set)(tb_static_stream_ref_t sstream, tb_bool_t be, tb_uint32_t ch); }tb_charset_t; //Define a native array static tb_charset_t charsets[] = { {TB_CHARSET_TYPE_ASCII, "ascii", tb_charset_ascii_get, tb_charset_ascii_set } , {TB_CHARSET_TYPE_GB2312, "gb2312", tb_charset_gb2312_get, tb_charset_gb2312_set } , {TB_CHARSET_TYPE_GBK, "gbk", tb_charset_gb2312_get, tb_charset_gb2312_set } , {TB_CHARSET_TYPE_ISO8859, "iso8859", tb_charset_iso8859_get, tb_charset_iso8859_set } , {TB_CHARSET_TYPE_UCS2, "ucs3", tb_charset_ucs2_get, tb_charset_ucs2_set } , {TB_CHARSET_TYPE_UCS4, "ucs4", tb_charset_ucs4_get, tb_charset_ucs4_set } , {TB_CHARSET_TYPE_UTF16, "utf16", tb_charset_utf16_get, tb_charset_utf16_set } , {TB_CHARSET_TYPE_UTF32, "utf32", tb_charset_utf32_get, tb_charset_utf32_set } , {TB_CHARSET_TYPE_UTF8, "utf8", tb_charset_utf8_get, tb_charset_utf8_set } }; //Find Comparison Function by Name static tb_long_t tb_charset_comp_by_name(tb_iterator_ref_t iterator, tb_cpointer_t item, tb_cpointer_t name) { return tb_stricmp(((tb_charset_ref_t)item)->name, (tb_char_t const*)name); } //Initialize the original array into an iterator tb_iterator_t iterator = tb_iterator_init_mem(charsets, tb_arrayn(charsets), sizeof(tb_charset_t)); //Perform binary search according to the name for this iterator tb_size_t itor = tb_binary_find_all_if(&iterator, tb_charset_comp_by_name, "utf8"); //If found if (itor != tb_iterator_tail(&iterator)) { //Get Element Object tb_charset_t* charset = (tb_charset_t*)tb_iterator_item(&iterator, itor); }