├── .gitignore ├── CHANGES.txt ├── LICENSE.md ├── README.md ├── celib.pro.txt └── src ├── Makefile ├── cel_api.h ├── cel_array.c ├── cel_array.h ├── cel_bitmap.c ├── cel_bitmap.h ├── cel_bloomfilter.c ├── cel_bloomfilter.h ├── cel_bstree.c ├── cel_bstree.h ├── cel_compress.c ├── cel_compress.h ├── cel_crc32.c ├── cel_hash.c ├── cel_hash.h ├── cel_hashmap.c ├── cel_hashmap.h ├── cel_link.c ├── cel_link.h ├── cel_math.c ├── cel_math.h ├── cel_md5.c ├── cel_sort.c ├── cel_sort.h ├── cel_stdio.c ├── cel_stdio.h ├── cel_string.c ├── cel_string.h ├── gdb └── Makefile ├── mcache └── Makefile └── test ├── rle ├── rle.decode.txt ├── rle.encode.txt ├── rle.txt └── tst.rle.c ├── tst-array.c ├── tst-bitmap.c ├── tst-bloomfilter.c ├── tst-crc32.c ├── tst-format.c ├── tst-hashmap.c ├── tst-ihashmap.c ├── tst-intArray.c ├── tst-link.c ├── tst-lzw.c ├── tst-md5.c ├── tst-md5.php ├── tst-mem.c ├── tst-rle.c ├── tst-rlefile.c ├── tst-sort.c ├── tst-strbuff.c ├── tst-strbuff2.c ├── tst-ucode.c └── tst.c /.gitignore: -------------------------------------------------------------------------------- 1 | doc/ 2 | help/ 3 | # Binary Files # 4 | *.out 5 | *.o 6 | *.lib 7 | *.dll 8 | *.so 9 | *.a 10 | # vim swp file # 11 | *.swp 12 | -------------------------------------------------------------------------------- /CHANGES.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lionsoul2014/celib/cb434e308a61485525eaa9f1d94e9f2d4340a740/CHANGES.txt -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | ========================================================================== 204 | The following license applies to the JQuery JavaScript library 205 | -------------------------------------------------------------------------- 206 | Copyright (c) 2010 John Resig, http://jquery.com/ 207 | 208 | Permission is hereby granted, free of charge, to any person obtaining 209 | a copy of this software and associated documentation files (the 210 | "Software"), to deal in the Software without restriction, including 211 | without limitation the rights to use, copy, modify, merge, publish, 212 | distribute, sublicense, and/or sell copies of the Software, and to 213 | permit persons to whom the Software is furnished to do so, subject to 214 | the following conditions: 215 | 216 | The above copyright notice and this permission notice shall be 217 | included in all copies or substantial portions of the Software. 218 | 219 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 220 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 221 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 222 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 223 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 224 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 225 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 226 | 227 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | celib是使用ANSI C开发的一个扩展类库(c extend library),包含了一些常用的数据结构和算法的封装,可以用于应用或者学习。 2 | 3 | 目前已经包含的封装如下: 4 | 5 | (01). 动态数组。 6 | 7 | (02). bitmap。 8 | 9 | (03). 布隆过滤器。 10 | 11 | (03). 二叉树。 12 | 13 | (04). 基本压缩函数(RLE和LZE算法)。 14 | 15 | (05). 常用的Hash函数(BKDR,FNV,DJB,ELF,bobJenkins,Murmur,crc32)。 16 | 17 | (06). 哈希表(hashmap)。 18 | 19 | (07). 双向链表。 20 | 21 | (08). 基本的数学操作函数。 22 | 23 | (09). MD5(字符串md5, 文件md5)。 24 | 25 | (10). 排序(插入,希尔,归并,快速,桶式,Bit桶式)。 26 | 27 | (11). 字符串API(类Java的StringBuffer字符串缓冲, token, 截取, 查找/替换)。 28 | 29 | (12). 一些简单的内存操作函数。 30 | 31 | (13). 小型内存池。 -未完工 32 | 33 | (14). GDB(基于hash的持久化嵌入式kv数据库)。 -未完工 34 | 35 |
36 | 官方首页:http://code.google.com/p/cx-util/downloads/list 37 | -------------------------------------------------------------------------------- /celib.pro.txt: -------------------------------------------------------------------------------- 1 | 1. 全部结构体名称结构类似于:xxx_t, xxx_t *表示对应的指针。 2 | 2. 结构的创建函数统一使用:(需要配对使用) 3 | (1). new_xxx来返回指针, free_xxx来释放对应的分配空间。 4 | (2). xxx_create()来初始化,xxx_destroy来释放空间。 5 | 3. 全部的结构释放函数需要传递结构指针的指针,并且在释放函数中将指针赋值为NULL,防止重复free带来的错误, 6 | 也可以防止通过free掉以后的引用发送内存反问。 7 | 4. 函数返回值,统一:0表示失败,1表示成功。 8 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | #celib Make file 2 | #author chenxin 3 | CC = gcc 4 | #flag for develop 5 | CFLAGS = -g -Wall 6 | #flag for products 7 | FFLAGS = -O2 -Wall -fPIC 8 | #extra library 9 | EXTRA_LIB = 10 | #lib file 11 | SH_FILE = libcelib.so 12 | #install path 13 | LIB_DIR = /usr/lib 14 | INCLUDE_DIR = /usr/include/celib 15 | 16 | SOURCE = cel_array.o cel_bitmap.o cel_bloomfilter.o cel_bstree.o cel_hashmap.o cel_hash.o cel_crc32.o cel_md5.o cel_link.o cel_sort.o cel_stdio.o cel_string.o cel_math.o cel_compress.o 17 | 18 | all: share 19 | 20 | share: $(SOURCE) 21 | $(CC) $(CFLAGS) $(SOURCE) -fPIC -shared $(EXTRA_LIB) -o $(SH_FILE) 22 | 23 | cel_array.o: cel_api.h cel_array.h cel_array.c 24 | $(CC) $(CFLAGS) -c cel_array.c 25 | 26 | cel_bitmap.o: cel_api.h cel_bitmap.h cel_bitmap.c 27 | $(CC) $(CFLAGS) -c cel_bitmap.c 28 | 29 | cel_hash.o: cel_api.h cel_hash.h cel_hash.c 30 | $(CC) $(CFLAGS) -c cel_hash.c 31 | 32 | cel_md5.o: cel_api.h cel_hash.h cel_md5.c 33 | $(CC) $(CFLAGS) -c cel_md5.c 34 | 35 | cel_crc32.o: cel_api.h cel_hash.h cel_hash.c cel_crc32.c 36 | $(CC) $(CFLAGS) -c cel_crc32.c 37 | 38 | cel_bloomfilter.o: cel_api.h cel_hash.c cel_hash.h cel_bloomfilter.h cel_bloomfilter.c 39 | $(CC) $(CFLAGS) -c cel_bloomfilter.c 40 | 41 | cel_bstree.o: cel_api.h cel_bstree.h cel_bstree.c 42 | $(CC) $(CFLAGS) -c cel_bstree.c 43 | 44 | cel_hashmap.o: cel_api.h cel_hash.h cel_hash.c cel_math.h cel_math.c cel_hashmap.h cel_hashmap.c 45 | $(CC) $(CFLAGS) -c cel_hashmap.c 46 | 47 | cel_link.o: cel_api.h cel_link.h cel_link.c 48 | $(CC) $(CFLAGS) -c cel_link.c 49 | 50 | cel_sort.o: cel_api.h cel_stdio.h cel_stdio.c cel_sort.h cel_sort.c 51 | $(CC) $(CFLAGS) -c cel_sort.c 52 | 53 | cel_string.o: cel_api.h cel_string.h cel_string.c 54 | $(CC) $(CFLAGS) -c cel_string.c 55 | 56 | cel_math.o: cel_api.h cel_math.h cel_math.c 57 | $(CC) $(CFLAGS) -c cel_math.c 58 | 59 | cel_stdio.o: cel_api.h cel_stdio.h cel_stdio.c 60 | $(CC) $(CFLAGS) -c cel_stdio.c 61 | 62 | cel_compress.o: cel_api.h cel_compress.h cel_compress.c 63 | $(CC) $(CFLAGS) -c cel_compress.c 64 | 65 | #clean object 66 | .PHONY: clean 67 | clean: 68 | find . -name \*.o | xargs rm -f 69 | find . -name \*.so | xargs rm -f 70 | 71 | 72 | 73 | install: 74 | @if [ -d $(LIB_DIR) ];then\ 75 | cp $(SH_FILE) $(LIB_DIR);\ 76 | sudo chmod a+x $(LIB_DIR)/$(SH_FILE);\ 77 | sudo chmod og-w $(LIB_DIR)/$(SH_FILE);\ 78 | echo "Congratulation, celib install successfully.";\ 79 | else\ 80 | echo "Fail to install celib, $(LIB_DIR) is not exist.";\ 81 | fi 82 | @if [ ! -d $(INCLUDE_DIR) ];then\ 83 | mkdir $(INCLUDE_DIR);\ 84 | fi 85 | @cp *.h $(INCLUDE_DIR);\ 86 | chmod a+r $(INCLUDE_DIR)/*.h;\ 87 | chmod a+x $(INCLUDE_DIR)/*.h; 88 | -------------------------------------------------------------------------------- /src/cel_api.h: -------------------------------------------------------------------------------- 1 | /** 2 | * common api define of celib 3 | * 4 | * @author chenxin 5 | */ 6 | #ifndef _cel_api_h 7 | #define _cel_api_h 8 | 9 | #include 10 | #include 11 | 12 | //version of the celib 13 | #define _CEL_VERSION 1.1 14 | #define cel_version() _CEL_VERSION 15 | 16 | #if ( defined(WIN32) || defined(_WIN32) || defined(WINNT) ) 17 | # define CEL_API extern __declspec(dllexport) 18 | # define CEL_STATIC static 19 | #else 20 | # define CEL_API extern 21 | # define CEL_STATIC static inline 22 | #endif 23 | 24 | 25 | //memory allocation error 26 | #define CEL_ALLOCATE_ERROR(func, bytes) \ 27 | do { \ 28 | printf(": Allocate Error In Function <%s> For %lu Bytes.\n", func, (unsigned long int) bytes); \ 29 | return NULL; \ 30 | } while (0); 31 | 32 | /* 33 | * memory allocation macro define 34 | * change it to the the serial of function you may to 35 | * want to use, like the api of your memory pool 36 | */ 37 | #define cel_calloc( _bytes, _blocks ) calloc( _bytes, _blocks ) 38 | #define cel_malloc( _bytes ) malloc( _bytes ) 39 | #define cel_free( _ptr ) free( _ptr ) 40 | 41 | //short data type 42 | typedef long long int llong_t; 43 | typedef unsigned long long int ullong_t; 44 | 45 | typedef long int long_t; 46 | typedef unsigned long int ulong_t; 47 | 48 | typedef unsigned int uint_t; 49 | typedef unsigned short ushort_t; 50 | typedef unsigned char uchar_t; 51 | typedef char * cstring; 52 | 53 | //generics compare function 54 | typedef int ( * cel_compare_fn_t ) ( void *, void * ); 55 | typedef void ( * cel_release_callback_fn_t ) ( void * ); 56 | 57 | #endif /*end ifndef*/ 58 | -------------------------------------------------------------------------------- /src/cel_array.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel array interface implemented file src/cel_array.c 3 | * 4 | * @author chenxin 5 | */ 6 | #include 7 | #include 8 | #include 9 | #include "cel_array.h" 10 | 11 | //default size of the array 12 | #define _cel_array_default_length 16 13 | 14 | static void ** create_array_blocks( uint_t ); 15 | static void resize_array( cel_array_t *, uint_t ); 16 | 17 | /** 18 | * create a new cel array with a default size. 19 | * 20 | * @return cel_array_t 21 | */ 22 | //CEL_API cel_array_t *new_cel_array() { 23 | // return new_cel_array_opacity( _cel_array_default_length ); 24 | //} 25 | 26 | //create a new cel array with a specified size 27 | CEL_API cel_array_t *new_cel_array_opacity( int opacity ) 28 | { 29 | cel_array_t *ptr = ( cel_array_t * ) cel_malloc( sizeof( cel_array_t ) ); 30 | if ( ptr == NULL ) { 31 | CEL_ALLOCATE_ERROR("new_cel_array_opacity", sizeof(cel_array_t)); 32 | } 33 | 34 | if ( cel_array_init(ptr, opacity) == 0 ) { 35 | cel_free(ptr); 36 | CEL_ALLOCATE_ERROR("cel_array_create", opacity); 37 | } 38 | 39 | return ptr; 40 | } 41 | 42 | //free the specified cel array 43 | CEL_API void free_cel_array( 44 | cel_array_t **array, cel_release_callback_fn_t relfunc ) 45 | { 46 | if ( array == NULL ) return; 47 | if ( *array != NULL ) { 48 | cel_array_destroy(*array, relfunc); 49 | cel_free( *array ); 50 | *array = NULL; 51 | } 52 | } 53 | 54 | /* 55 | * initialize the specified cel_array_t 56 | * 57 | * @param cel_array_t * 58 | * @return 1 for success and 0 for failed 59 | */ 60 | CEL_API int cel_array_init( cel_array_t *array, int opacity ) 61 | { 62 | //check and use default opacity 63 | if ( opacity < 0 ) opacity = _cel_array_default_length; 64 | 65 | array->items = create_array_blocks( opacity ); 66 | if ( array->items == NULL ) return 0; 67 | array->length = opacity; 68 | array->size = 0; 69 | 70 | return 1; 71 | } 72 | 73 | /* 74 | * destroy the specified cel_array_t 75 | * 76 | * @param cel_array_t * 77 | * @return 1 for success and 0 for failed 78 | */ 79 | CEL_API int cel_array_destroy( 80 | cel_array_t *array, 81 | cel_release_callback_fn_t relfunc ) 82 | { 83 | register int i; 84 | 85 | if ( array != NULL ) { 86 | if ( relfunc != NULL ) { 87 | for ( i = 0; i < array->size; i++ ) { 88 | relfunc( array->items[i] ); 89 | } 90 | } 91 | 92 | cel_free( array->items ); 93 | array->items = NULL; 94 | } 95 | 96 | return 1; 97 | } 98 | 99 | /** 100 | * create a specified number of ptr blocks. 101 | * 102 | * @param __blocks 103 | * @return void ** 104 | */ 105 | static void **create_array_blocks( uint_t __blocks ) 106 | { 107 | void ** items = ( void ** ) cel_calloc( sizeof( void * ), __blocks ); 108 | if ( items == NULL ) return NULL; 109 | return items; 110 | } 111 | 112 | 113 | CEL_API void cel_array_add( cel_array_t *array, void * value ) 114 | { 115 | if ( array->size >= array->length ) 116 | resize_array( array, array->size * 2 + 1 ); 117 | array->items[array->size++] = value; 118 | } 119 | 120 | /** 121 | * resize the array to a specified opacity . 122 | * 123 | * @param array 124 | * @param opacity 125 | */ 126 | static void resize_array( cel_array_t *array, uint_t opacity ) 127 | { 128 | void ** nitems = create_array_blocks( opacity ); 129 | 130 | //register uint_t i; 131 | //for ( i = 0; i < array->size; i++ ) 132 | // nitems[i] = array->items[i]; 133 | memcpy(nitems, array->items, array->size * sizeof(void *)); 134 | 135 | //free the old items 136 | cel_free( array->items ); 137 | array->items = nitems; 138 | array->length = opacity; 139 | } 140 | 141 | CEL_API void cel_array_insert( 142 | cel_array_t *array, uint_t idx, void * value ) 143 | { 144 | register uint_t i; 145 | if ( idx < array->size ) { 146 | if ( array->size >= array->length ) { 147 | resize_array( array, array->size * 2 + 1 ); 148 | } 149 | 150 | //copy the items 151 | for ( i = array->size; i > idx; i-- ) { 152 | array->items[i] = array->items[i - 1]; 153 | } 154 | array->items[idx] = value; 155 | array->size++; 156 | } 157 | } 158 | 159 | /** 160 | * deletet the element at a specified position . 161 | * the deleted element will be returned . 162 | * 163 | * @param idx 164 | * @return void * 165 | */ 166 | CEL_API void * cel_array_del( cel_array_t *array, uint_t idx ) 167 | { 168 | register uint_t i; 169 | void * old; 170 | if ( idx >= array->size ) return NULL; 171 | 172 | //bak the element to deleted 173 | old = array->items[idx]; 174 | for ( i = idx; i < array->size; i++ ) { 175 | array->items[i] = array->items[i+1]; 176 | } 177 | array->size--; //minus the size 178 | 179 | return old; 180 | } 181 | 182 | /** 183 | * search the object in the array and delete it . 184 | * 185 | * @param obj 186 | * @param cfunc 187 | * @return void * 188 | */ 189 | CEL_API void * cel_array_del_obj( 190 | cel_array_t *array, 191 | void * obj, 192 | cel_compare_fn_t cfunc ) 193 | { 194 | int i = cel_array_get_obj( array, obj, cfunc ); 195 | if ( i == -1 ) return NULL; 196 | return cel_array_del( array, i ); 197 | } 198 | 199 | 200 | CEL_API void * cel_array_get( cel_array_t *array, uint_t idx ) 201 | { 202 | if ( idx >= array->size ) return NULL; 203 | return array->items[idx]; 204 | } 205 | 206 | /** 207 | * search the object in the array . 208 | * 209 | * @param obj 210 | * @param cfunc 211 | * @return int 212 | */ 213 | CEL_API int cel_array_get_obj( 214 | cel_array_t *array, 215 | void * obj, 216 | cel_compare_fn_t cfunc ) 217 | { 218 | register uint_t i; 219 | for ( i = 0; i < array->size; i++ ) { 220 | if ( obj == array->items[i] 221 | || cfunc( array->items[i], obj ) == 0 ) { 222 | return i; 223 | } 224 | } 225 | return -1; 226 | } 227 | 228 | /** 229 | * replace the element at a specified position 230 | * with a new value, and the old element will be return . 231 | * 232 | * @param idx 233 | * @param value 234 | * @reutrn void * 235 | */ 236 | CEL_API void * cel_array_set( 237 | cel_array_t *array, uint_t idx, void * value ) 238 | { 239 | void * old; 240 | if ( idx >= array->size ) return NULL; 241 | //bak the old element 242 | old = array->items[idx]; 243 | array->items[idx] = value; 244 | 245 | return old; 246 | } 247 | 248 | 249 | CEL_API void * cel_array_set_obj( 250 | cel_array_t *array, 251 | void * obj, 252 | void * value, 253 | cel_compare_fn_t cfunc ) 254 | { 255 | int i = cel_array_get_obj( array, obj, cfunc ); 256 | if ( i == -1 ) return NULL; 257 | return cel_array_set( array, i, value ); 258 | } 259 | 260 | //instead this will macro define 261 | //CEL_API uint_t cel_array_size( cel_array_t *array ) { 262 | // return array->size; 263 | //} 264 | 265 | 266 | 267 | 268 | 269 | //int array block 270 | //@date 2013-09-28 271 | /** 272 | * create a new cel int array with a default size. */ 273 | //CEL_API cel_intArray_t *new_cel_intArray() 274 | //{ 275 | // return new_cel_intArray_opacity( _cel_array_default_length ); 276 | //} 277 | 278 | #define create_int_blocks( opacity )\ 279 | ( int * ) cel_calloc( sizeof(int), opacity ) 280 | 281 | static void resize_intArray( cel_intArray_t *array, uint_t opacity ) 282 | { 283 | int * nitems = create_int_blocks( opacity ); 284 | 285 | //register uint_t i; 286 | //for ( i = 0; i < array->size; i++ ) 287 | // nitems[i] = array->items[i]; 288 | memcpy( nitems, array->items, array->size * sizeof(int *) ); 289 | 290 | //free the old items 291 | cel_free( array->items ); 292 | array->items = nitems; 293 | array->length = opacity; 294 | } 295 | 296 | /** 297 | * create a new cel int array with a specified size. 298 | * 299 | * @param uint_t 300 | * @return cel_array_t 301 | */ 302 | CEL_API cel_intArray_t *new_cel_intArray_opacity( uint_t opacity ) 303 | { 304 | cel_intArray_t *ptr = ( cel_intArray_t * ) 305 | cel_malloc( sizeof( cel_intArray_t ) ); 306 | if ( ptr == NULL ) { 307 | CEL_ALLOCATE_ERROR("new_cel_intArray_opacity", sizeof(cel_intArray_t)); 308 | } 309 | 310 | if ( cel_intArray_init(ptr, opacity) == 0 ) { 311 | cel_free(ptr); 312 | CEL_ALLOCATE_ERROR("cel_intArray_create", opacity); 313 | } 314 | 315 | return ptr; 316 | } 317 | 318 | //free the specified cel array 319 | CEL_API void free_cel_intArray( cel_intArray_t **arr ) 320 | { 321 | cel_intArray_t *array; 322 | 323 | if ( arr == NULL ) return; 324 | if ( *arr != NULL ) { 325 | array = *arr; 326 | cel_intArray_destroy(array); 327 | cel_free( array ); 328 | } 329 | 330 | arr = NULL; 331 | } 332 | 333 | /* 334 | * initialize the specified cel_array_t 335 | * 336 | * @param cel_intArray_t * 337 | * @param uint_t initialize opacity of the array 338 | * @return 1 for success and 0 for failed 339 | */ 340 | CEL_API int cel_intArray_init( cel_intArray_t *array, uint_t opacity ) 341 | { 342 | array->items = create_int_blocks( opacity ); 343 | if ( array->items == NULL ) return 0; 344 | array->length = opacity; 345 | array->size = 0; 346 | 347 | return 1; 348 | } 349 | 350 | /* 351 | * destroy the specified cel_array_t 352 | * 353 | * @param cel_intArray_t * 354 | * @return 1 for success and 0 for failed 355 | */ 356 | CEL_API int cel_intArray_destroy( cel_intArray_t *array ) 357 | { 358 | if ( array != NULL ) { 359 | cel_free( array->items ); 360 | } 361 | 362 | return 1; 363 | } 364 | 365 | 366 | //add a new element to the array 367 | CEL_API int cel_intArray_add( cel_intArray_t *array, int val ) 368 | { 369 | if ( array->size >= array->length ) { 370 | resize_intArray( array, array->size * 2 + 1 ); 371 | } 372 | array->items[array->size++] = val; 373 | return 1; 374 | } 375 | 376 | //insert a specified element to at a specified position 377 | CEL_API int cel_intArray_insert( 378 | cel_intArray_t *array, uint_t idx, int val ) 379 | { 380 | register uint_t i; 381 | if ( idx >= array->size ) return 0; 382 | 383 | if ( array->size >= array->length ) { 384 | resize_intArray( array, array->size * 2 + 1 ); 385 | } 386 | 387 | //copy the items 388 | for ( i = array->size; i > idx; i-- ) { 389 | array->items[i] = array->items[i - 1]; 390 | } 391 | array->items[idx] = val; 392 | array->size++; 393 | 394 | return 1; 395 | } 396 | 397 | /** 398 | * delete the element at a specified position . 399 | * the deleted element will be returned . 400 | * 401 | * @param idx 402 | * @return int 0 for fail and 1 for bingo. 403 | */ 404 | CEL_API int cel_intArray_del( cel_intArray_t *array, uint_t idx ) 405 | { 406 | register uint_t i; 407 | if ( idx >= array->size ) return 0; 408 | for ( i = idx; i < array->size; i++ ) { 409 | array->items[i] = array->items[i+1]; 410 | } 411 | 412 | array->size--; //minus the size 413 | 414 | return 1; 415 | } 416 | 417 | /** 418 | * search the object in the array and delete it . 419 | * 420 | * @param obj 421 | * @param cfunc 422 | * @return int 0 for fail and 1 for true. 423 | */ 424 | CEL_API int cel_intArray_del_obj( 425 | cel_intArray_t *array, int val ) 426 | { 427 | int i = cel_intArray_get_obj( array, val ); 428 | if ( i == -1 ) return 0; 429 | return cel_intArray_del( array, i ); 430 | } 431 | 432 | /** 433 | * get the element at a specified postion. 434 | * 435 | * @param uint_t 436 | * @return void * 437 | */ 438 | //CEL_API int cel_intArray_get( cel_intArray_t *array, uint_t idx ) 439 | //{ 440 | // return array->items[idx]; 441 | //} 442 | 443 | /** 444 | * search the object in the array . 445 | * 446 | * @param obj 447 | * @return int - the index of the search object. 448 | */ 449 | CEL_API int cel_intArray_get_obj( cel_intArray_t *array, int val ) 450 | { 451 | register uint_t i; 452 | for ( i = 0; i < array->size; i++ ) { 453 | if ( array->items[i] == val ) 454 | return i; 455 | } 456 | return 0; 457 | } 458 | 459 | /** 460 | * set the element at the specified position, 461 | * and the old element will be returned . 462 | * 463 | * @param uint_t 464 | * @param void * 465 | * @return void * ptr of the old element 466 | */ 467 | //CEL_API int cel_intArray_set( 468 | // cel_intArray_t *array, uint_t idx, int val ) 469 | //{ 470 | // array->items[idx] = val; 471 | //} 472 | -------------------------------------------------------------------------------- /src/cel_array.h: -------------------------------------------------------------------------------- 1 | /** 2 | * cel array interface header file src/cel_array.h 3 | * 4 | * @author chenxin 5 | */ 6 | #ifndef _cel_array_h 7 | #define _cel_array_h 8 | 9 | #include 10 | #include 11 | #include "cel_api.h" 12 | 13 | //default size of the array 14 | #define _cel_array_default_length 16 15 | 16 | typedef struct { 17 | uint_t size; //size of the array 18 | uint_t length; //the length of the arrary 19 | void **items; //array pointer 20 | } cel_array_t; 21 | 22 | /** 23 | * create a new cel array with a default size. */ 24 | //CEL_API cel_array_t *new_cel_array(); 25 | #define new_cel_array()\ 26 | new_cel_array_opacity(_cel_array_default_length) 27 | 28 | /** 29 | * create a new cel array with a specified size. 30 | * 31 | * @param uint_t 32 | * @return cel_array_t * 33 | */ 34 | CEL_API cel_array_t *new_cel_array_opacity( int ); 35 | 36 | //free the specified cel array 37 | CEL_API void free_cel_array( cel_array_t **, cel_release_callback_fn_t ); 38 | 39 | /* 40 | * initialize the specified cel_array_t 41 | * 42 | * @param cel_array_t * 43 | * @param uint_t the opacity of the array - -1 to use the default opacity 44 | * @return 1 for success and 0 for failed 45 | */ 46 | CEL_API int cel_array_init( cel_array_t *, int ); 47 | 48 | /* 49 | * destroy the specified cel_array_t 50 | * 51 | * @param cel_array_t * 52 | * @return 1 for success and 0 for failed 53 | */ 54 | CEL_API int cel_array_destroy( cel_array_t *, cel_release_callback_fn_t ); 55 | 56 | //add a new element to the array 57 | CEL_API void cel_array_add( cel_array_t *, void * ); 58 | 59 | //insert a specified element to at a specified position 60 | CEL_API void cel_array_insert( cel_array_t *, uint_t, void * ); 61 | 62 | /** 63 | * deletet the element at a specified position . 64 | * the deleted element will be returned . 65 | * 66 | * @param idx 67 | * @return void * 68 | */ 69 | CEL_API void *cel_array_del( cel_array_t *, uint_t ); 70 | 71 | /** 72 | * search the object in the array and delete it . 73 | * 74 | * @param obj 75 | * @param cfunc 76 | * @return void * 77 | */ 78 | CEL_API void *cel_array_del_obj( cel_array_t *, void *, cel_compare_fn_t ); 79 | 80 | /** 81 | * get the element at a specified postion. 82 | * 83 | * @param uint_t 84 | * @return void * 85 | */ 86 | CEL_API void *cel_array_get( cel_array_t *, uint_t ); 87 | 88 | /** 89 | * search the object in the array . 90 | * 91 | * @param obj 92 | * @param cfunc 93 | * @return int 94 | */ 95 | CEL_API int cel_array_get_obj( cel_array_t *, void *, cel_compare_fn_t ); 96 | 97 | /** 98 | * set the element at the specified position, 99 | * and the old element will be returned . 100 | * 101 | * @param uint_t 102 | * @param void * 103 | * @return void * ptr of the old element 104 | */ 105 | CEL_API void *cel_array_set( cel_array_t *, uint_t, void * ); 106 | 107 | //replace the old value with the new value 108 | CEL_API void *cel_array_set_obj( cel_array_t *, void *, void *, cel_compare_fn_t ); 109 | 110 | //size get macro 111 | #define cel_array_size( array ) array->size 112 | 113 | //count the size of the specified cel array 114 | //CEL_API uint_t cel_array_size( cel_array_t * ); 115 | // 116 | 117 | 118 | 119 | 120 | //// 121 | //int array list block. 122 | //@date 2013-09-27 123 | typedef struct { 124 | uint_t size; //size of the array 125 | uint_t length; //the length of the arrary 126 | int *items; //int array pointer 127 | } cel_intArray_t; 128 | 129 | /** 130 | * create a new cel int array with a default size. */ 131 | //CEL_API cel_intArray_t *new_cel_intArray(); 132 | #define new_cel_intArray()\ 133 | new_cel_intArray_opacity(_cel_array_default_length) 134 | 135 | /** 136 | * create a new cel int array with a specified size. 137 | * 138 | * @param uint_t 139 | * @return cel_array_t 140 | */ 141 | CEL_API cel_intArray_t *new_cel_intArray_opacity( uint_t ); 142 | 143 | //free the specified cel array 144 | CEL_API void free_cel_intArray( cel_intArray_t ** ); 145 | 146 | /* 147 | * initialize the specified cel_array_t 148 | * 149 | * @param cel_intArray_t * 150 | * @param uint_t initialize opacity of the array 151 | * @return 1 for success and 0 for failed 152 | */ 153 | CEL_API int cel_intArray_init( cel_intArray_t *, uint_t ); 154 | 155 | /* 156 | * destroy the specified cel_array_t 157 | * 158 | * @param cel_intArray_t * 159 | * @return 1 for success and 0 for failed 160 | */ 161 | CEL_API int cel_intArray_destroy( cel_intArray_t * ); 162 | 163 | //add a new element to the array 164 | CEL_API int cel_intArray_add( cel_intArray_t *, int ); 165 | 166 | //insert a specified element to at a specified position 167 | CEL_API int cel_intArray_insert( cel_intArray_t *, uint_t, int ); 168 | 169 | /** 170 | * delete the element at a specified position . 171 | * the deleted element will be returned . 172 | * 173 | * @param idx 174 | * @return int 0 for fail and 1 for true. 175 | */ 176 | CEL_API int cel_intArray_del( cel_intArray_t *, uint_t ); 177 | 178 | /** 179 | * search the object in the array and delete it . 180 | * 181 | * @param obj 182 | * @param cfunc 183 | * @return int 0 for fail and 1 for true. 184 | */ 185 | CEL_API int cel_intArray_del_obj( cel_intArray_t *, int ); 186 | 187 | /** 188 | * get the element at a specified postion. 189 | * 190 | * @param uint_t 191 | * @return int - item value. 192 | */ 193 | //CEL_API int cel_intArray_get( cel_intArray_t *, uint_t ); 194 | #define cel_intArray_get( array, idx ) array->items[idx] 195 | 196 | /** 197 | * search the object in the array . 198 | * 199 | * @param obj 200 | * @return int - the index of the searching object. 201 | */ 202 | CEL_API int cel_intArray_get_obj( cel_intArray_t *, int ); 203 | 204 | /** 205 | * set the element at the specified position, 206 | * and the old element will be returned . 207 | * 208 | * @param uint_t 209 | * @param idx 210 | */ 211 | //CEL_API int cel_intArray_set( cel_intArray_t, uint_t, int ); 212 | #define cel_intArray_set( array, idx, val )\ 213 | array->items[idx] = val 214 | 215 | //size get macro 216 | #define cel_intArray_size( array ) array->size 217 | 218 | #endif /*end ifndef*/ 219 | -------------------------------------------------------------------------------- /src/cel_bitmap.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel bitmap interface implemented source file src/cel_bitmap.c 3 | * 4 | * @author chenxin 5 | */ 6 | #include "cel_bitmap.h" 7 | #include 8 | #include 9 | 10 | /** 11 | * create a new bitmap with a specified opacity. 12 | * 13 | * @param opacity 14 | * @return cel_bitmap_t * 15 | */ 16 | CEL_API cel_bitmap_t *new_cel_bitmap( int opacity ) 17 | { 18 | cel_bitmap_t *map = ( cel_bitmap_t * ) cel_malloc( sizeof( cel_bitmap_t ) ); 19 | if ( map == NULL ) { 20 | CEL_ALLOCATE_ERROR("new_cel_bitmap", sizeof(cel_bitmap_t)); 21 | } 22 | 23 | if ( cel_bitmap_init(map, opacity) == 0 ) { 24 | cel_free(map); 25 | CEL_ALLOCATE_ERROR("cel_bitmap_create", opacity); 26 | } 27 | 28 | return map; 29 | } 30 | 31 | //free the specified cel_bitmap_t 32 | CEL_API void free_cel_bitmap( cel_bitmap_t **map ) 33 | { 34 | if ( map == NULL ) return; 35 | if ( *map != NULL ) { 36 | cel_bitmap_destroy(*map); 37 | cel_free(*map); 38 | *map = NULL; 39 | } 40 | } 41 | 42 | /* 43 | * initialize the specified cel_bitmap_t 44 | * 45 | * @param uint_t opacity of the bitmap 46 | * @return int (1 for success and 0 for failed) 47 | */ 48 | CEL_API int cel_bitmap_init( cel_bitmap_t *map, int opacity ) 49 | { 50 | uint_t bytes = (opacity + CHAR_BIT - 1) / CHAR_BIT; 51 | map->bit = (char *) cel_calloc( sizeof(char), bytes ); 52 | if ( map->bit == NULL ) return 0; 53 | 54 | map->length = bytes * CHAR_BIT; 55 | map->size = 0; 56 | //reset the memeory to 0x00 57 | memset( map->bit, 0x00, bytes ); 58 | 59 | return 1; 60 | } 61 | 62 | /* 63 | * destroy the specified cel_bitmap_t 64 | * 65 | * @param cel_array_t 66 | * @return int 1 for success and 0 for failed 67 | */ 68 | CEL_API int cel_bitmap_destroy( cel_bitmap_t *map ) 69 | { 70 | if ( map != NULL ) { 71 | cel_free(map->bit); 72 | map->bit = NULL; 73 | } 74 | 75 | return 1; 76 | } 77 | 78 | /** 79 | * set the specified bit to 1. 80 | * 81 | * @param idx 82 | */ 83 | CEL_API void cel_bitmap_set( cel_bitmap_t *map, uint_t idx ) 84 | { 85 | if ( map != NULL && idx < map->length ) { 86 | map->bit[idx/CHAR_BIT] |= (0x01<<(idx%CHAR_BIT)); 87 | map->size++; 88 | } 89 | } 90 | 91 | //set the specified bit to 0 92 | CEL_API void cel_bitmap_remove( cel_bitmap_t *map, uint_t idx ) 93 | { 94 | if ( map != NULL && idx < map->length ) { 95 | map->bit[idx/CHAR_BIT] &= (~(0x01<<(idx%CHAR_BIT))); 96 | map->size--; 97 | } 98 | } 99 | 100 | /** 101 | * get the specified bit. 102 | * 103 | * @param idx 104 | * @return 1 or 0 105 | */ 106 | CEL_API int cel_bitmap_get( cel_bitmap_t *map, uint_t idx ) 107 | { 108 | if ( map == NULL ) return 0; 109 | if ( idx >= map->length ) return 0; 110 | if ( ( map->bit[idx/CHAR_BIT] & (0x01<<(idx%CHAR_BIT)) ) != 0 ) { 111 | return 1; 112 | } 113 | return 0; 114 | } 115 | -------------------------------------------------------------------------------- /src/cel_bitmap.h: -------------------------------------------------------------------------------- 1 | /** 2 | * cel bitmap interface header source file. 3 | * package: src/cel_bitmap.h 4 | * 5 | * @author chenxin 6 | * @date 2013-08-07 7 | */ 8 | #ifndef _cel_bitmap_h 9 | #define _cel_bitmap_h 10 | 11 | #include "cel_api.h" 12 | 13 | typedef struct { 14 | uint_t length; 15 | uint_t size; 16 | char * bit; 17 | } cel_bitmap_t; 18 | 19 | #define cel_bitmap_length( map ) map->length 20 | #define cel_bitmap_size( map ) map->size 21 | 22 | /** 23 | * create a new bitmap with a specified opacity. 24 | * 25 | * @param uint_t 26 | * @return cel_bitmap_t * 27 | */ 28 | CEL_API cel_bitmap_t *new_cel_bitmap( int ); 29 | 30 | //free the specified cel_bitmap_t 31 | CEL_API void free_cel_bitmap( cel_bitmap_t ** ); 32 | 33 | /* 34 | * initialize the specified cel_bitmap_t 35 | * 36 | * @param uint_t opacity of the bitmap 37 | * @return int (1 for success and 0 for failed) 38 | */ 39 | CEL_API int cel_bitmap_init( cel_bitmap_t *, int ); 40 | 41 | /* 42 | * destroy the specified cel_bitmap_t 43 | * 44 | * @param cel_array_t 45 | * @return int 1 for success and 0 for failed 46 | */ 47 | CEL_API int cel_bitmap_destroy( cel_bitmap_t * ); 48 | 49 | /** 50 | * set the specified bit to 1. 51 | * 52 | * @param uint_t 53 | */ 54 | CEL_API void cel_bitmap_set( cel_bitmap_t *, uint_t ); 55 | 56 | /** 57 | * set the specified bit to 0 58 | * 59 | * @param uint_t 60 | */ 61 | CEL_API void cel_bitmap_remove( cel_bitmap_t *, uint_t ); 62 | 63 | /** 64 | * get the specified bit. 65 | * 66 | * @param uint_t 67 | * @return 1 or 0 68 | */ 69 | CEL_API int cel_bitmap_get( cel_bitmap_t *, uint_t ); 70 | 71 | #endif 72 | -------------------------------------------------------------------------------- /src/cel_bloomfilter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel bloom filter implemented functions source file 3 | * @package src/cel_bloomfilter.c 4 | * 5 | * @author chenxin 6 | */ 7 | #include "cel_stdio.h" 8 | #include "cel_hash.h" 9 | #include "cel_bloomfilter.h" 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | /** 16 | * create a new bloom filter with a specified length 17 | * 18 | * @param length 19 | * @param hfuncs 20 | * @return cel_bloomfilter_t 21 | */ 22 | CEL_API cel_bloomfilter_t *new_cel_bloomfilter( int length, int hfuncs ) 23 | { 24 | cel_bloomfilter_t *bloom = ( cel_bloomfilter_t * ) \ 25 | cel_malloc( sizeof( cel_bloomfilter_t ) ); 26 | if ( bloom == NULL ) { 27 | CEL_ALLOCATE_ERROR("new_cel_bloomfilter", sizeof(cel_bloomfilter_t)); 28 | } 29 | 30 | if ( cel_bloomfilter_init(bloom, length, hfuncs) == 0 ) { 31 | cel_free(bloom); 32 | CEL_ALLOCATE_ERROR("cel_bloomfilter_create", length / CHAR_BIT); 33 | } 34 | 35 | return bloom; 36 | } 37 | 38 | //free the specified bloom filter 39 | CEL_API void free_cel_bloomfilter( cel_bloomfilter_t **bloom ) 40 | { 41 | if ( bloom == NULL ) return; 42 | if ( *bloom != NULL ) { 43 | cel_bloomfilter_destroy(*bloom); 44 | cel_free(*bloom); 45 | *bloom = NULL; 46 | } 47 | } 48 | 49 | /** 50 | * create a default bloom filter with a specified length 51 | * also, this will load the hash functions from cel 52 | * defined in header file src/cel_hash.h, and 8 hash functions is loaded for default 53 | * 54 | * math: 55 | * k = ln2 * m/n 56 | * statistics 57 | * m/n false positive rate 58 | * 9 0.0145 59 | * 10 0.00846 60 | * 11 0.00509 61 | * 12 0.00314 62 | * . 63 | * . 64 | * . 65 | * 30 9.01e-06 66 | * 31 7.16e-06 67 | * 32 5.73e-06 68 | * 69 | * @param length 70 | * @return cel_bloomfilter_t 71 | */ 72 | CEL_API int cel_bloomfilter_init( cel_bloomfilter_t *bloom, int length, int hfuncs ) 73 | { 74 | int __bytes; 75 | 76 | //make the space for the str 77 | __bytes = ( length + CHAR_BIT - 1 ) / CHAR_BIT; 78 | if ( (bloom->str = (char *) cel_calloc(__bytes, sizeof(char))) == NULL ) { 79 | return 0; 80 | } 81 | 82 | //hash functions buckets allocations 83 | if ( (bloom->hfuncs = (cel_hash_fn_t *) \ 84 | cel_calloc(hfuncs, sizeof(cel_hash_fn_t)) ) == NULL ) { 85 | cel_free(bloom->str); 86 | return 0; 87 | } 88 | 89 | //initialize 90 | bloom->length = length; 91 | bloom->size = 0; 92 | bloom->hlength = hfuncs; 93 | bloom->hsize = 0; 94 | memset(bloom->str, 0x00, __bytes); 95 | 96 | //Load the hash functions 97 | //most of the hash functions is simple and fast, but efficient 98 | 99 | cel_bloomfilter_add_func(bloom, cel_bkdr_hash); 100 | cel_bloomfilter_add_func(bloom, cel_fnv1a_hash); 101 | cel_bloomfilter_add_func(bloom, cel_ap_hash); 102 | cel_bloomfilter_add_func(bloom, cel_djp_hash); 103 | cel_bloomfilter_add_func(bloom, cel_js_hash); 104 | cel_bloomfilter_add_func(bloom, cel_sdms_hash); 105 | cel_bloomfilter_add_func(bloom, cel_rs_hash); 106 | cel_bloomfilter_add_func(bloom, cel_dek_hash); 107 | 108 | return 1; 109 | } 110 | 111 | /* 112 | * destroy the specified bloomfilter 113 | * 114 | * @param cel_bloomfilter_t * 115 | * @return int 1 for success and 0 for failed 116 | */ 117 | CEL_API int cel_bloomfilter_destroy( cel_bloomfilter_t *bloom ) 118 | { 119 | if ( bloom != NULL ) { 120 | cel_free(bloom->hfuncs); 121 | bloom->hfuncs = NULL; 122 | 123 | cel_free(bloom->str); 124 | bloom->str = NULL; 125 | } 126 | 127 | return 1; 128 | } 129 | 130 | /** 131 | * add a new hash function for the bloom filter 132 | * 133 | * @param bloom 134 | * @param func 135 | * @return int 136 | */ 137 | CEL_API int cel_bloomfilter_add_func( 138 | cel_bloomfilter_t *bloom, 139 | cel_hash_fn_t func ) 140 | { 141 | if ( bloom == NULL ) return 0; 142 | if ( bloom->hsize < bloom->hlength ) { 143 | bloom->hfuncs[bloom->hsize++] = func; 144 | return 1; 145 | } 146 | 147 | return 0; 148 | } 149 | 150 | /** 151 | * add a new string to the specified bloom filter 152 | * 153 | * @param bloom 154 | * @param str 155 | * @return int 156 | */ 157 | CEL_API int cel_bloomfilter_add( cel_bloomfilter_t *bloom, char *str ) 158 | { 159 | register uint_t i; 160 | 161 | if ( bloom == NULL || bloom->hsize == 0 ) { 162 | return 0; 163 | } 164 | 165 | for ( i = 0; i < bloom->hsize; i++ ) { 166 | CEL_BIT_OPEN(bloom->str, bloom->hfuncs[i](str) % bloom->length); 167 | } 168 | bloom->size++; 169 | 170 | return 1; 171 | } 172 | 173 | /** 174 | * check the specified string is in the specified bloom filter. 175 | * 176 | * @param bloom 177 | * @param str 178 | * @return int (1 for true and 0 for false) 179 | */ 180 | CEL_API int cel_bloomfilter_exists( cel_bloomfilter_t *bloom, char *str ) 181 | { 182 | register uint_t i; 183 | 184 | if ( bloom == NULL || bloom->hsize == 0 ) { 185 | return 0; 186 | } 187 | 188 | for ( i = 0; i < bloom->hsize; i++ ) { 189 | if ( CEL_BIT_FETCH(bloom->str, bloom->hfuncs[i](str) % bloom->length) == 0 ) { 190 | return 0; 191 | } 192 | } 193 | 194 | return 1; 195 | } 196 | -------------------------------------------------------------------------------- /src/cel_bloomfilter.h: -------------------------------------------------------------------------------- 1 | /** 2 | * cel bloom filter interface header file src/bloomfilter.h 3 | * 4 | * @author chenxin 5 | */ 6 | #ifndef _cel_bloomfilter_h 7 | #define _cel_bloomfilter_h 8 | 9 | #include "cel_api.h" 10 | #include 11 | #include 12 | 13 | typedef uint32_t ( * cel_hash_fn_t ) ( const char * ); 14 | 15 | typedef struct { 16 | uint_t length; 17 | uint_t size; 18 | char *str; 19 | //about the hash functions 20 | cel_hash_fn_t *hfuncs; 21 | uint_t hlength; 22 | uint_t hsize; 23 | } cel_bloomfilter_t; 24 | 25 | //quick macro define 26 | #define cel_bloomfilter_length( bloom ) bloom->length 27 | #define cel_bloomfilter_size( bloom ) bloom->size 28 | #define cel_bloomfilter_hlength( bloom ) bloom->hlength 29 | #define cel_bloomfilter_hsize( bloom ) bloom->hsize 30 | 31 | /** 32 | * create a new bloom filter with a specified length. 33 | * 34 | * @param length 35 | * @param hfuncs 36 | * @return cel_bloomfilter_t * 37 | */ 38 | CEL_API cel_bloomfilter_t *new_cel_bloomfilter( int, int ); 39 | 40 | //free the specified bloom filter. 41 | CEL_API void free_cel_bloomfilter( cel_bloomfilter_t ** ); 42 | 43 | /** 44 | * create a default bloom filter with a specified length. 45 | * also, this will load the hash functions from cel, 46 | * defined in header file src/cel_hash.h, and 8 hash functions is loaded for default. 47 | * 48 | * math: 49 | * k = ln2 * m/n 50 | * statistics 51 | * m/n false positive rate 52 | * 9 0.0145 53 | * 10 0.00846 54 | * 11 0.00509 55 | * 12 0.00314 56 | * . 57 | * . 58 | * . 59 | * 30 9.01e-06 60 | * 31 7.16e-06 61 | * 32 5.73e-06 62 | * 63 | * @param length 64 | * @return cel_bloomfilter_t * 65 | */ 66 | CEL_API int cel_bloomfilter_init( cel_bloomfilter_t *, int, int ); 67 | 68 | /* 69 | * destroy the specified bloomfilter 70 | * 71 | * @param cel_bloomfilter_t * 72 | * @return int 1 for success and 0 for failed 73 | */ 74 | CEL_API int cel_bloomfilter_destroy( cel_bloomfilter_t * ); 75 | 76 | /** 77 | * add a new hash function for the bloom filter. 78 | * 79 | * @param cel_bloomfilter_t * 80 | * @param cel_hash_fn_t 81 | * @return int 82 | */ 83 | CEL_API int cel_bloomfilter_add_func( cel_bloomfilter_t *, cel_hash_fn_t ); 84 | 85 | /** 86 | * add a new string to the specified bloom filter. 87 | * 88 | * @param cel_bloomfilter_t * 89 | * @param char * 90 | * @return int 91 | */ 92 | CEL_API int cel_bloomfilter_add( cel_bloomfilter_t *, char * ); 93 | 94 | /** 95 | * check the specified string is in the specified bloom filter. 96 | * 97 | * @param cel_bloomfilter_t * 98 | * @param char * 99 | * @return int (1 for true and 0 for false) 100 | */ 101 | CEL_API int cel_bloomfilter_exists( cel_bloomfilter_t *, char * ); 102 | 103 | #endif /*end ifndef*/ 104 | 105 | -------------------------------------------------------------------------------- /src/cel_bstree.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lionsoul2014/celib/cb434e308a61485525eaa9f1d94e9f2d4340a740/src/cel_bstree.c -------------------------------------------------------------------------------- /src/cel_bstree.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lionsoul2014/celib/cb434e308a61485525eaa9f1d94e9f2d4340a740/src/cel_bstree.h -------------------------------------------------------------------------------- /src/cel_compress.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel data comnpress/decompress module 3 | * interface implemented source file. 4 | * @package src/cel_compress.c 5 | * 6 | * @author chenxin 7 | * @date 2013-09-20 8 | */ 9 | #include "cel_compress.h" 10 | #include "cel_hashmap.h" 11 | #include "cel_string.h" 12 | #include "cel_array.h" 13 | #include 14 | #include 15 | #include 16 | 17 | 18 | /* {{{ rle - run length encoding. 19 | * */ 20 | #define RLE_BUCKET_MAX_LENGTH 127 21 | 22 | /* find a char repeat for more than 2 times 23 | * from a specifie string buffer. 24 | * 25 | * @param inbuff 26 | * @param inlen 27 | * @param start 28 | * @param len 29 | * @return idx or -1 (when match none) 30 | * */ 31 | static int next_least3_repeat( char *inbuff, uint_t inlen, uint_t start, uint_t *len ) 32 | { 33 | register uint_t i, j; 34 | register int p; 35 | register char val; 36 | 37 | for ( i = start; i < inlen; ) { 38 | val = inbuff[i]; 39 | p = 0; 40 | for ( j = i; j < inlen && p < RLE_BUCKET_MAX_LENGTH 41 | && inbuff[j] == val; j++, p++ ); 42 | 43 | //find the char repeat for more than 2 times. 44 | if ( p > 2 ) { 45 | *len = p; 46 | return i; 47 | } 48 | 49 | //plus the offset. 50 | i += (p > 1) ? p : 1; 51 | } 52 | 53 | return -1; 54 | } 55 | 56 | /* compress the specified string with rle algorithm. 57 | * 58 | * @param inbuff - the string to encode. 59 | * @param inlen - length of the string. 60 | * @param outbuff 61 | * @param outlen 62 | * @return 1 for encode success and 0 for fail. 63 | * */ 64 | CEL_API int cel_rle_encode_string( char *inbuff, uint_t inlen, cel_strbuff_t *sb ) 65 | { 66 | register uint_t i, p, last = 0; 67 | uint_t len = 0; 68 | 69 | //no-repeat bucket 70 | register uint_t buckets, t; 71 | while (last < inlen && (p = next_least3_repeat(inbuff, inlen, last, &len)) != -1) { 72 | //Handle the no-repeat chars. 73 | if ( p > last ) { 74 | buckets = (p - last) / 127; 75 | 76 | //Handle the buckets. 77 | t = last; 78 | for ( i = 0; i < buckets; i++ ) { 79 | cel_strbuff_append_char(sb, 127, 1); 80 | cel_strbuff_append_from(sb, &inbuff[t], 127, 1); 81 | t += 127; 82 | } 83 | 84 | //Handle the left few chars(less than 127). 85 | if ( t < p ) { 86 | cel_strbuff_append_char(sb, ((p-t)&0x7F), 1); 87 | cel_strbuff_append_from(sb, &inbuff[t], p-t, 1); 88 | } 89 | } 90 | 91 | //Handle the repeat chars. 92 | cel_strbuff_append_char(sb, (len|0x80), 1); 93 | cel_strbuff_append_char(sb, inbuff[p], 1); 94 | 95 | //change the last offset. 96 | last = p + len; 97 | } 98 | 99 | //Handle the last left chars as no-repeat bucket. 100 | if ( last < inlen ) { 101 | buckets = (inlen - last) / 127; 102 | 103 | //Handle the buckets. 104 | t = last; 105 | for ( i = 0; i < buckets; i++ ) { 106 | cel_strbuff_append_char(sb, 127, 1); 107 | cel_strbuff_append_from(sb, &inbuff[t], 127, 1); 108 | t += 127; 109 | } 110 | 111 | //Handle the left few chars. 112 | if ( t < inlen ) { 113 | cel_strbuff_append_char(sb, ((inlen-t)&0x7F), 1); 114 | cel_strbuff_append_from(sb, &inbuff[t], inlen-t, 1); 115 | } 116 | } 117 | 118 | return 1; 119 | } 120 | 121 | /* decompress the specified string 122 | * compress with RLE compress algorithm. 123 | * 124 | * @return 1 for decode success or 0. 125 | * @see cel_rle_enckde_string 126 | * */ 127 | CEL_API int cel_rle_decode_string( char *inbuff, uint_t inlen, cel_strbuff_t *sb ) 128 | { 129 | register uint_t i, len; 130 | register char val; 131 | 132 | for ( i = 0; i < inlen; ) { 133 | val = inbuff[i]; 134 | //Count the length 135 | len = (val & 0x7F); 136 | if ( len > 127 ) return 0; 137 | 138 | //repeat chars. 139 | if ( (val & 0x80) != 0 ) { 140 | cel_strbuff_append_char(sb, inbuff[i+1], len); 141 | i += 2; 142 | } else { //no-repeat chars. 143 | cel_strbuff_append_from(sb, &inbuff[i+1], len, 1); 144 | i += (len + 1); 145 | } 146 | } 147 | 148 | return 1; 149 | } 150 | 151 | 152 | /* compress the specified file with RLE compress algorhim. 153 | * 154 | * @param infile - the file to encode. 155 | * @param outfile 156 | * @return the length of the encoded file or -1 for failed. 157 | * */ 158 | CEL_API int cel_rle_encode_file( char * infile, char * outfile ) 159 | { 160 | FILE * infp, * outfp; 161 | uint_t len; 162 | int length = 0; 163 | char inbuff[127]; 164 | cel_strbuff_t *sb = new_cel_strbuff(); 165 | 166 | //Open the file. 167 | if ( (infp = fopen( infile, "rb" )) == NULL 168 | || (outfp = fopen(outfile, "wb")) == NULL ) { 169 | return -1; 170 | } else { 171 | while ( (len = fread( inbuff, 1, 127, infp )) > 0 ) { 172 | //encode the buffer 173 | //return the string after encode. 174 | cel_rle_encode_string( inbuff, len, sb ); 175 | if ( fwrite( sb->buffer, 1, sb->size, outfp ) != sb->size ) { 176 | return -1; 177 | } 178 | 179 | length += sb->size; 180 | cel_strbuff_clear(sb); 181 | } 182 | 183 | fclose(infp); 184 | fclose(outfp); 185 | } 186 | 187 | //free the string buffer. 188 | free_cel_strbuff(&sb); 189 | 190 | return length; 191 | } 192 | 193 | 194 | /* decompress the specified file encode with RLE compress algorithm. 195 | * 196 | * @return the length of the decoded file or -1 for failed. 197 | * @see cel_rle_encode_file 198 | * */ 199 | CEL_API int cel_rle_decode_file( char * infile, char * outfile ) 200 | { 201 | FILE * infp, * outfp; 202 | register char val; 203 | register int length = 0, len; 204 | char inbuff[127]; 205 | 206 | if ( (infp = fopen( infile, "rb" )) == NULL 207 | || (outfp = fopen( outfile, "wb" )) == NULL ) { 208 | return -1; 209 | } else { 210 | while ( (val = getc(infp)) != EOF ) { 211 | //Get the data length. 212 | len = (val & 0x7F); 213 | if ( len > 127 ) return -1; 214 | length += len; 215 | 216 | //repeat chars. 217 | if ( (val & 0x80) != 0 ) { 218 | //check the EOF. 219 | if ( (val = getc(infp)) == EOF ) 220 | return -1; 221 | 222 | memset( inbuff, val, len ); 223 | if ( fwrite( inbuff, 1, len, outfp ) != len ) 224 | return -1; 225 | } else { //no-repeat chars. 226 | if ( fread( inbuff, 1, len, infp ) != len ) 227 | return -1; 228 | if ( fwrite( inbuff, 1, len, outfp ) != len ) 229 | return -1; 230 | } 231 | } 232 | 233 | fclose( infp ); 234 | fclose( outfp ); 235 | } 236 | 237 | return length; 238 | } 239 | /* end rle 240 | * }}} 241 | * */ 242 | 243 | 244 | 245 | 246 | /*{{{ 247 | * @date 2013-09-30 248 | * lzw compress module. 249 | * */ 250 | 251 | //lzw encode dictionary hash release callback function. 252 | //free all the key. 253 | static void lzw_encode_rcb( cel_hashmap_node_t *e ) 254 | { 255 | cel_free( e->key ); 256 | } 257 | 258 | /* encode the specified string with lzw algorithm. 259 | * 260 | * @param source string. 261 | * @param len - length of the source string. 262 | * @param sb - encode result string buffer. 263 | * */ 264 | CEL_API int cel_lzw_encode_string( char * inbuff, uint_t len, cel_strbuff_t *sb ) 265 | { 266 | int dsize = 256, i, v = 0, rlen = 0, bits = CHAR_BIT; 267 | cel_hashmap_t *map = new_cel_hashmap_opacity(16, (float)0.80); 268 | cel_strbuff_t *S = new_cel_strbuff_opacity(3); 269 | cel_intArray_t *ret = new_cel_intArray(); 270 | 271 | //initialize the dictionary. 272 | char Z[2] = {0}; 273 | for ( i = 0; i < dsize; i++ ) { 274 | if ( sprintf( Z, "%c", i ) == -1 ) return 0; 275 | cel_ihashmap_put(map, strdup(Z), i); 276 | } 277 | 278 | //encode loop 279 | for ( i = 0; i < len; i++ ) { 280 | Z[0] = inbuff[i]; 281 | cel_strbuff_append_char(S, Z[0], 1); 282 | 283 | //check the dictionary. 284 | if ( ! cel_ihashmap_exists(map, S->buffer) ) { 285 | //add the S+Z to dictionary. 286 | cel_ihashmap_put(map, strdup(S->buffer), dsize++); 287 | 288 | //remove the Z from S as the output. 289 | cel_strbuff_remove(S, S->size - 1, 1); 290 | cel_intArray_add(ret, cel_ihashmap_get(map, S->buffer)); 291 | //printf("+%d\n", cel_ihashmap_get(map, S->buffer)); 292 | 293 | //reset the S to Z. 294 | cel_strbuff_clear(S); 295 | cel_strbuff_append_char(S, Z[0], 1); 296 | } 297 | } 298 | 299 | //Handle the left buffer. 300 | //if ( ! cel_strbuff_empty(S) ) 301 | cel_intArray_add(ret, cel_ihashmap_get(map, S->buffer)); 302 | 303 | free_cel_hashmap(&map, lzw_encode_rcb); 304 | free_cel_strbuff(&S); 305 | 306 | 307 | //convert the encode codes to binary string. 308 | dsize = 256; 309 | for ( i = 0; i < ret->size; i++ ) { 310 | //printf("v=%d, rlen=%d, bits=%d\n", v, rlen, bits); 311 | v = ( v << bits ) + ret->items[i]; 312 | rlen += bits; 313 | dsize++; 314 | 315 | //check the range 316 | if ( dsize > ( 1 << bits ) ) bits++; 317 | 318 | //handle the bits large than CHAR_BIT 319 | while ( rlen > 7 ) { 320 | rlen -= CHAR_BIT; 321 | cel_strbuff_append_char(sb, (v >> rlen), 1); 322 | v &= ( 1 << rlen ) - 1; 323 | } 324 | } 325 | 326 | //check and handle the left bits. 327 | if ( rlen > 0 ) { 328 | cel_strbuff_append_char(sb, v << (CHAR_BIT - rlen), 1); 329 | } 330 | 331 | return 1; 332 | } 333 | 334 | 335 | //lzw decode dictionary hash release callback function. 336 | //free all the key. 337 | static void lzw_decode_rcb( cel_hashmap_node_t *e ) 338 | { 339 | cel_free( e->key ); 340 | cel_free( e->value.ptr ); 341 | } 342 | 343 | /* decode the specified string with lzw algorithm. 344 | * 345 | * @param source string. 346 | * @param len - length of the source string. 347 | * @param sb - encode result string buffer. 348 | * */ 349 | CEL_API int cel_lzw_decode_string( cstring inbuff, uint_t len, cel_strbuff_t *sb ) 350 | { 351 | int dsize = 256, i; 352 | int v = 0, rlen = 0; 353 | uint_t bits = CHAR_BIT; 354 | char Z[4] = {0}, key[sizeof(int)] = {0}; 355 | char * entry; 356 | 357 | cel_intArray_t *ret = new_cel_intArray_opacity(len); 358 | cel_hashmap_t *map; 359 | cel_strbuff_t *S, *T; 360 | 361 | //Convert the binary string to lzw codes. 362 | for ( i = 0; i < len; i++ ) { 363 | v = ( v << CHAR_BIT ) + ((uchar_t)inbuff[i]); 364 | rlen += CHAR_BIT; 365 | if ( rlen >= bits ) { 366 | rlen -= bits; 367 | cel_intArray_add(ret, (v >> rlen)); 368 | v &= (1 << rlen) - 1; 369 | dsize++; 370 | if ( dsize > (1 << bits)) bits++; 371 | } 372 | } 373 | 374 | 375 | //decode the lzw codes. 376 | map = new_cel_hashmap_opacity(16, (float)0.80); 377 | S = new_cel_strbuff(); 378 | T = new_cel_strbuff(); 379 | dsize = 256; 380 | 381 | //initialize the table. 382 | for ( i = 0; i < dsize; i++ ) { 383 | if ( sprintf(key, "%d", i) == -1 ) return 0; 384 | if ( sprintf(Z , "%c", i) == -1 ) return 0; 385 | cel_hashmap_put(map, strdup(key), strdup(Z)); 386 | } 387 | 388 | /* input the first item from ret, then 389 | * continue from the second one. 390 | * so, the first dsize++ is useless. 391 | * */ 392 | dsize--; 393 | 394 | //decode loop 395 | for ( i = 0; i < ret->size; i++ ) { 396 | if ( sprintf(key, "%d", ret->items[i]) == -1 ) { 397 | return 0; 398 | } 399 | //printf("i=%d, key=%s, ", i, key); 400 | 401 | if ( cel_hashmap_exists(map, key) ) { 402 | entry = (char *)cel_hashmap_get( map, key ); 403 | } else { 404 | cel_strbuff_clear(T); 405 | cel_strbuff_append(T, S->buffer, 1); 406 | cel_strbuff_append_char(T, S->buffer[0], 1); 407 | entry = T->buffer; //S + S[0] 408 | } 409 | 410 | //append the entry to the result. 411 | cel_strbuff_append(sb, entry, 1); 412 | //printf("entry=%s\n", entry); 413 | //printf("%s\n\n", sb->buffer); 414 | 415 | //add the entry to the dictionary. 416 | if ( sprintf(key, "%d", dsize++) == -1 ) return 0; 417 | cel_strbuff_append_char(S, entry[0], 1);//S + entry[0] 418 | cel_hashmap_put(map, strdup(key), strdup(S->buffer)); 419 | 420 | //set S = entry. 421 | cel_strbuff_clear(S); 422 | cel_strbuff_append(S, entry, 1); 423 | } 424 | 425 | //free the allocation. 426 | free_cel_strbuff(&S); 427 | free_cel_strbuff(&T); 428 | free_cel_hashmap(&map, lzw_decode_rcb); 429 | 430 | return 1; 431 | } 432 | 433 | /* encode the specified file with lzw compress algorithm. 434 | * 435 | * @param srcfile 436 | * @param dstfile - encode destination file. 437 | * */ 438 | CEL_API int cel_lzw_encode_file( char * srcfile, char * dstfile ) 439 | { 440 | return 1; 441 | } 442 | 443 | /* decode the specified file with lzw compress algorithm. 444 | * 445 | * @param srcfile. 446 | * @param dstfile - decode destination file. 447 | * */ 448 | CEL_API int cel_lzw_decode_file( char * srcfile, char * dstfile ) 449 | { 450 | return 1; 451 | } 452 | /*}}}*/ 453 | -------------------------------------------------------------------------------- /src/cel_compress.h: -------------------------------------------------------------------------------- 1 | /** 2 | * cel data compress/decompress module interface header file. 3 | * @package src/cel_compress.h. 4 | * 5 | * compress algorithm: 6 | * 1. RLE - Run length encoding. 7 | * 2. LZW 8 | * 3. Huffman 9 | * 4. LZ(LZ77, LZSS) 10 | * 11 | * @author chenxin 12 | * @date 2013-09-20 13 | */ 14 | #ifndef cel_compress_h 15 | #define cel_compress_h 16 | 17 | #include "cel_api.h" 18 | #include "cel_string.h" 19 | #include "cel_array.h" 20 | 21 | /* {{{ rle - run length encoding. 22 | * */ 23 | 24 | /* compress the specified string with rle algorithm. 25 | * 26 | * @return 0 for false and 1 for encode success. 27 | * */ 28 | //CEL_API uint_t cel_rle_encode_string( char *, uint_t, char *, uint_t ); 29 | CEL_API int cel_rle_encode_string( char *, uint_t, cel_strbuff_t * ); 30 | 31 | /* decompress the specified string 32 | * compress with RLE compress algorithm. 33 | * 34 | * @return 0 for false and 1 for decode success. 35 | * */ 36 | CEL_API int cel_rle_decode_string( char *, uint_t, cel_strbuff_t * ); 37 | 38 | /* compress the specified file with RLE compress algorhim. 39 | * 40 | * @return the length of the encoded file or -1 for failed. 41 | * */ 42 | CEL_API int cel_rle_encode_file( char *, char * ); 43 | 44 | /* decompress the specified file 45 | * encode with RLE compress algorithm. 46 | * 47 | * @return the length of the decoded file or -1 for failed. 48 | * */ 49 | CEL_API int cel_rle_decode_file( char *, char * ); 50 | /* end rle 51 | * }}} 52 | * */ 53 | 54 | 55 | 56 | 57 | /*{{{ 58 | * @date 2013-09-30 59 | * lzw compress module. 60 | * */ 61 | 62 | /* encode the specified string with lzw algorithm. 63 | * 64 | * @param source string. 65 | * @param len - length of the source string. 66 | * @param sb - encode result string buffer. 67 | * */ 68 | CEL_API int cel_lzw_encode_string( char *, uint_t, cel_strbuff_t * ); 69 | 70 | /* decode the specified string with lzw algorithm. 71 | * 72 | * @param source string. 73 | * @param len - length of the source string. 74 | * @param sb - encode result string buffer. 75 | * */ 76 | CEL_API int cel_lzw_decode_string( cstring, uint_t, cel_strbuff_t * ); 77 | 78 | /* encode the specified file with lzw compress algorithm. 79 | * 80 | * @param srcfile 81 | * @param dstfile - encode destination file. 82 | * */ 83 | CEL_API int cel_lzw_encode_file( char *, char * ); 84 | 85 | /* decode the specified file with lzw compress algorithm. 86 | * 87 | * @param srcfile. 88 | * @param dstfile - decode destination file. 89 | * */ 90 | CEL_API int cel_lzw_decode_file( char *, char * ); 91 | /*}}}*/ 92 | 93 | #endif /*end ifndef*/ 94 | -------------------------------------------------------------------------------- /src/cel_crc32.c: -------------------------------------------------------------------------------- 1 | /** 2 | * CRC32(cyclic redundancy check) implements functions. 3 | * define in header file src/cel_hash.h. 4 | * @package src/cel_crc32.c 5 | * 6 | * @author chenxin 7 | */ 8 | #include "cel_hash.h" 9 | #include 10 | #include 11 | #include 12 | 13 | //crc32 polynomial 14 | #define POLYNOMIAL_CRC32 0x04c11db7L 15 | 16 | /** 17 | * crc32 quick driven table. 18 | * generated by function create_crc32_table 19 | * and print by function print_crc32_table. 20 | */ 21 | static const uint32_t crc32_table[256] = { 22 | 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 23 | 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005, 24 | 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 25 | 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 26 | 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 27 | 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75, 28 | 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 29 | 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd, 30 | 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 31 | 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 32 | 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81, 33 | 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d, 34 | 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 35 | 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95, 36 | 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 37 | 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 38 | 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae, 39 | 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072, 40 | 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 41 | 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca, 42 | 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, 43 | 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 44 | 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066, 45 | 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba, 46 | 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 47 | 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692, 48 | 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 49 | 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 50 | 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e, 51 | 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2, 52 | 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 53 | 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a, 54 | 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, 55 | 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 56 | 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 57 | 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53, 58 | 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 59 | 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b, 60 | 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 61 | 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 62 | 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 63 | 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b, 64 | 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 65 | 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3, 66 | 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 67 | 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 68 | 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f, 69 | 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3, 70 | 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 71 | 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c, 72 | 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, 73 | 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 74 | 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30, 75 | 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec, 76 | 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 77 | 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654, 78 | 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 79 | 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 80 | 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18, 81 | 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4, 82 | 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 83 | 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c, 84 | 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, 85 | 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4, 86 | }; 87 | 88 | 89 | //create the crc32 table 90 | //static void create_crc32_table() 91 | //{ 92 | // register ushort_t i, j; 93 | // register uint32_t crc; 94 | // 95 | // for ( i = 0; i < 256; i++ ) 96 | // { 97 | // crc = ( (uint32_t)i << 24); 98 | // for ( j = 0; j < 8; j++ ) 99 | // { 100 | // if ( (crc & 0x80000000L) != 0 ) 101 | // crc = (crc << 1) ^ POLYNOMIAL_CRC32; 102 | // else 103 | // crc = (crc << 1); 104 | // } 105 | // crc32_table[i] = crc; 106 | // } 107 | //} 108 | 109 | //print the crc32 table 110 | //static void print_crc32_table( uint32_t * table, char * name ) 111 | //{ 112 | // register uint_t i = 0,j; 113 | // printf("static const uint32_t %s[256] = {\n", name); 114 | // while ( i < 256 ) { 115 | // for ( j = 0; j < 4; j++ ) { 116 | // printf("0x%x, ", table[i + j]); 117 | // } 118 | // printf("\n"); 119 | // i += 4; 120 | // } 121 | // printf("};\n"); 122 | //} 123 | 124 | /** 125 | * CRC32 hash algorithm. 126 | * cyclic Redundancy check. 127 | */ 128 | CEL_API uint32_t cel_crc32( uint32_t crc, const void * data, size_t length ) 129 | { 130 | register uint_t i,j; 131 | 132 | /* you may use the following two functions to generate 133 | * the above the lookup table, 134 | * after the calculation, we don't need the two functions 135 | * any more. yeah, we use if only once. 136 | */ 137 | //create_crc32_table(); 138 | //print_crc32_table(crc32_table, "crc32_table"); 139 | uchar_t * str = ( uchar_t * ) data; 140 | 141 | for ( i = 0; i < length; i++ ) { 142 | j = ((int)(crc >> 24) ^ *str++) & 0xFF; 143 | crc = (crc << 8) ^ crc32_table[j]; 144 | } 145 | crc = ~crc; 146 | 147 | return crc; 148 | } 149 | 150 | //we use a macro define instead. 151 | //CEL_API uint32_t cel_crc32_hash( const char * str ) 152 | //{ 153 | // return 0; 154 | //} 155 | 156 | #define BUFSIZE 2048 157 | /** 158 | * generate a crc32 code for a specified file. 159 | * 160 | * @param file 161 | * @return uint32_t 162 | */ 163 | CEL_API void cel_crc32_file( const char *_file, uint32_t *file_crc32 ) 164 | { 165 | FILE * stream; 166 | char buffer[BUFSIZE]; 167 | uint32_t crc = 0xffffffff; 168 | 169 | //open the file. 170 | if ( ( stream = fopen(_file, "r") ) == NULL ) { 171 | printf("Error: unable to open file %s\n", _file); 172 | *file_crc32 = crc; 173 | return; 174 | } 175 | 176 | //read and count the crc32 code. 177 | while ( fgets(buffer, BUFSIZE, stream) != NULL ) { 178 | //printf("buffer: %s\n", buffer); 179 | crc = cel_crc32( crc, buffer, strlen(buffer) ); 180 | } 181 | *file_crc32 = crc; 182 | 183 | //close the file stream. 184 | fclose(stream); 185 | } 186 | -------------------------------------------------------------------------------- /src/cel_hash.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel hash function implemented source file. 3 | * @package src/cel_hash.c 4 | * 5 | * @author chenxin 6 | */ 7 | #include "cel_hash.h" 8 | #include 9 | #include 10 | 11 | //get 32 bits 12 | #undef get32bits 13 | #if ( defined(__GNUC__) || defined(__i386__) || defined(__WATCOMC__) \ 14 | || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__TURBOC__) ) 15 | #define get32bits(d) (*((const uint32_t *) (d))) 16 | #endif 17 | 18 | #if ! defined(get32bits) 19 | //type of d should be char * 20 | #define get32bits(d) ( (uint32_t)(d)[0] \ 21 | + ((uint32_t)(d)[1] << 8) \ 22 | + ((uint32_t)(d)[2] << 16) \ 23 | + ((uint32_t)(d)[3] << 24) ) 24 | #endif 25 | 26 | //get 16 bits 27 | #undef get16bits 28 | #if ( defined(__GNUC__) || defined(__i386__) || defined(__WATCOMC__) \ 29 | || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__TURBOC__) ) 30 | #define get16bits(d) (*((const uint16_t *) (d))) 31 | #endif 32 | 33 | #if ! defined(get16bits) 34 | //type of d should be char * 35 | #define get16bits(d) ( (uint16_t)(d)[0] + ((uint16_t)(d)[1] << 8) ) 36 | #endif 37 | 38 | //simple BKDR hash algorithm 39 | CEL_API uint32_t cel_bkdr_hash( const char * str ) 40 | { 41 | register uint32_t h = 0; 42 | uint32_t seed = 131; //31 131 1313 13131 43 | 44 | while ( *str != '\0' ) { 45 | h = h * seed + ( *str++ ); 46 | } 47 | 48 | return (h & 0x7FFFFFFF); 49 | } 50 | 51 | /** 52 | * FNV hash algorithm wildly use in Unix system. 53 | */ 54 | CEL_API uint32_t cel_fnv_hash( const char *str ) 55 | { 56 | register uint32_t h = 2166136261UL; 57 | uint32_t prime = 16777619; 58 | 59 | while ( *str != '\0' ) { 60 | h *= prime; 61 | h ^= *str++; 62 | } 63 | 64 | return (h & 0x7FFFFFFF); 65 | } 66 | 67 | /** 68 | * FNV-1a hash algorithm. wildly use in Unix system. 69 | * the only difference between FNV-1 is the order of xor and multipy. 70 | * @link http://www.isthe.com/chongo/tech/comp/fnv/ 71 | */ 72 | CEL_API uint32_t cel_fnv1a_hash( const char *str ) 73 | { 74 | register uint32_t h = 2166136261UL; 75 | uint32_t prime = 16777619; 76 | 77 | while ( *str != '\0' ) { 78 | h ^= (*str++); 79 | h *= prime; 80 | } 81 | 82 | return (h & 0x7FFFFFFF); 83 | } 84 | 85 | /** 86 | * AP hash algorithm. 87 | * Design by Arash Partow. 88 | */ 89 | CEL_API uint32_t cel_ap_hash( const char *str ) 90 | { 91 | register uint32_t i = 0, h = 0; 92 | 93 | while ( *str != '\0' ) { 94 | if ( (i++ & 1) == 0) { 95 | h ^= ( (h << 7) ^ (*str++) ^ (h << 3) ); 96 | } else { 97 | h ^= ( ~( (h << 11) ^ (*str++) ^ (h << 5) ) ); 98 | } 99 | } 100 | 101 | return (h & 0x7FFFFFFF); 102 | } 103 | 104 | /** 105 | * DJP hash algorithm. 106 | * invented by doctor Daniel J. Bernstein. 107 | */ 108 | CEL_API uint32_t cel_djp_hash( const char *str ) 109 | { 110 | register uint32_t h = 5381; 111 | 112 | while ( *str != '\0' ) { 113 | h += (h << 5) + (*str++); 114 | } 115 | 116 | return ( h & 0x7FFFFFFF ); 117 | } 118 | 119 | /** 120 | * DJP hash algorithm 2. 121 | * invented by doctor Daniel J. Bernstein. 122 | */ 123 | CEL_API uint32_t cel_djp2_hash( const char *str ) 124 | { 125 | register uint32_t h = 5381; 126 | 127 | while ( *str != '\0' ) { 128 | h = h * 33 ^ (*str++); 129 | } 130 | 131 | return (h & 0x7FFFFFFF); 132 | } 133 | 134 | /** 135 | * JS hash algorithm. 136 | * invented by Justin Sobel. 137 | */ 138 | CEL_API uint32_t cel_js_hash( const char *str ) 139 | { 140 | register uint32_t h = 0; 141 | 142 | while ( *str != '\0' ) { 143 | h ^= ( (h << 5) + (*str++) + (h << 2) ); 144 | } 145 | 146 | return (h & 0x7FFFFFFF); 147 | } 148 | 149 | /** 150 | * SDMS hash algorithm. 151 | * came from open source simple database engin SDMS. 152 | */ 153 | CEL_API uint32_t cel_sdms_hash( const char *str ) 154 | { 155 | register uint32_t h = 0; 156 | 157 | while ( *str != '\0' ) { 158 | h = 65599 * h + (*str++); 159 | } 160 | 161 | return (h & 0x7FFFFFFF); 162 | } 163 | 164 | /** 165 | * RS hash algoritm. 166 | * came from book [Algorithm in c] Written by Robert Sedgwicks. 167 | */ 168 | CEL_API uint32_t cel_rs_hash( const char *str ) 169 | { 170 | register uint32_t h = 0; 171 | register uint32_t magic = 63689; 172 | 173 | while ( *str != '\0' ) { 174 | h = h * magic + (*str++); 175 | magic *= 378551; 176 | } 177 | 178 | return (h & 0x7FFFFFFF); 179 | } 180 | 181 | /** 182 | * DEK hash algorithm. 183 | * came from [Art of Computer programing Volumes 3] written by Donald E.knuth. 184 | */ 185 | CEL_API uint32_t cel_dek_hash( const char *str ) 186 | { 187 | register uint32_t h = 0; 188 | 189 | while ( *str != '\0' ) { 190 | h = ( (h << 5) ^ (h >> 27) ^ (*str++) ); 191 | } 192 | 193 | return (h & 0x7FFFFFFF); 194 | } 195 | 196 | /** 197 | * ELF hash algorithm. 198 | * executable and linking Format. 199 | */ 200 | CEL_API uint32_t cel_elf_hash( const char *str ) 201 | { 202 | register uint32_t h = 0; 203 | register uint32_t x = 0; 204 | 205 | while ( *str != '\0' ) { 206 | h = (h << 24) + (*str++); 207 | if ( (x = h & 0xF0000000L) != 0 ) { 208 | h ^= (h >> 24); 209 | h &= ~x; 210 | } 211 | } 212 | 213 | return (h & 0x7FFFFFFF); 214 | } 215 | 216 | #define cel_bj_mix( a, b, c ) \ 217 | {\ 218 | a -= b; a -= c; a ^= (c>>13); \ 219 | b -= c; b -= a; b ^= (a<<8); \ 220 | c -= a; c -= b; c ^= (b>>13); \ 221 | a -= b; a -= c; a ^= (c>>12); \ 222 | b -= c; b -= a; b ^= (a<<16); \ 223 | c -= a; c -= b; c ^= (b>>5); \ 224 | a -= b; a -= c; a ^= (c>>3); \ 225 | b -= c; b -= a; b ^= (a<<10); \ 226 | c -= a; c -= b; c ^= (b>>15); \ 227 | } 228 | 229 | /** 230 | * Bob Jenkins hash algorithm. 231 | * @see http://burtleburtle.net/bob/hash/evahash.html. 232 | */ 233 | CEL_API uint32_t cel_bobJenkins_hash32( 234 | const void *str, 235 | uint_t length, /*the length of the key in bytes*/ 236 | uint32_t initval ) /*the previous hash, or an arbitrary value*/ 237 | { 238 | register uint32_t a,b,c; 239 | register uint32_t len; /*how many bytes still need to mixing*/ 240 | uchar_t * k = (uchar_t *) str; 241 | 242 | //Set up internal state. 243 | len = length; 244 | a = b = 0x9e3779b9; /*Golden ratio: an arbitrary value.*/ 245 | c = initval; /*variable initialization of internal state*/ 246 | 247 | 248 | //handle most of the bytes of the str 249 | while ( len >= 12 ) { 250 | a += get32bits(k); 251 | b += get32bits(k + 4); 252 | c += get32bits(k + 8); 253 | cel_bj_mix(a, b, c); 254 | k += 12; len -= 12; 255 | } 256 | 257 | //handle the last 11 bytes 258 | c += length; 259 | switch ( len ) { 260 | /* all the case statements fall through */ 261 | case 11: c+=((uint32_t)k[10]<<24); 262 | case 10: c+=((uint32_t)k[9]<<16); 263 | case 9 : c+=((uint32_t)k[8]<<8); 264 | // the first byte of c is reserved for the length 265 | case 8 : b+=((uint32_t)k[7]<<24); 266 | case 7 : b+=((uint32_t)k[6]<<16); 267 | case 6 : b+=((uint32_t)k[5]<<8); 268 | case 5 : b+=k[4]; 269 | case 4 : a+=((uint32_t)k[3]<<24); 270 | case 3 : a+=((uint32_t)k[2]<<16); 271 | case 2 : a+=((uint32_t)k[1]<<8); 272 | case 1 : a+=k[0]; 273 | /* case 0: nothing left to add */ 274 | } 275 | cel_bj_mix(a,b,c); 276 | 277 | return c; 278 | } 279 | 280 | CEL_API uint32_t cel_bobJenkins32_hash( const char *str ) 281 | { 282 | return cel_bobJenkins_hash32(str, strlen(str), 0x00); 283 | } 284 | 285 | /** 286 | * Murmur hash algorithm is a relative fast hash function 287 | * from http://murmurhash.googlepages.com for platforms 288 | * with efficient multiuoplication. 289 | * 290 | * Generate 32 bit hash from byte array of the given length and seed. 291 | */ 292 | CEL_API uint32_t cel_murmur_hash32( const void *str, uint_t length, uint_t seed ) 293 | { 294 | //The 'm' and 'r' are mixing constants generated offline. 295 | //They are not really 'magic', they just happen to work well. 296 | 297 | const uint32_t m = 0x5bd1e995; 298 | const int r = 24; 299 | 300 | uint32_t k; 301 | 302 | //Initialize the hash to a random value. 303 | 304 | uint32_t h = seed & length; 305 | 306 | //Mix 4 bytes at a time into the hash. 307 | 308 | const uchar_t * data = (uchar_t *) str; 309 | while ( length >= 4 ) { 310 | k = *(uint32_t *) data; 311 | 312 | k *= m; 313 | k ^= k >> r; 314 | k *= m; 315 | 316 | h *= m; 317 | h ^= k; 318 | 319 | data += 4; 320 | length -= 4; 321 | } 322 | 323 | //Handle the last few bytes of the input array. 324 | switch ( length ) { 325 | case 3: h ^= data[2] << 16; break; 326 | case 2: h ^= data[1] << 8; break; 327 | case 1: h ^= data[0]; h *= m; break; 328 | } 329 | 330 | //Do a few final mixes of the hash to ensure the last few 331 | //bytes are well-incorporated. 332 | 333 | h ^= h >> 13; 334 | h *= m; 335 | h ^= h >> 15; 336 | h &= 0x7FFFFFFF; 337 | 338 | return h; 339 | } 340 | -------------------------------------------------------------------------------- /src/cel_hash.h: -------------------------------------------------------------------------------- 1 | /** 2 | * cel hash function interface . 3 | * @package src/cel_hash.h. 4 | * 5 | * @author chenxin 6 | */ 7 | #ifndef _cel_hash_h 8 | #define _cel_hash_h 9 | 10 | #include "cel_api.h" 11 | #include 12 | #include 13 | 14 | //simple BKDR hash algorithm 15 | CEL_API uint32_t cel_bkdr_hash( const char * ); 16 | 17 | /** 18 | * FNV hash algorithm. 19 | * wildly use in Unix system. 20 | */ 21 | CEL_API uint32_t cel_fnv_hash( const char * ); 22 | 23 | /** 24 | * FNV-1a hash algorithm. 25 | * wildly use in Unix system. 26 | */ 27 | CEL_API uint32_t cel_fnv1a_hash( const char * ); 28 | 29 | /** 30 | * AP hash algorithm. 31 | * design by Arash Partow. 32 | */ 33 | CEL_API uint32_t cel_ap_hash( const char * ); 34 | 35 | /** 36 | * DJP hash algorithm. 37 | * invented by doctor Daniel J. Bernstein. 38 | */ 39 | CEL_API uint32_t cel_djp_hash( const char * ); 40 | 41 | /** 42 | * DJP hash algorithm 2. 43 | * invented by doctor Daniel J. Bernstein. 44 | */ 45 | CEL_API uint32_t cel_djp2_hash( const char * ); 46 | 47 | /** 48 | * JS hash algorithm. 49 | * invented by Justin Sobel. 50 | */ 51 | CEL_API uint32_t cel_js_hash( const char * ); 52 | 53 | /** 54 | * SDMS hash algorithm. 55 | * came from open source simple database engin SDMS. 56 | */ 57 | CEL_API uint32_t cel_sdms_hash( const char * ); 58 | 59 | /** 60 | * RS hash algoritm. 61 | * came from book [Algorithm in c] Written by Robert Sedgwicks. 62 | */ 63 | CEL_API uint32_t cel_rs_hash( const char * ); 64 | 65 | /** 66 | * DEK hash algorithm. 67 | * came from [Art of Computer programing Volumes 3] 68 | * written by Donald E.knuth. 69 | */ 70 | CEL_API uint32_t cel_dek_hash( const char * ); 71 | 72 | /** 73 | * ELF hash algorithm. 74 | * executable and linking Format. 75 | */ 76 | CEL_API uint32_t cel_elf_hash( const char * ); 77 | 78 | /** 79 | * Bob Jenkins hash algorithm. 80 | * @see http://burtleburtle.net/bob/hash/evahash.html. 81 | */ 82 | CEL_API uint32_t cel_bobJenkins_hash32( const void *, 83 | uint_t length, uint32_t initval ); 84 | 85 | CEL_API uint32_t cel_bobJenkins32_hash( const char * ); 86 | 87 | 88 | /** 89 | * Murmur hash algorithm is a relative fast hash function 90 | * from http://murmurhash.googlepages.com for platforms 91 | * with efficient multiuoplication. 92 | * 93 | * Generate 32 bit hash from byte array of the given length and seed. 94 | */ 95 | CEL_API uint32_t cel_murmur_hash32( const void *, uint_t length, uint_t seed ); 96 | 97 | //CEL_API uint32_t cel_murmur32_hash( const char * ); 98 | #define cel_murmur32_hash(str) cel_murmur_hash32(str, strlen(str), 0x9747b28c) 99 | 100 | /** 101 | * CRC32 hash algorithm. 102 | * cyclic Redundancy check. 103 | */ 104 | CEL_API uint32_t cel_crc32( uint32_t, const void *, uint_t ); 105 | 106 | //CEL_API uint32_t cel_crc32_hash( const char * ); 107 | #define cel_crc32_hash(str) cel_crc32(-1, str, strlen(str)) 108 | 109 | /** 110 | * generate a crc32 code for a specified file. 111 | * 112 | * @param file 113 | * @return uint32_t 114 | */ 115 | CEL_API void cel_crc32_file( const char *, uint32_t * ); 116 | 117 | 118 | /** 119 | * MD5 hash algorithm. 120 | * Message digest Algorithm 5. 121 | */ 122 | // struct cel_md5_entry { 123 | // ulong_t state[4]; /*state (ABCD)*/ 124 | // ulong_t count[2]; /*number of bits, modulo 2^64*/ 125 | // uchar_t buffer[64]; /*input buffer*/ 126 | // }; 127 | // typedef struct cel_md5_entry cel_md5_t; 128 | 129 | /* Define the state of the MD5 Algorithm. */ 130 | typedef struct cel_md5_entry { 131 | uint_t count[2]; /* message length in bits, lsw first */ 132 | uint_t abcd[4]; /* digest buffer */ 133 | uchar_t buf[64]; /* accumulate block */ 134 | } cel_md5_t; 135 | 136 | CEL_API void cel_md5_init( cel_md5_t * ); 137 | CEL_API void cel_md5_update( cel_md5_t *, const uchar_t *, int ); 138 | CEL_API void cel_md5_final( cel_md5_t *, uchar_t digest[16] ); 139 | 140 | CEL_API void cel_md5_string( const cstring, uchar_t digest[16] ); 141 | CEL_API int cel_md5_file( const cstring, uchar_t digest[16] ); 142 | CEL_API void cel_md5_print(uchar_t digest[16], char buffer[32]); 143 | 144 | #endif /*end ifndef*/ 145 | 146 | -------------------------------------------------------------------------------- /src/cel_hashmap.c: -------------------------------------------------------------------------------- 1 | /** 2 | * hashmap implemented functions . 3 | * @package src/cel_hashmap.c 4 | * 5 | * @author chenxin 6 | */ 7 | #include "cel_hashmap.h" 8 | #include "cel_math.h" 9 | #include "cel_hash.h" 10 | #include 11 | 12 | static cel_hashmap_node_t **create_node_blocks( uint_t ); 13 | static cel_hashmap_node_t *create_hashmap_node( char *, void *, cel_hashmap_node_t * ); 14 | static void rebuild_hash( cel_hashmap_t * ); 15 | 16 | //hash function marco define 17 | #define cel_hash( str ) cel_bkdr_hash( str ) 18 | 19 | /* 20 | * create a default cel hashmap with a default] 21 | * length and default factor 22 | * 23 | * @return cel_hashmap_t 24 | * A macro define has replace this 25 | */ 26 | //CEL_API cel_hashmap_t *new_cel_hashmap() 27 | //{ 28 | // return new_cel_hashmap_opacity( 16, 0.75 ); 29 | //} 30 | 31 | /* 32 | * create a cel hashmap with a specified length and factory . 33 | * 34 | * @param opacity 35 | * @param factor 36 | * @return cel_hashmap_t 37 | */ 38 | CEL_API cel_hashmap_t *new_cel_hashmap_opacity( int opacity, float factor ) 39 | { 40 | cel_hashmap_t *map = ( cel_hashmap_t * )cel_malloc(sizeof( cel_hashmap_t )); 41 | if ( map == NULL ) { 42 | CEL_ALLOCATE_ERROR("new_cel_hashmap_opacity", sizeof(cel_hashmap_t)); 43 | } 44 | 45 | if ( cel_hashmap_init(map, opacity, factor) == 0 ) { 46 | cel_free(map); 47 | CEL_ALLOCATE_ERROR("cel_hashmap_create", opacity); 48 | } 49 | 50 | return map; 51 | } 52 | 53 | /* 54 | * free the specified cel hashmap. 55 | * callback function will be invoked for each node if it is not null. 56 | */ 57 | CEL_API void free_cel_hashmap(cel_hashmap_t **hash, cel_hashmap_rcb_fn_t relfunc ) 58 | { 59 | if ( hash == NULL ) return; 60 | if ( *hash != NULL ) { 61 | cel_hashmap_destroy(*hash, relfunc); 62 | cel_free( *hash ); 63 | *hash = NULL; 64 | } 65 | } 66 | 67 | /* 68 | * initialize the specified hashmap 69 | * 70 | * @param cel_hashmap_t * 71 | * @param uint_t the default length of the blocks 72 | * @param float the threshold of the hash map 73 | * @return int 1 for success and 0 for failed 74 | */ 75 | CEL_API int cel_hashmap_init(cel_hashmap_t *map, int opacity, float factor) 76 | { 77 | map->table = create_node_blocks( opacity ); 78 | if ( map->table == NULL ) return 0; 79 | 80 | map->length = opacity; 81 | map->size = 0; 82 | map->factor = factor; 83 | map->threshold = ( uint_t ) ( factor * opacity ); 84 | 85 | return 1; 86 | } 87 | 88 | /* 89 | * destroy the specified hashmap 90 | * 91 | * @param cel_hashmap_t * 92 | * @return 1 for success and 0 for fialed 93 | */ 94 | CEL_API int cel_hashmap_destroy(cel_hashmap_t *hash, cel_hashmap_rcb_fn_t relfunc) 95 | { 96 | register uint_t idx; 97 | cel_hashmap_node_t *e, *next; 98 | 99 | if ( hash != NULL ) { 100 | for ( idx = 0; idx < hash->length; idx++ ) { 101 | for ( e = hash->table[idx]; e != NULL; ) { 102 | next = e->next; 103 | if ( relfunc != NULL ) relfunc(e); 104 | cel_free( e ); 105 | e = next; 106 | } 107 | } 108 | 109 | //free the table bucket 110 | cel_free( hash->table ); 111 | hash->table = NULL; 112 | } 113 | 114 | return 1; 115 | } 116 | 117 | 118 | //create a new hashmap node. 119 | static cel_hashmap_node_t **create_node_blocks(uint_t __blocks) 120 | { 121 | register uint_t i; 122 | 123 | cel_hashmap_node_t **table = ( cel_hashmap_node_t ** ) 124 | cel_calloc( sizeof( cel_hashmap_node_t * ), __blocks ); 125 | if ( table == NULL ) return NULL; 126 | 127 | //initialize the bucket 128 | for ( i = 0; i < __blocks; i++ ) { 129 | table[i] = NULL; 130 | } 131 | 132 | return table; 133 | } 134 | 135 | /* 136 | * hash rebuild mapping . 137 | * 138 | * @param hash 139 | */ 140 | static void rebuild_hash( cel_hashmap_t *hash ) 141 | { 142 | register uint_t i; 143 | uint_t idx; 144 | 145 | uint_t length = cel_next_prime(hash->length * 2 + 1); 146 | cel_hashmap_node_t **_table = create_node_blocks( length ); 147 | cel_hashmap_node_t *e, *next; 148 | 149 | if ( _table == NULL ) { 150 | return; 151 | } 152 | 153 | //printf("rebuild-hash: %d\n", length); 154 | for ( i = 0; i < hash->length; i++ ) { 155 | //for ( e = hash->table[i]; 156 | // e != NULL; ) { 157 | // //recount the hash 158 | // idx = cel_hash(e->key) % length; 159 | // tmp = e->next; 160 | // e->next = _table[idx]; 161 | // _table[idx] = e; 162 | // e = tmp; 163 | //} 164 | e = *(hash->table + i); 165 | if ( e != NULL ) { 166 | do { 167 | next = e->next; 168 | #ifdef CEL_HASHMAP_STORE_HCODE 169 | idx = e->hash % length; 170 | #else 171 | //recount the hash code 172 | idx = cel_hash(e->key) % length; 173 | #endif 174 | e->next = _table[idx]; 175 | _table[idx] = e; 176 | e = next; 177 | } while ( e != NULL ); 178 | } 179 | } 180 | 181 | //free the old bucket 182 | cel_free( hash->table ); 183 | hash->table = _table; 184 | hash->length = length; 185 | hash->threshold = (uint_t) (hash->factor * length); 186 | } 187 | 188 | /* 189 | * associated the key with the specified value . 190 | * 191 | * @param key 192 | * @param value 193 | * @return void * 194 | */ 195 | CEL_API void *cel_hashmap_put(cel_hashmap_t *hash, char *key, void *value) 196 | { 197 | void * v; 198 | cel_hashmap_node_t *e; 199 | uint_t hcode = cel_hash(key); 200 | uint_t idx = hcode % hash->length; 201 | 202 | for (e = *(hash->table + idx); e != NULL; e = e->next) { 203 | if ( key == e->key || strcmp( key, e->key ) == 0 ) { 204 | v = e->value.ptr; 205 | e->value.ptr = value; 206 | return v; 207 | } 208 | } 209 | 210 | hash->table[idx] = create_hashmap_node(key, value, hash->table[idx]); 211 | 212 | #ifdef CEL_HASHMAP_STORE_HCODE 213 | e = hash->table[idx]; 214 | e->hash = hcode; 215 | //printf("hash: %u\n", hcode); 216 | #endif 217 | hash->size++; 218 | if ( hash->size > hash->threshold ) { 219 | //rebuild the hashmap 220 | rebuild_hash(hash); 221 | } 222 | 223 | return NULL; 224 | } 225 | 226 | /* 227 | * create new hashmap node. 228 | * 229 | * @param key 230 | * @param value 231 | * @param next 232 | */ 233 | static cel_hashmap_node_t *create_hashmap_node( 234 | char *key, void *value, cel_hashmap_node_t *next ) 235 | { 236 | cel_hashmap_node_t *node = (cel_hashmap_node_t *) 237 | cel_malloc(sizeof( cel_hashmap_node_t )); 238 | if ( node == NULL ) { 239 | CEL_ALLOCATE_ERROR("create_hashmap_node", sizeof(cel_hashmap_node_t)); 240 | } 241 | 242 | node->key = key; 243 | node->value.ptr = value; 244 | node->next = next; 245 | 246 | return node; 247 | } 248 | 249 | //remove the mapping associated with the specified key 250 | CEL_API void *cel_hashmap_remove( 251 | cel_hashmap_t *hash, char *key, cel_hashmap_rcb_fn_t rfunc) 252 | { 253 | void * v; 254 | cel_hashmap_node_t *e, *prev = NULL; 255 | uint_t idx = cel_hash( key ) % hash->length; 256 | 257 | for ( e = hash->table[idx]; e != NULL; prev = e, e = e->next ) { 258 | if ( key == e->key || strcmp( key, e->key ) == 0 ) { 259 | if ( prev == NULL ) { 260 | hash->table[idx] = e->next; 261 | } else { 262 | prev->next = e->next; 263 | } 264 | 265 | v = e->value.ptr; 266 | 267 | //invoke the callback function if it is not NULL. 268 | if ( rfunc != NULL ) rfunc( e ); 269 | //free the removed node 270 | cel_free( e ); 271 | hash->size--; 272 | return v; 273 | } 274 | } 275 | 276 | return NULL; 277 | } 278 | 279 | //get the value associated with the specified key. 280 | CEL_API void *cel_hashmap_get(cel_hashmap_t *hash, char *key) 281 | { 282 | cel_hashmap_node_t *e; 283 | uint_t idx = cel_hash(key) % hash->length; 284 | 285 | for (e = hash->table[idx]; e != NULL; e = e->next) { 286 | if (key == e->key || strcmp(key, e->key) == 0) { 287 | return e->value.ptr; 288 | } 289 | } 290 | 291 | return NULL; 292 | } 293 | 294 | //check the existence of the mapping associated with the specified key. 295 | CEL_API int cel_hashmap_exists(cel_hashmap_t *hash, char *key) 296 | { 297 | cel_hashmap_node_t *e; 298 | uint_t idx = cel_hash( key ) % hash->length; 299 | 300 | for ( e = hash->table[idx]; e != NULL; e = e->next ) { 301 | if ( key == e->key || strcmp( key, e->key ) == 0 ) { 302 | return 1; 303 | } 304 | } 305 | 306 | return 0; 307 | } 308 | 309 | //replace the value associated with the specified key. 310 | CEL_API void *cel_hashmap_set(cel_hashmap_t *hash, char *key, void *value) 311 | { 312 | void * v; 313 | cel_hashmap_node_t *e; 314 | uint_t idx = cel_hash( key ) % hash->length; 315 | 316 | for ( e = hash->table[idx]; e != NULL; e = e->next ) { 317 | if ( key == e->key || strcmp( key, e->key ) == 0 ) { 318 | v = e->value.ptr; 319 | e->value.ptr = value; 320 | return v; 321 | } 322 | } 323 | 324 | return NULL; 325 | } 326 | 327 | 328 | 329 | 330 | /* {{{ char int hashmap block::start*/ 331 | 332 | /* 333 | * create new hashmap node. 334 | * 335 | * @param key 336 | * @param value 337 | * @param next 338 | */ 339 | static cel_hashmap_node_t *create_ihashmap_node( 340 | char *key, int value, cel_hashmap_node_t *next) 341 | { 342 | cel_hashmap_node_t *node = (cel_hashmap_node_t *) \ 343 | cel_malloc(sizeof( cel_hashmap_node_t )); 344 | if ( node == NULL ) { 345 | CEL_ALLOCATE_ERROR("create_ihashmap_node", sizeof(cel_hashmap_node_t)); 346 | } 347 | 348 | node->key = key; 349 | node->value.num = value; 350 | node->next = next; 351 | 352 | return node; 353 | } 354 | 355 | /* 356 | * associated the key with the specified value . 357 | * 358 | * @param char * 359 | * @param int 360 | * @return int (0 for fail and 1 for true) 361 | */ 362 | CEL_API int cel_ihashmap_put(cel_ihashmap_t *hash, char *key, int value) 363 | { 364 | cel_hashmap_node_t *e; 365 | uint_t hcode = cel_hash(key); 366 | uint_t idx = hcode % hash->length; 367 | 368 | for ( e = *(hash->table + idx); e != NULL; e = e->next ) { 369 | if ( key == e->key || strcmp( key, e->key ) == 0 ) { 370 | e->value.num = value; 371 | return 1; 372 | } 373 | } 374 | 375 | hash->table[idx] = create_ihashmap_node(key, value, hash->table[idx]); 376 | #ifdef CEL_HASHMAP_STORE_HCODE 377 | e = hash->table[idx]; 378 | e->hash = hcode; 379 | #endif 380 | hash->size++; 381 | if ( hash->size > hash->threshold ) { 382 | //rebuild the hashmap 383 | rebuild_hash(hash); 384 | } 385 | 386 | return 1; 387 | } 388 | 389 | //remove the mapping associated with the specified key 390 | CEL_API int cel_ihashmap_remove( 391 | cel_ihashmap_t *hash, char *key, cel_hashmap_rcb_fn_t rfunc) 392 | { 393 | cel_hashmap_node_t *e, *prev = NULL; 394 | uint_t idx = cel_hash(key) % hash->length; 395 | 396 | for ( e = hash->table[idx]; e != NULL; prev = e, e = e->next ) { 397 | if ( key == e->key || strcmp( key, e->key ) == 0 ) { 398 | if ( prev == NULL ) { 399 | hash->table[idx] = e->next; 400 | } else { 401 | prev->next = e->next; 402 | } 403 | //invoke the release callback function. 404 | if ( rfunc != NULL ) rfunc( e ); 405 | //free the removed node 406 | cel_free( e ); 407 | hash->size--; 408 | return 1; 409 | } 410 | } 411 | 412 | return 0; 413 | } 414 | 415 | //get the value associated with the specified key. 416 | CEL_API int cel_ihashmap_get(cel_ihashmap_t *hash, char *key) 417 | { 418 | cel_hashmap_node_t *e; 419 | uint_t idx = cel_hash(key) % hash->length; 420 | 421 | for ( e = hash->table[idx]; e != NULL; e = e->next ) { 422 | if ( key == e->key || strcmp( key, e->key ) == 0 ) { 423 | return e->value.num; 424 | } 425 | } 426 | 427 | return 0; 428 | } 429 | 430 | //check the existence of the mapping associated with the specified key. 431 | CEL_API int cel_ihashmap_exists(cel_ihashmap_t *hash, char *key) 432 | { 433 | cel_hashmap_node_t *e; 434 | uint_t idx = cel_hash( key ) % hash->length; 435 | 436 | for ( e = hash->table[idx]; e != NULL; e = e->next ) { 437 | if ( key == e->key || strcmp( key, e->key ) == 0 ) { 438 | return 1; 439 | } 440 | } 441 | 442 | return 0; 443 | } 444 | 445 | //replace the value associated with the specified key. 446 | CEL_API int cel_ihashmap_set(cel_ihashmap_t *hash, char *key, int value) 447 | { 448 | cel_hashmap_node_t *e; 449 | uint_t idx = cel_hash( key ) % hash->length; 450 | 451 | for ( e = hash->table[idx]; e != NULL; e = e->next ) { 452 | if ( key == e->key || strcmp( key, e->key ) == 0 ) { 453 | e->value.num = value; 454 | return 1; 455 | } 456 | } 457 | 458 | return 0; 459 | } 460 | /* }}}*/ 461 | -------------------------------------------------------------------------------- /src/cel_hashmap.h: -------------------------------------------------------------------------------- 1 | /** 2 | * hashmap interface header file src/cel_hashmap.h . 3 | * 4 | * @author chenxin 5 | */ 6 | #ifndef _cel_hashmap_h 7 | #define _cel_hashmap_h 8 | 9 | #include "cel_api.h" 10 | 11 | /** 12 | * when you define this marco, 13 | * means: you want to store the hashcode in the hashmap_node_t for 14 | * your program, of course this will make the hashmap 15 | * rebuild faster, otherwise will make more allocations. 16 | * 17 | * comment it for not store the hashcode. 18 | */ 19 | #define CEL_HASHMAP_STORE_HCODE 20 | 21 | //hashmap node struct 22 | struct cel_hashmap_node_struct { 23 | char *key; 24 | //value union 25 | union { 26 | void *ptr; 27 | int num; 28 | } value; 29 | struct cel_hashmap_node_struct *next; 30 | #ifdef CEL_HASHMAP_STORE_HCODE 31 | uint_t hash; 32 | #endif 33 | }; 34 | typedef struct cel_hashmap_node_struct cel_hashmap_node_t; 35 | 36 | //cel hashmap entry 37 | typedef struct { 38 | cel_hashmap_node_t **table; 39 | uint_t length; 40 | uint_t size; 41 | uint_t threshold; 42 | float factor; 43 | } cel_hashmap_t; 44 | 45 | //cel hashmap release callback function pointer 46 | typedef void ( * cel_hashmap_rcb_fn_t ) ( cel_hashmap_node_t * ); 47 | 48 | //quick lanch macro define 49 | #define cel_hashmap_size( map ) map->size 50 | #define cel_hashmap_length( map ) map->length 51 | #define cel_hashmap_factor( map ) map->factor 52 | #define cel_hashmap_threshold( map ) map->threshold 53 | 54 | /* 55 | * create a default cel hashmap with a default 56 | * length and default factor 57 | * 58 | * @return cel_hashmap_t 59 | */ 60 | //CEL_API cel_hashmap_t new_cel_hashmap(); 61 | #define new_cel_hashmap() new_cel_hashmap_opacity( 16, 0.75 ) 62 | 63 | /* 64 | * create a cel hashmap with a specified length and 65 | * factory . 66 | * 67 | * @param uint_t 68 | * @param float 69 | * @return cel_hashmap_t 70 | */ 71 | CEL_API cel_hashmap_t *new_cel_hashmap_opacity( int, float ); 72 | 73 | /* 74 | * free the specified cel hashmap. 75 | * callback function will be invoked for each node, 76 | * if it is not null. 77 | */ 78 | CEL_API void free_cel_hashmap( 79 | cel_hashmap_t **, cel_hashmap_rcb_fn_t ); 80 | 81 | /* 82 | * initialize the specified hashmap 83 | * 84 | * @param cel_hashmap_t * 85 | * @param uint_t the default length of the blocks 86 | * @param float the threshold of the hash map 87 | * @return int 1 for success and 0 for failed 88 | */ 89 | CEL_API int cel_hashmap_init( cel_hashmap_t *, int, float ); 90 | 91 | /* 92 | * destroy the specified hashmap 93 | * 94 | * @param cel_hashmap_t * 95 | * @return 1 for success and 0 for fialed 96 | */ 97 | CEL_API int cel_hashmap_destroy( cel_hashmap_t *, cel_hashmap_rcb_fn_t ); 98 | 99 | /* 100 | * associated the key with the specified value . 101 | * 102 | * @param char * 103 | * @param void * 104 | * @return void * the old the value or NULL 105 | */ 106 | CEL_API void *cel_hashmap_put( cel_hashmap_t *, char *, void * ); 107 | 108 | //remove the mapping associated with the specified key 109 | CEL_API void *cel_hashmap_remove( cel_hashmap_t *, char *, cel_hashmap_rcb_fn_t ); 110 | 111 | //get the value associated with the specified key. 112 | CEL_API void *cel_hashmap_get( cel_hashmap_t *, char * ); 113 | 114 | //check the existence of the mapping associated with the specified key. 115 | CEL_API int cel_hashmap_exists( cel_hashmap_t *, char * ); 116 | 117 | //replace the value associated with the specified key. 118 | CEL_API void *cel_hashmap_set( cel_hashmap_t *, char *, void * ); 119 | 120 | 121 | 122 | 123 | /* {{{ char int hash block::start.*/ 124 | typedef cel_hashmap_t cel_ihashmap_t; 125 | 126 | //quick lanch macro define 127 | #define cel_ihashmap_size( map ) map->size 128 | #define cel_ihashmap_length( map ) map->length 129 | #define cel_ihashmap_factor( map ) map->factor 130 | #define cel_ihashmap_threshold( map ) map->threshold 131 | 132 | //quick interface to create new ihashmap. 133 | #define new_cel_ihashmap() new_cel_hashmap() 134 | #define new_cel_ihashmap_opacity( opacity, factor )\ 135 | new_cel_hashmap_opacity( opacity, factor ) 136 | 137 | /* 138 | * free the specified cel ihashmap. 139 | */ 140 | //CEL_API void free_cel_ihashmap( cel_hashmap_t ); 141 | #define free_cel_ihashmap( map, rfunc ) free_cel_hashmap( map, rfunc ) 142 | 143 | #define cel_ihashmap_init( map, opacity, factor )\ 144 | cel_hashmap_init(map, opacity, factor) 145 | #define cel_ihashmap_destroy( map )\ 146 | cel_hashmap_destroy(map, NULL) 147 | 148 | /* 149 | * associated the key with the specified value . 150 | * 151 | * @param char * 152 | * @param int 153 | * @return int (0 for fail and 1 for true) 154 | */ 155 | CEL_API int cel_ihashmap_put( cel_ihashmap_t *, char *, int ); 156 | 157 | //remove the mapping associated with the specified key 158 | CEL_API int cel_ihashmap_remove( cel_ihashmap_t *, char *, cel_hashmap_rcb_fn_t ); 159 | 160 | //get the value associated with the specified key. 161 | CEL_API int cel_ihashmap_get( cel_ihashmap_t *, char * ); 162 | 163 | //check the existence of the mapping associated with the specified key. 164 | CEL_API int cel_ihashmap_exists( cel_ihashmap_t *, char * ); 165 | 166 | //replace the value associated with the specified key. 167 | CEL_API int cel_ihashmap_set( cel_ihashmap_t *, char *, int ); 168 | /*}}}*/ 169 | #endif /*end ifndef*/ 170 | -------------------------------------------------------------------------------- /src/cel_link.c: -------------------------------------------------------------------------------- 1 | /** 2 | * link interface implemented source file src/cel_link.c 3 | * 4 | * @author chenxin 5 | */ 6 | #include "cel_link.h" 7 | 8 | static cel_link_node_t *create_link_node( void *, cel_link_node_t *, cel_link_node_t * ); 9 | static void insert_before( cel_link_node_t *, void * ); 10 | static cel_link_node_t *get_node( cel_link_t *, uint_t ); 11 | //static inline void cel_remove_node( cel_link_node_t * ); 12 | 13 | #define cel_remove_node( node ) \ 14 | node->_prev->_next = node->_next;\ 15 | node->_next->_prev = node->_prev; 16 | 17 | /** 18 | * create a new cel link list . 19 | * 20 | * @return cel_link_t 21 | */ 22 | CEL_API cel_link_t *new_cel_link( void ) 23 | { 24 | cel_link_t *ptr = (cel_link_t *) cel_malloc(sizeof(cel_link_t)); 25 | if ( ptr == NULL ) { 26 | CEL_ALLOCATE_ERROR("new_cel_link", sizeof(cel_link_t)); 27 | } 28 | 29 | if ( cel_link_init(ptr) == 0 ) { 30 | cel_free(ptr); 31 | CEL_ALLOCATE_ERROR("new_cel_link", sizeof(cel_link_node_t)); 32 | } 33 | 34 | return ptr; 35 | } 36 | 37 | /** 38 | * free the specified cel link list, 39 | * the remove callback function maybe pass insite to 40 | * finish the extra allocation free work . 41 | * 42 | * @param rmfunc 43 | */ 44 | CEL_API void free_cel_link(cel_link_t **link, cel_release_callback_fn_t relfunc) 45 | { 46 | if ( link == NULL ) return; 47 | if ( *link != NULL ) { 48 | cel_link_destroy(*link, relfunc); 49 | cel_free( *link ); 50 | *link = NULL; 51 | } 52 | } 53 | 54 | /* 55 | * create the specified cel_link_t 56 | * 57 | * @param cel_link_t * 58 | * @return int 1 for success and 0 for failed 59 | */ 60 | CEL_API int cel_link_init( cel_link_t *link ) 61 | { 62 | link->size = 0; 63 | link->tail = create_link_node( NULL, NULL, NULL ); 64 | if ( link->tail == NULL ) return 0; 65 | 66 | link->head = create_link_node( NULL, NULL, link->tail ); 67 | if ( link->head == NULL ) { 68 | cel_free(link->tail); 69 | return 0; 70 | } 71 | 72 | link->tail->_prev = link->head; 73 | 74 | return 1; 75 | } 76 | 77 | /* 78 | * destroy the specified cel_link_t 79 | * 80 | * @param cel_link_t * 81 | * @return int 1 for success and 0 for failed 82 | */ 83 | CEL_API int cel_link_destroy(cel_link_t *link, cel_release_callback_fn_t relfunc) 84 | { 85 | cel_link_node_t *node, *_next; 86 | 87 | if ( link != NULL ) { 88 | for ( node = link->head->_next; node != link->tail; ) { 89 | // backup the _next 90 | _next = node->_next; 91 | //invoke the callback function if it is not NULL 92 | if ( relfunc != NULL ) relfunc( node->value ); 93 | cel_free( node ); 94 | node = _next; 95 | } 96 | 97 | cel_free( link->head ); 98 | link->head = NULL; 99 | 100 | cel_free( link->tail ); 101 | link->tail = NULL; 102 | } 103 | 104 | return 1; 105 | } 106 | 107 | /** 108 | * create a new link_node_entry . 109 | * 110 | * @param value 111 | * @param _prev 112 | * @param _next 113 | * @return cel_link_node_t 114 | */ 115 | static cel_link_node_t *create_link_node( 116 | void * value, cel_link_node_t *_prev, cel_link_node_t *_next) 117 | { 118 | cel_link_node_t *node = (cel_link_node_t *) cel_malloc(sizeof(cel_link_node_t)); 119 | if ( node == NULL ) return NULL; 120 | 121 | node->value = value; 122 | node->_prev = _prev; 123 | node->_next = _next; 124 | 125 | return node; 126 | } 127 | 128 | 129 | CEL_API void cel_link_add( cel_link_t *link, void *value ) 130 | { 131 | insert_before( link->tail, value ); 132 | link->size++; 133 | } 134 | 135 | //add a element from the head . 136 | CEL_API void cel_link_add_first( cel_link_t *link, void *value ) 137 | { 138 | insert_before( link->head->_next, value ); 139 | link->size++; 140 | } 141 | 142 | //append element from the tail 143 | CEL_API void cel_link_add_last( cel_link_t *link, void *value ) 144 | { 145 | insert_before( link->tail, value ); 146 | link->size++; 147 | } 148 | 149 | /** 150 | * insert a element before the specified node . 151 | * 152 | * @param _old 153 | * @param value 154 | */ 155 | static void insert_before( cel_link_node_t *_old, void *value ) 156 | { 157 | cel_link_node_t *o = create_link_node( value, _old->_prev, _old ); 158 | if ( o != NULL ) { 159 | o->_prev->_next = o; 160 | o->_next->_prev = o; 161 | } 162 | } 163 | 164 | /** 165 | * insert the element at a specified position . 166 | * 167 | * @param uint_t 168 | * @param void * 169 | */ 170 | CEL_API void cel_link_insert( cel_link_t *link, uint_t idx, void *value ) 171 | { 172 | cel_link_node_t *o = get_node( link, idx ); 173 | if ( o != NULL ) { 174 | insert_before( o, value ); 175 | link->size++; 176 | } 177 | } 178 | 179 | /** 180 | * get the node at a specified position . 181 | * 182 | * @param link 183 | * @param idx 184 | */ 185 | static cel_link_node_t *get_node( cel_link_t *link, uint_t idx ) 186 | { 187 | register uint_t i; 188 | cel_link_node_t *p; 189 | 190 | if ( idx >= link->size ) return NULL; 191 | 192 | //find the node 193 | if ( idx > link->size / 2 ) { 194 | p = link->tail; 195 | for ( i = link->size; i > idx; i-- ) { 196 | p = p->_prev; 197 | } 198 | } else { 199 | p = link->head; 200 | for ( i = 0; i <= idx; i++ ) { 201 | p = p->_next; 202 | } 203 | } 204 | 205 | return p; 206 | } 207 | 208 | /** 209 | * remove the element at a specified position . 210 | * 211 | * @param uint_t 212 | * @return void * 213 | */ 214 | CEL_API void *cel_link_remove( cel_link_t *link, uint_t idx ) 215 | { 216 | void * v; 217 | cel_link_node_t *o = get_node( link, idx ); 218 | if ( o == NULL ) return NULL; 219 | 220 | //remove the node 221 | v = o->value; 222 | cel_remove_node(o); 223 | cel_free(o); 224 | 225 | link->size--; 226 | return v; 227 | } 228 | 229 | //static void cel_remove_node( cel_link_node_t node ) 230 | //{ 231 | // node->_prev->_next = node->_next; 232 | // node->_next->_prev = node->_prev; 233 | //} 234 | 235 | //remove the element from the head 236 | CEL_API void *cel_link_remove_first( cel_link_t *link ) 237 | { 238 | void * v; 239 | cel_link_node_t *o; 240 | 241 | if ( link->size == 0 ) return NULL; 242 | 243 | o = link->head->_next; 244 | v = o->value; 245 | cel_remove_node(o); 246 | cel_free(o); //free the link node 247 | link->size--; 248 | 249 | return v; 250 | } 251 | 252 | //remove the element from the tail 253 | CEL_API void *cel_link_remove_last( cel_link_t *link ) 254 | { 255 | void * v; 256 | cel_link_node_t *o; 257 | 258 | if ( link->size == 0 ) return NULL; 259 | 260 | o = link->tail->_prev; 261 | v = o->value; 262 | cel_remove_node(o); 263 | cel_free(o); //free the link node 264 | link->size--; 265 | 266 | return v; 267 | } 268 | 269 | /** 270 | * remove the element equals the given one . 271 | * 272 | * @param void * 273 | * @param cel_compare_fn_t 274 | * @return void * 275 | */ 276 | CEL_API void *cel_link_remove_obj(cel_link_t *link, void *value, cel_compare_fn_t comp) 277 | { 278 | void * v; 279 | cel_link_node_t *o; 280 | 281 | for ( o = link->head->_next; o != link->tail; o = o->_next ) { 282 | if ( value == o->value || comp( value, o->value ) == 0 ) { 283 | v = o->value; 284 | cel_remove_node(o); 285 | cel_free(o); 286 | link->size--; 287 | return v; 288 | } 289 | } 290 | 291 | return NULL; 292 | } 293 | 294 | //get the element at the specified postion. 295 | CEL_API void *cel_link_get( cel_link_t *link, uint_t idx ) 296 | { 297 | cel_link_node_t *o = get_node( link, idx ); 298 | if ( o == NULL ) return NULL; 299 | return o->value; 300 | } 301 | 302 | /** 303 | * set the element at the specified position with 304 | * the specified value . 305 | * 306 | * @param uint_t 307 | * @param void * 308 | * @return void * 309 | */ 310 | CEL_API void *cel_link_set(cel_link_t *link, uint_t idx, void *value) 311 | { 312 | void *v; 313 | cel_link_node_t *old = get_node( link, idx ); 314 | 315 | if ( old == NULL ) return NULL; 316 | 317 | //bak the old value 318 | v = old->value; 319 | old->value = value; 320 | 321 | return v; 322 | } 323 | 324 | //search the element and replace it with a new one. 325 | CEL_API void * cel_link_set_obj( 326 | cel_link_t *link, void *search, void *value, cel_compare_fn_t comp) 327 | { 328 | void * v; 329 | cel_link_node_t *o; 330 | 331 | for ( o = link->head->_next; o != link->tail; o = o->_next ) { 332 | if ( search == o->value || comp( search, o->value ) == 0 ) { 333 | v = o->value; 334 | o->value = value; 335 | return v; 336 | } 337 | } 338 | 339 | return NULL; 340 | } 341 | -------------------------------------------------------------------------------- /src/cel_link.h: -------------------------------------------------------------------------------- 1 | /** 2 | * link list interface header file src/cel_link.h. 3 | * 4 | * @author chenxin 5 | */ 6 | #ifndef _cel_link_h 7 | #define _cel_link_h 8 | 9 | #include "cel_api.h" 10 | 11 | struct cel_link_node_struct { 12 | void *value; 13 | struct cel_link_node_struct *_prev; 14 | struct cel_link_node_struct *_next; 15 | }; 16 | typedef struct cel_link_node_struct cel_link_node_t; 17 | 18 | typedef struct { 19 | uint_t size; 20 | cel_link_node_t *head; 21 | cel_link_node_t *tail; 22 | } cel_link_t; 23 | 24 | /** 25 | * create a new cel link list . 26 | * 27 | * @return cel_link_t 28 | */ 29 | CEL_API cel_link_t *new_cel_link( void ); 30 | 31 | /** 32 | * free the specified link list . 33 | * 34 | * @param cel_link_t * 35 | */ 36 | CEL_API void free_cel_link( cel_link_t **, cel_release_callback_fn_t ); 37 | 38 | /* 39 | * create the specified cel_link_t 40 | * 41 | * @param cel_link_t * 42 | * @return int 1 for success and 0 for failed 43 | */ 44 | CEL_API int cel_link_init( cel_link_t * ); 45 | 46 | /* 47 | * destroy the specified cel_link_t 48 | * 49 | * @param cel_link_t * 50 | * @return int 1 for success and 0 for failed 51 | */ 52 | CEL_API int cel_link_destroy( cel_link_t *, cel_release_callback_fn_t ); 53 | 54 | //add a element from the head . 55 | CEL_API void cel_link_add_first( cel_link_t *, void * ); 56 | 57 | //append element from the tail 58 | CEL_API void cel_link_add_last( cel_link_t *, void * ); 59 | 60 | /** 61 | * insert the element at a specified position . 62 | * 63 | * @param uint_t 64 | * @param void * 65 | */ 66 | CEL_API void cel_link_insert( cel_link_t *, uint_t, void * ); 67 | 68 | /** 69 | * remove the element at a specified position . 70 | * 71 | * @param uint_t 72 | * @return void * 73 | */ 74 | CEL_API void *cel_link_remove( cel_link_t *, uint_t ); 75 | 76 | //remove the element from the head 77 | CEL_API void *cel_link_remove_first( cel_link_t * ); 78 | 79 | //remove the element from the tail 80 | CEL_API void *cel_link_remove_last( cel_link_t * ); 81 | 82 | /** 83 | * remove the element equals the given one . 84 | * 85 | * @param void * 86 | * @param cel_compare_fn_t 87 | * @return void * 88 | */ 89 | CEL_API void *cel_link_remove_obj( cel_link_t *, void *, cel_compare_fn_t ); 90 | 91 | //get the element at the specified postion. 92 | CEL_API void *cel_link_get( cel_link_t *, uint_t ); 93 | 94 | /** 95 | * set the element at the specified position with 96 | * the specified value . 97 | * 98 | * @param uint_t 99 | * @param void * 100 | * @return void * 101 | */ 102 | CEL_API void *cel_link_set( cel_link_t *, uint_t, void * ); 103 | 104 | //search the element and replace it with a new one. 105 | CEL_API void *cel_link_set_obj( cel_link_t *, void *, void *, cel_compare_fn_t ); 106 | 107 | //link struct quick getter macro define 108 | #define cel_link_size( link ) link->size 109 | #define cel_link_head( link ) link->head 110 | #define cel_link_tail( link ) link->tail 111 | 112 | #endif /*end ifndef*/ 113 | 114 | -------------------------------------------------------------------------------- /src/cel_math.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel math module implemented functions. 3 | * @package src/cel_math.c 4 | * 5 | * @author chenxin 6 | */ 7 | #include "cel_math.h" 8 | 9 | 10 | /** 11 | * get the next prime of the specified numeric. 12 | * 13 | * @param n 14 | * @return int 15 | */ 16 | CEL_API int cel_next_prime( int n ) 17 | { 18 | if ( n % 2 == 0 ) n++; 19 | for ( ; ! cel_is_prime( n ); n = n + 2 ) ; 20 | 21 | return n; 22 | } 23 | 24 | /** 25 | * check the specified numeric is a prime. 26 | * 27 | * @param n 28 | * @return int 29 | */ 30 | CEL_API int cel_is_prime( int n ) 31 | { 32 | int j; 33 | 34 | //simple check 35 | if ( n == 2 || n == 3 ) 36 | return 1; 37 | 38 | if ( n <= 1 || n % 2 == 0 ) 39 | return 0; 40 | 41 | //loop check 42 | for ( j = 3; j * j < n; j++ ) 43 | if ( n % j == 0 ) return 0; 44 | 45 | return 1; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /src/cel_math.h: -------------------------------------------------------------------------------- 1 | /** 2 | * cel math module interface header file. 3 | * @package src/cel_math.h. 4 | * 5 | * @author chenxin 6 | */ 7 | #ifndef _cel_math_h 8 | #define _cel_math_h 9 | 10 | #include "cel_api.h" 11 | 12 | /** 13 | * get the next prime of the specified numeric. 14 | * 15 | * @param int 16 | * @return int 17 | */ 18 | CEL_API int cel_next_prime( int ); 19 | 20 | /** 21 | * check the specified numeric is a prime. 22 | * 23 | * @param int 24 | * @return int 25 | */ 26 | CEL_API int cel_is_prime( int ); 27 | 28 | #endif /*end ifndef*/ 29 | -------------------------------------------------------------------------------- /src/cel_md5.c: -------------------------------------------------------------------------------- 1 | /** 2 | * md5 (message-digest algorithm) implemented source file. 3 | * @package src/cel_md5.c 4 | * 5 | * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All 6 | * rights reserved. 7 | * 8 | * License to copy and use this software is granted provided that it 9 | * is identified as the "RSA Data Security, Inc. MD5 Message-Digest 10 | * Algorithm" in all material mentioning or referencing this software 11 | * or this function. 12 | * 13 | * License is also granted to make and use derivative works provided 14 | * that such works are identified as "derived from the RSA Data 15 | * Security, Inc. MD5 Message-Digest Algorithm" in all material 16 | * mentioning or referencing the derived work. 17 | * 18 | * RSA Data Security, Inc. makes no representations concerning either 19 | * the merchantability of this software or the suitability of this 20 | * software for any particular purpose. It is provided "as is" 21 | * without express or implied warranty of any kind. 22 | * These notices must be retained in any copies of any part of this 23 | * documentation and/or software. 24 | * 25 | * Take the implementation from 26 | * L. Peter Deutsch at https://sourceforge.net/projects/libmd5-rfc/ 27 | * 28 | */ 29 | #include "cel_hash.h" 30 | #include 31 | #include 32 | 33 | #undef BYTE_ORDER /* 1 = big-endian, -1 = little-endian, 0 = unknown */ 34 | #ifdef ARCH_IS_BIG_ENDIAN 35 | # define BYTE_ORDER (ARCH_IS_BIG_ENDIAN ? 1 : -1) 36 | #else 37 | # define BYTE_ORDER 0 38 | #endif 39 | 40 | #define T_MASK ((uint_t)~0) 41 | #define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87) 42 | #define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9) 43 | #define T3 0x242070db 44 | #define T4 /* 0xc1bdceee */ (T_MASK ^ 0x3e423111) 45 | #define T5 /* 0xf57c0faf */ (T_MASK ^ 0x0a83f050) 46 | #define T6 0x4787c62a 47 | #define T7 /* 0xa8304613 */ (T_MASK ^ 0x57cfb9ec) 48 | #define T8 /* 0xfd469501 */ (T_MASK ^ 0x02b96afe) 49 | #define T9 0x698098d8 50 | #define T10 /* 0x8b44f7af */ (T_MASK ^ 0x74bb0850) 51 | #define T11 /* 0xffff5bb1 */ (T_MASK ^ 0x0000a44e) 52 | #define T12 /* 0x895cd7be */ (T_MASK ^ 0x76a32841) 53 | #define T13 0x6b901122 54 | #define T14 /* 0xfd987193 */ (T_MASK ^ 0x02678e6c) 55 | #define T15 /* 0xa679438e */ (T_MASK ^ 0x5986bc71) 56 | #define T16 0x49b40821 57 | #define T17 /* 0xf61e2562 */ (T_MASK ^ 0x09e1da9d) 58 | #define T18 /* 0xc040b340 */ (T_MASK ^ 0x3fbf4cbf) 59 | #define T19 0x265e5a51 60 | #define T20 /* 0xe9b6c7aa */ (T_MASK ^ 0x16493855) 61 | #define T21 /* 0xd62f105d */ (T_MASK ^ 0x29d0efa2) 62 | #define T22 0x02441453 63 | #define T23 /* 0xd8a1e681 */ (T_MASK ^ 0x275e197e) 64 | #define T24 /* 0xe7d3fbc8 */ (T_MASK ^ 0x182c0437) 65 | #define T25 0x21e1cde6 66 | #define T26 /* 0xc33707d6 */ (T_MASK ^ 0x3cc8f829) 67 | #define T27 /* 0xf4d50d87 */ (T_MASK ^ 0x0b2af278) 68 | #define T28 0x455a14ed 69 | #define T29 /* 0xa9e3e905 */ (T_MASK ^ 0x561c16fa) 70 | #define T30 /* 0xfcefa3f8 */ (T_MASK ^ 0x03105c07) 71 | #define T31 0x676f02d9 72 | #define T32 /* 0x8d2a4c8a */ (T_MASK ^ 0x72d5b375) 73 | #define T33 /* 0xfffa3942 */ (T_MASK ^ 0x0005c6bd) 74 | #define T34 /* 0x8771f681 */ (T_MASK ^ 0x788e097e) 75 | #define T35 0x6d9d6122 76 | #define T36 /* 0xfde5380c */ (T_MASK ^ 0x021ac7f3) 77 | #define T37 /* 0xa4beea44 */ (T_MASK ^ 0x5b4115bb) 78 | #define T38 0x4bdecfa9 79 | #define T39 /* 0xf6bb4b60 */ (T_MASK ^ 0x0944b49f) 80 | #define T40 /* 0xbebfbc70 */ (T_MASK ^ 0x4140438f) 81 | #define T41 0x289b7ec6 82 | #define T42 /* 0xeaa127fa */ (T_MASK ^ 0x155ed805) 83 | #define T43 /* 0xd4ef3085 */ (T_MASK ^ 0x2b10cf7a) 84 | #define T44 0x04881d05 85 | #define T45 /* 0xd9d4d039 */ (T_MASK ^ 0x262b2fc6) 86 | #define T46 /* 0xe6db99e5 */ (T_MASK ^ 0x1924661a) 87 | #define T47 0x1fa27cf8 88 | #define T48 /* 0xc4ac5665 */ (T_MASK ^ 0x3b53a99a) 89 | #define T49 /* 0xf4292244 */ (T_MASK ^ 0x0bd6ddbb) 90 | #define T50 0x432aff97 91 | #define T51 /* 0xab9423a7 */ (T_MASK ^ 0x546bdc58) 92 | #define T52 /* 0xfc93a039 */ (T_MASK ^ 0x036c5fc6) 93 | #define T53 0x655b59c3 94 | #define T54 /* 0x8f0ccc92 */ (T_MASK ^ 0x70f3336d) 95 | #define T55 /* 0xffeff47d */ (T_MASK ^ 0x00100b82) 96 | #define T56 /* 0x85845dd1 */ (T_MASK ^ 0x7a7ba22e) 97 | #define T57 0x6fa87e4f 98 | #define T58 /* 0xfe2ce6e0 */ (T_MASK ^ 0x01d3191f) 99 | #define T59 /* 0xa3014314 */ (T_MASK ^ 0x5cfebceb) 100 | #define T60 0x4e0811a1 101 | #define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d) 102 | #define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca) 103 | #define T63 0x2ad7d2bb 104 | #define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e) 105 | 106 | static void 107 | md5_process(cel_md5_t *pms, const uchar_t *data /*[64]*/) 108 | { 109 | uint_t a = pms->abcd[0], b = pms->abcd[1]; 110 | uint_t c = pms->abcd[2], d = pms->abcd[3]; 111 | uint_t t; 112 | #if BYTE_ORDER > 0 113 | /* Define storage only for big-endian CPUs. */ 114 | uint_t X[16]; 115 | #else 116 | /* Define storage for little-endian or both types of CPUs. */ 117 | uint_t xbuf[16]; 118 | const uint_t *X; 119 | #endif 120 | 121 | { 122 | #if BYTE_ORDER == 0 123 | /* 124 | * Determine dynamically whether this is a big-endian or 125 | * little-endian machine, since we can use a more efficient 126 | * algorithm on the latter. 127 | */ 128 | static const int w = 1; 129 | 130 | if (*((const uchar_t *)&w)) /* dynamic little-endian */ 131 | #endif 132 | #if BYTE_ORDER <= 0 /* little-endian */ 133 | { 134 | /* 135 | * On little-endian machines, we can process properly aligned 136 | * data without copying it. 137 | */ 138 | if (!((data - (const uchar_t *)0) & 3)) { 139 | /* data are properly aligned */ 140 | X = (const uint_t *)data; 141 | } else { 142 | /* not aligned */ 143 | memcpy(xbuf, data, 64); 144 | X = xbuf; 145 | } 146 | } 147 | #endif 148 | #if BYTE_ORDER == 0 149 | else /* dynamic big-endian */ 150 | #endif 151 | #if BYTE_ORDER >= 0 /* big-endian */ 152 | { 153 | /* 154 | * On big-endian machines, we must arrange the bytes in the 155 | * right order. 156 | */ 157 | const uchar_t *xp = data; 158 | int i; 159 | 160 | # if BYTE_ORDER == 0 161 | X = xbuf; /* (dynamic only) */ 162 | # else 163 | # define xbuf X /* (static only) */ 164 | # endif 165 | for (i = 0; i < 16; ++i, xp += 4) { 166 | xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24); 167 | } 168 | } 169 | #endif 170 | } 171 | 172 | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) 173 | 174 | /* Round 1. */ 175 | /* Let [abcd k s i] denote the operation 176 | a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */ 177 | #define F(x, y, z) (((x) & (y)) | (~(x) & (z))) 178 | #define SET(a, b, c, d, k, s, Ti)\ 179 | t = a + F(b,c,d) + X[k] + Ti;\ 180 | a = ROTATE_LEFT(t, s) + b 181 | /* Do the following 16 operations. */ 182 | SET(a, b, c, d, 0, 7, T1); 183 | SET(d, a, b, c, 1, 12, T2); 184 | SET(c, d, a, b, 2, 17, T3); 185 | SET(b, c, d, a, 3, 22, T4); 186 | SET(a, b, c, d, 4, 7, T5); 187 | SET(d, a, b, c, 5, 12, T6); 188 | SET(c, d, a, b, 6, 17, T7); 189 | SET(b, c, d, a, 7, 22, T8); 190 | SET(a, b, c, d, 8, 7, T9); 191 | SET(d, a, b, c, 9, 12, T10); 192 | SET(c, d, a, b, 10, 17, T11); 193 | SET(b, c, d, a, 11, 22, T12); 194 | SET(a, b, c, d, 12, 7, T13); 195 | SET(d, a, b, c, 13, 12, T14); 196 | SET(c, d, a, b, 14, 17, T15); 197 | SET(b, c, d, a, 15, 22, T16); 198 | #undef SET 199 | 200 | /* Round 2. */ 201 | /* Let [abcd k s i] denote the operation 202 | a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */ 203 | #define G(x, y, z) (((x) & (z)) | ((y) & ~(z))) 204 | #define SET(a, b, c, d, k, s, Ti)\ 205 | t = a + G(b,c,d) + X[k] + Ti;\ 206 | a = ROTATE_LEFT(t, s) + b 207 | /* Do the following 16 operations. */ 208 | SET(a, b, c, d, 1, 5, T17); 209 | SET(d, a, b, c, 6, 9, T18); 210 | SET(c, d, a, b, 11, 14, T19); 211 | SET(b, c, d, a, 0, 20, T20); 212 | SET(a, b, c, d, 5, 5, T21); 213 | SET(d, a, b, c, 10, 9, T22); 214 | SET(c, d, a, b, 15, 14, T23); 215 | SET(b, c, d, a, 4, 20, T24); 216 | SET(a, b, c, d, 9, 5, T25); 217 | SET(d, a, b, c, 14, 9, T26); 218 | SET(c, d, a, b, 3, 14, T27); 219 | SET(b, c, d, a, 8, 20, T28); 220 | SET(a, b, c, d, 13, 5, T29); 221 | SET(d, a, b, c, 2, 9, T30); 222 | SET(c, d, a, b, 7, 14, T31); 223 | SET(b, c, d, a, 12, 20, T32); 224 | #undef SET 225 | 226 | /* Round 3. */ 227 | /* Let [abcd k s t] denote the operation 228 | a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */ 229 | #define H(x, y, z) ((x) ^ (y) ^ (z)) 230 | #define SET(a, b, c, d, k, s, Ti)\ 231 | t = a + H(b,c,d) + X[k] + Ti;\ 232 | a = ROTATE_LEFT(t, s) + b 233 | /* Do the following 16 operations. */ 234 | SET(a, b, c, d, 5, 4, T33); 235 | SET(d, a, b, c, 8, 11, T34); 236 | SET(c, d, a, b, 11, 16, T35); 237 | SET(b, c, d, a, 14, 23, T36); 238 | SET(a, b, c, d, 1, 4, T37); 239 | SET(d, a, b, c, 4, 11, T38); 240 | SET(c, d, a, b, 7, 16, T39); 241 | SET(b, c, d, a, 10, 23, T40); 242 | SET(a, b, c, d, 13, 4, T41); 243 | SET(d, a, b, c, 0, 11, T42); 244 | SET(c, d, a, b, 3, 16, T43); 245 | SET(b, c, d, a, 6, 23, T44); 246 | SET(a, b, c, d, 9, 4, T45); 247 | SET(d, a, b, c, 12, 11, T46); 248 | SET(c, d, a, b, 15, 16, T47); 249 | SET(b, c, d, a, 2, 23, T48); 250 | #undef SET 251 | 252 | /* Round 4. */ 253 | /* Let [abcd k s t] denote the operation 254 | a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */ 255 | #define I(x, y, z) ((y) ^ ((x) | ~(z))) 256 | #define SET(a, b, c, d, k, s, Ti)\ 257 | t = a + I(b,c,d) + X[k] + Ti;\ 258 | a = ROTATE_LEFT(t, s) + b 259 | /* Do the following 16 operations. */ 260 | SET(a, b, c, d, 0, 6, T49); 261 | SET(d, a, b, c, 7, 10, T50); 262 | SET(c, d, a, b, 14, 15, T51); 263 | SET(b, c, d, a, 5, 21, T52); 264 | SET(a, b, c, d, 12, 6, T53); 265 | SET(d, a, b, c, 3, 10, T54); 266 | SET(c, d, a, b, 10, 15, T55); 267 | SET(b, c, d, a, 1, 21, T56); 268 | SET(a, b, c, d, 8, 6, T57); 269 | SET(d, a, b, c, 15, 10, T58); 270 | SET(c, d, a, b, 6, 15, T59); 271 | SET(b, c, d, a, 13, 21, T60); 272 | SET(a, b, c, d, 4, 6, T61); 273 | SET(d, a, b, c, 11, 10, T62); 274 | SET(c, d, a, b, 2, 15, T63); 275 | SET(b, c, d, a, 9, 21, T64); 276 | #undef SET 277 | 278 | /* Then perform the following additions. (That is increment each 279 | of the four registers by the value it had before this block 280 | was started.) */ 281 | pms->abcd[0] += a; 282 | pms->abcd[1] += b; 283 | pms->abcd[2] += c; 284 | pms->abcd[3] += d; 285 | } 286 | 287 | 288 | 289 | void cel_md5_init(cel_md5_t *pms) 290 | { 291 | pms->count[0] = pms->count[1] = 0; 292 | pms->abcd[0] = 0x67452301; 293 | pms->abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476; 294 | pms->abcd[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301; 295 | pms->abcd[3] = 0x10325476; 296 | } 297 | 298 | CEL_API void cel_md5_update(cel_md5_t *pms, const uchar_t *data, int nbytes) 299 | { 300 | const uchar_t *p = data; 301 | int left = nbytes; 302 | int offset = (pms->count[0] >> 3) & 63; 303 | uint_t nbits = (uint_t)(nbytes << 3); 304 | 305 | if (nbytes <= 0) { 306 | return; 307 | } 308 | 309 | /* Update the message length. */ 310 | pms->count[1] += nbytes >> 29; 311 | pms->count[0] += nbits; 312 | if (pms->count[0] < nbits) { 313 | pms->count[1]++; 314 | } 315 | 316 | /* Process an initial partial block. */ 317 | if (offset) { 318 | int copy = (offset + nbytes > 64 ? 64 - offset : nbytes); 319 | 320 | memcpy(pms->buf + offset, p, copy); 321 | if (offset + copy < 64) { 322 | return; 323 | } 324 | 325 | p += copy; 326 | left -= copy; 327 | md5_process(pms, pms->buf); 328 | } 329 | 330 | /* Process full blocks. */ 331 | for (; left >= 64; p += 64, left -= 64) { 332 | md5_process(pms, p); 333 | } 334 | 335 | /* Process a final partial block. */ 336 | if (left) { 337 | memcpy(pms->buf, p, left); 338 | } 339 | } 340 | 341 | CEL_API void cel_md5_final(cel_md5_t *pms, uchar_t digest[16]) 342 | { 343 | static const uchar_t pad[64] = { 344 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 345 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 346 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 347 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 348 | }; 349 | uchar_t data[8]; 350 | int i; 351 | 352 | /* Save the length before padding. */ 353 | for (i = 0; i < 8; ++i) { 354 | data[i] = (uchar_t)(pms->count[i >> 2] >> ((i & 3) << 3)); 355 | } 356 | 357 | /* Pad to 56 bytes mod 64. */ 358 | cel_md5_update(pms, pad, ((55 - (pms->count[0] >> 3)) & 63) + 1); 359 | /* Append the length. */ 360 | cel_md5_update(pms, data, 8); 361 | 362 | for (i = 0; i < 16; ++i) { 363 | digest[i] = (uchar_t)(pms->abcd[i >> 2] >> ((i & 3) << 3)); 364 | } 365 | } 366 | 367 | /* 368 | * MD5 hash algorithm. Message digest Algorithm 5. 369 | * 370 | * Digest a string and get the results. 371 | */ 372 | CEL_API void cel_md5_string(const cstring str, uchar_t digest[16]) 373 | { 374 | cel_md5_t context; 375 | uint_t len = strlen(str); 376 | 377 | cel_md5_init(&context); 378 | cel_md5_update(&context, (uchar_t *) str, len); 379 | cel_md5_final(&context, digest); 380 | } 381 | 382 | /* Digest a file and get the result. 383 | * */ 384 | CEL_API int cel_md5_file(const cstring filename, uchar_t digest[16]) 385 | { 386 | cel_md5_t context; 387 | FILE * file; 388 | int len; 389 | uchar_t buffer[1024]; 390 | 391 | if ( (file = fopen(filename, "rb")) == NULL ) { 392 | return -1; 393 | } 394 | 395 | cel_md5_init(&context); 396 | while ( (len = (int)fread(buffer, 1, 1024, file)) > 0 ) { 397 | cel_md5_update(&context, buffer, len); 398 | } 399 | 400 | cel_md5_final(&context, digest); 401 | 402 | //close the file 403 | fclose( file ); 404 | 405 | return 0; 406 | } 407 | 408 | CEL_API void cel_md5_print(uchar_t digest[16], char buffer[32]) 409 | { 410 | int i; 411 | for ( i = 0; i < 16; i++ ) { 412 | sprintf(buffer + i * 2, "%02x", digest[i]); 413 | } 414 | } 415 | -------------------------------------------------------------------------------- /src/cel_sort.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel sort module implements functions. 3 | * @package src/cel_sort.c 4 | * 5 | * @author chenxin 6 | */ 7 | #include "cel_sort.h" 8 | #include "cel_stdio.h" 9 | #include 10 | #include 11 | #include 12 | 13 | /** 14 | * insertion sort algorithm. 15 | * 16 | * @param a - the array to sort. 17 | * @param size - the bytes that one element in the array takes. 18 | * @param comp - the compare function. 19 | * @param start - the start offset. 20 | * @param end - the end offset. 21 | */ 22 | CEL_API void cel_subinsert_sort( 23 | void *a, uint_t size, cel_compare_fn_t comp, uint_t start, uint_t end ) 24 | { 25 | uchar_t * tmp = ( uchar_t * ) cel_malloc(size); 26 | uchar_t * ptr = ( uchar_t * ) a; 27 | uchar_t * p; 28 | 29 | register int i, j; 30 | 31 | if ( tmp == NULL ) return; 32 | 33 | //printf("size: %d, start: %d, end: %d\n", size, start, end); 34 | 35 | for ( i = start + 1; i <= end; i++ ) { 36 | cel_mem_copy( ptr + i * size, tmp, size ); 37 | //find the right index for tmp. 38 | for ( j = i; j > start && comp(tmp, (p = ptr + (j - 1) * size)) < 0; j-- ) { 39 | cel_mem_copy( p, p + size, size ); 40 | } 41 | 42 | //Copy the element to the right position. 43 | if ( j < i ) { 44 | cel_mem_copy( tmp, ptr + j * size, size ); 45 | } 46 | } 47 | 48 | //free the tmp allocations. 49 | cel_free(tmp); 50 | } 51 | 52 | 53 | /** 54 | * gaps array for shell sort. 55 | * this array was generated with 56 | * 9 * pow(4, j) - 9 * pow(2, j) + 1 57 | * and 58 | * pow(4, j) - 3 * pow(2, j) + 1 . 59 | */ 60 | static const int gaps[] = { 61 | 1, 5, 62 | 19, 41, 63 | 109, 209, 505, 929, 64 | 2161, 8929, 65 | 16001, 36289, 64769, //10000th 66 | 146305, 260609, 587521, 67 | 1045505, 2354689, 4188161, 9427969, 68 | 16764929, 37730305, 67084289, 69 | 150958081, 268386305, 603906049, 70 | 1073643521, 2147483647 71 | }; 72 | 73 | 74 | /** 75 | * shell sort algorithm. 76 | * 77 | * @param a - the array to sort. 78 | * @param length - the length of the array. 79 | * @param size - the bytes that one element in the array takes. 80 | * @param comp - comprare function. 81 | */ 82 | CEL_API void cel_shell_sort( 83 | void *a, uint_t length, uint_t size, cel_compare_fn_t comp) 84 | { 85 | 86 | uchar_t * tmp = ( uchar_t * ) malloc( size ); 87 | uchar_t * ptr = ( uchar_t * ) a; 88 | uchar_t * p; 89 | 90 | register int i, j, gap; 91 | register int k = 0; 92 | 93 | if ( tmp == NULL ) return; 94 | 95 | while ( gaps[k] < length ) k++; 96 | 97 | while ( --k >= 0 ) { 98 | //Get the gap 99 | gap = gaps[k]; 100 | for ( i = gap; i < length; i++ ) { 101 | cel_mem_copy( ptr + i * size, tmp, size ); 102 | //find the right index of the tmp 103 | for ( j = i; j >= gap 104 | && comp(tmp, (p = ptr + (j - gap) * size)) < 0; j -= gap ) { 105 | cel_mem_copy( p, p + gap * size, size ); 106 | } 107 | 108 | //located the tmp 109 | if ( j < i ) { 110 | cel_mem_copy( tmp, ptr + j * size, size ); 111 | } 112 | } 113 | } 114 | 115 | //free the temp allocations. 116 | free(tmp); 117 | } 118 | 119 | 120 | /** 121 | * merge the two half array. 122 | * 123 | * @param a 124 | * @param tmp - temp array. 125 | * @param l - left index. 126 | * @param m - middle index. 127 | * @param r - right index. 128 | */ 129 | static void merge( 130 | uchar_t *a, uchar_t *tmp, uint_t size, 131 | cel_compare_fn_t comp, uint_t l, uint_t r, uint_t rend) 132 | { 133 | uint_t ptmp = l; 134 | uint_t lend = r - 1; 135 | uint_t p = l; 136 | 137 | register uchar_t * ltmp, * rtmp; 138 | 139 | while ( l <=lend && r <= rend ) { 140 | ltmp = a + l * size; 141 | rtmp = a + r * size; 142 | if ( comp( ltmp, rtmp ) < 0 ) { 143 | //Copy the left current one to the temp. 144 | cel_mem_copy(ltmp, tmp + p * size, size); 145 | l++; 146 | } else { 147 | //Copy the right current one to the temp. 148 | cel_mem_copy(rtmp, tmp + p * size, size); 149 | r++; 150 | } 151 | p++; 152 | } 153 | 154 | //Copy the rest of the left to the temp. 155 | while ( l <= lend ) { 156 | cel_mem_copy( a + l * size, tmp + p * size, size ); 157 | l++; p++; 158 | } 159 | 160 | //Copy the rest of the right to the temp. 161 | while ( r <= rend ) { 162 | cel_mem_copy( a + r * size, tmp + p * size, size ); 163 | r++; p++; 164 | } 165 | 166 | //Copy the items back from the temp to the origin one. 167 | while ( ptmp <= rend ) { 168 | cel_mem_copy( tmp + ptmp * size, a + ptmp * size, size ); 169 | ptmp++; 170 | } 171 | } 172 | 173 | /** 174 | * merge sort algorithm. 175 | * 176 | * @see cel_sort#cel_shell_sort 177 | */ 178 | CEL_API void cel_merge_sort(void *a, uint_t length, uint_t size, cel_compare_fn_t comp) 179 | { 180 | //temp array. 181 | uchar_t * tmp = ( uchar_t * ) cel_calloc(length, sizeof(void *)); 182 | uchar_t * arr = ( uchar_t * ) a; 183 | 184 | register uint_t offset = 2; 185 | register uint_t len, i, j; 186 | 187 | if ( tmp == NULL ) return; 188 | 189 | //merge sort 190 | while ( offset <= length ) { 191 | len = length / offset; 192 | for ( j = 0, i = 0; j < len - 1; j++ ) { 193 | //The right end was included, so plus 1 with it. 194 | merge(arr, tmp, size, comp, i, i + offset / 2, i + offset - 1); 195 | i += offset; 196 | } 197 | 198 | //merge the len offset. (make sure you saw the i < len - 1) 199 | merge(arr, tmp, size, comp, i, i + offset / 2, i + offset - 1); 200 | i += offset; 201 | 202 | //Check if any items left for the array. 203 | len = i + offset / 2; 204 | if ( len <= length - 1 ) { 205 | merge(arr, tmp, size, comp, i, i + offset / 2, length - 1); 206 | } 207 | 208 | //Double size the offset. 209 | offset += offset; 210 | } 211 | 212 | //Check and merge the rest of the left items. 213 | offset /= 2; 214 | if ( offset < length ) { 215 | merge( arr, tmp, size, comp, 0, offset, length - 1 ); 216 | } 217 | 218 | //free the temp array. 219 | cel_free(tmp); 220 | } 221 | 222 | 223 | 224 | /** 225 | * internal function to get the median privot. 226 | */ 227 | static uchar_t *median3( 228 | uchar_t *arr, uint_t size, cel_compare_fn_t comp, uint_t left, uint_t right) 229 | { 230 | uchar_t * lp = arr + left * size; 231 | uchar_t * cp = arr + ( (left + right) / 2 ) * size; 232 | uchar_t * rp = arr + right * size; 233 | 234 | if ( comp( lp, cp ) > 0 ) 235 | cel_mem_swap( lp, cp, size ); 236 | if ( comp( lp, rp ) > 0 ) 237 | cel_mem_swap( lp, rp, size ); 238 | if ( comp( cp, rp ) > 0 ) 239 | cel_mem_swap( cp, rp, size ); 240 | 241 | //move the pivot to the right - 1 positon. 242 | cel_mem_swap( cp, rp - size, size ); 243 | 244 | return rp - size; 245 | } 246 | 247 | /** 248 | * internal static function to do the partition. 249 | * 250 | * @see cel_quick_sort 251 | */ 252 | static void quicksort( 253 | uchar_t *arr, uint_t size, cel_compare_fn_t comp, uint_t left, uint_t right) 254 | { 255 | //printf("left: %d, right: %d\n", left, right); 256 | if ( left + 11 <= right ) { 257 | //get the privot of the subarray. 258 | uchar_t * pivot = median3( arr, size, comp, left, right ); 259 | //printf("pivote: %d\n", *pivot); 260 | //start partitioning. 261 | register uint_t i = left, j = right - 1; 262 | for ( ; ; ) { 263 | while ( comp( arr + (++i * size), pivot ) < 0 ) ; 264 | while ( comp( arr + (--j * size), pivot ) > 0 ) ; 265 | if ( i < j ) { 266 | cel_mem_swap( arr + i * size, arr + j * size, size ); 267 | } else { 268 | break; 269 | } 270 | } 271 | 272 | //swap the privot back to the small colleciton. 273 | cel_mem_swap( arr + i * size, pivot, size ); 274 | 275 | quicksort( arr, size, comp, left, i - 1 ); 276 | quicksort( arr, size, comp, i, right ); 277 | 278 | } else { 279 | //if the number of the items is less than CUTOFF use insertion sort instead. 280 | cel_subinsert_sort( arr, size, comp, left, right ); 281 | } 282 | } 283 | 284 | /** 285 | * quick sort algorithm. 286 | * 287 | * @see cel_sort#cel_shell_sort 288 | */ 289 | CEL_API void cel_quick_sort( 290 | void *a, uint_t length, uint_t size, cel_compare_fn_t comp) 291 | { 292 | uchar_t * arr = (uchar_t *) a; 293 | quicksort(arr, size, comp, 0, length - 1); 294 | } 295 | 296 | 297 | 298 | /** 299 | * bucket sort algorithm. 300 | * yeah, this is fast, but also got lots of limits. 301 | * it applicable only for simple, unsigned integer (it could actually). 302 | * and the occurrence of the same numeric should be less than 2^(b - 1) . 303 | * 304 | * @param a - the array to be sort. 305 | * @param length - the length of the array. 306 | * @param bits - bits for one bucket it depened the occurrence of the same numeric. 307 | */ 308 | CEL_API void cel_bucket_sort( 309 | uint_t * a, uint_t length, uint_t bits) 310 | { 311 | register uint_t i; 312 | 313 | uint_t __bytes; 314 | uchar_t * key; 315 | uint_t idx; 316 | int v; 317 | 318 | //find the max item in the array. 319 | uint_t max = a[0]; 320 | for ( i = 1; i < length; i++ ) { 321 | if ( a[i] > max ) max = a[i]; 322 | } 323 | 324 | //make the allocations accorading to the max. 325 | __bytes = ( max * bits + CHAR_BIT - 1 ) / CHAR_BIT; 326 | key = ( uchar_t * ) cel_calloc( __bytes, sizeof(char) ); 327 | if ( key == NULL ) return; 328 | memset(key, __bytes, 0x00); 329 | 330 | //Set the bits. (sorting) 331 | for ( i = 0; i < length; i++ ) { 332 | v = 0; 333 | cel_mem_fetch( key, a[i] * bits, bits, &v, 0UL ); 334 | v++; 335 | cel_mem_store( &v, 0UL, bits, key, a[i] * bits ); 336 | //printf("step%2d, %2d, %2d, step%2d: ", i, a[i], v); 337 | //cel_mem_bits(NULL, key, __bytes); 338 | } 339 | 340 | idx = 0; 341 | for ( i = 0; i <= max; i++ ) { 342 | v = 0; 343 | cel_mem_fetch( key, i * bits, bits, &v, 0UL ); 344 | if ( v > 0 ) { 345 | while ( --v >= 0 ) a[idx++] = i; 346 | } 347 | } 348 | 349 | //free the allocations. 350 | cel_free( key ); 351 | } 352 | -------------------------------------------------------------------------------- /src/cel_sort.h: -------------------------------------------------------------------------------- 1 | /** 2 | * cel sort module interface header file. 3 | * @package src/cel_sort.h. 4 | * 5 | * @author chenxin 6 | */ 7 | #ifndef _cel_sort_h 8 | #define _cel_sort_h 9 | 10 | #include "cel_api.h" 11 | 12 | /** 13 | * insertion sort algorithm. 14 | * 15 | * @param a 16 | * @param length - the length of the array. 17 | * @param size - the bytes that one element takes in the array. 18 | * @param comp - the compare function. 19 | * @param start - the start offset. 20 | * @param end - the end offset (not included). 21 | */ 22 | CEL_API void cel_subinsert_sort( void *, 23 | uint_t, cel_compare_fn_t, uint_t, uint_t ); 24 | 25 | //insetion sort. 26 | #define cel_insertion_sort( a, len, size, comp ) \ 27 | cel_subinsert_sort(a, size, comp, 0, len - 1) 28 | 29 | /** 30 | * shell sort algorithm. 31 | * 32 | * @param length - the length of the array. 33 | * @param size - the bytes that one element in the array takes. 34 | * @param comp - comprare function 35 | */ 36 | CEL_API void cel_shell_sort( void *, uint_t, uint_t, cel_compare_fn_t ); 37 | 38 | /**merge sort algorithm.*/ 39 | CEL_API void cel_merge_sort( void *, uint_t, uint_t, cel_compare_fn_t ); 40 | 41 | /**quick sort algorithm.*/ 42 | CEL_API void cel_quick_sort( void *, uint_t, uint_t, cel_compare_fn_t ); 43 | 44 | /** 45 | * bucket sort algorithm. 46 | * yeah, this is fast, but also got lots of limits. 47 | * it applicable only for simple, unsigned integer (it could actually). 48 | * and the occurrence of the same numeric should be less than 2^(b - 1) . 49 | * 50 | * @param a - the array to be sort. 51 | * @param length - the length of the array. 52 | * @param bits - bits for one bucket, 53 | * it depened the occurrence of the same numeric. 54 | */ 55 | CEL_API void cel_bucket_sort( uint_t *, uint_t, uint_t ); 56 | 57 | #endif /*end ifndef*/ 58 | 59 | -------------------------------------------------------------------------------- /src/cel_stdio.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel memory operation module implements functions 3 | * source file. 4 | * @package src/cel_stdio.c 5 | * 6 | * @author chenxin 7 | */ 8 | 9 | #include "cel_stdio.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | //swap the content between a and b for specified bytes. 16 | CEL_API void cel_mem_swap(void *a, void *b, int size) 17 | { 18 | uchar_t * __a = a; 19 | uchar_t * __b = b; 20 | uchar_t temp; 21 | 22 | //swap the content. 23 | //do { 24 | // temp = *__a; 25 | // *__a++ = *__b; 26 | // *__b++ = temp; 27 | //} while ( --size > 0); 28 | while ( size-- > 0 ) { 29 | temp = *__a; 30 | *__a++ = *__b; 31 | *__b++ = temp; 32 | } 33 | } 34 | 35 | /** 36 | * copy specified bytes from source to the destination. 37 | * 38 | * @param _src - copy source. 39 | * @param _dst - copy destination. 40 | * @param size - bytes to copy 41 | */ 42 | CEL_API void cel_mem_copy( void * _src, void * _dst, int size ) 43 | { 44 | uchar_t * __src = _src; 45 | uchar_t * __dst = _dst; 46 | 47 | //do { 48 | // *__dst++ = *__src++; 49 | //} while ( --size > 0 ); 50 | while ( size-- > 0 ) { 51 | //printf("size=%d\n", size); 52 | *__dst++ = *__src++; 53 | } 54 | } 55 | 56 | /** 57 | * print the content with forms of bits. 58 | * 59 | * @param ptr 60 | * @param __bytes - bytes of the ptr. 61 | */ 62 | CEL_API void cel_mem_bits(const char *name, const void *ptr, uint_t __bytes) 63 | { 64 | uint_t i; 65 | uchar_t * str = ( uchar_t * ) ptr; 66 | uint_t length = __bytes * CHAR_BIT; 67 | 68 | if ( name != NULL ) printf("%s", name); 69 | for ( i = 0; i < length; ) { 70 | if ( CEL_BIT_FETCH(str, i) != 0 ) 71 | printf("1"); 72 | else 73 | printf("0"); 74 | //print a space for every 8 bits 75 | if ( (++i % 8) == 0 ) printf(" "); 76 | } 77 | printf("\n"); 78 | } 79 | 80 | /** 81 | * store specified bits from the specified index to 82 | * the specified address. 83 | * 84 | * @param src 85 | * @param start - the start index. 86 | * @param bits - number of bits to store. 87 | * @param dst - data to store. 88 | * @param idx - the start index. 89 | */ 90 | CEL_API void cel_mem_store( 91 | const void *_src, uint_t start, uint_t bits, void *_dst, uint_t idx) 92 | { 93 | uchar_t * __src = ( uchar_t * ) _src; 94 | uchar_t * __dst = ( uchar_t * ) _dst; 95 | 96 | register uint_t i; 97 | 98 | //cel_mem_bits("_src: ", _src, 8); 99 | for ( i = 0; i < bits; i++ ) { 100 | //printf("%2d: %d\n", start + i, CEL_BIT_FETCH(__src, start + i) != 0); 101 | if ( CEL_BIT_FETCH(__src, start + i) != 0 ) 102 | CEL_BIT_OPEN( __dst, idx + i ); 103 | else 104 | CEL_BIT_SHUT( __dst, idx + i ); 105 | } 106 | 107 | //uint32_t * __dst = ( uint32_t * ) _dst; 108 | //uint32_t * __src = ( uint32_t * ) _src; 109 | 110 | //uint32_t seed, v; 111 | //uint_t b = idx % 32; 112 | //idx = idx / 32; 113 | 114 | ////Handle the first byte 115 | //if ( b != 0 ) { 116 | // //Take b bits from the source. 117 | // seed = (uint32_t) ( pow(2, 32 - b) - 1 ); 118 | // v = ( (*__src) & seed ); 119 | 120 | // //Copy the source bits to the desitination. 121 | // seed = (uint32_t) (pow(2, b) - 1); 122 | // __dst[idx] = ( (__dst[idx] & seed) ); 123 | //} 124 | 125 | //register uint_t i = 0; 126 | //idx = idx / 32; 127 | 128 | //do { 129 | // //Get the souce bits. 130 | // b = (bits > 32) ? 32 : bits; 131 | // seed = (uint32_t) pow(2, b) - 1; 132 | // v = ( (* ( __src + i )) & seed ); 133 | 134 | // //Copy the source bits to the destination. 135 | // __dst[idx+i] = (__dst[idx+i] & (0x7FFFFFFE << (b - 1))) + v; 136 | // bits -= 32; i++; 137 | //} while ( bits > 0); 138 | } 139 | 140 | 141 | /* get the bytes of a specified file. 142 | * 143 | * @return size of file or -1 when fail. 144 | * */ 145 | CEL_API llong_t cel_filesize( char *file ) 146 | { 147 | FILE * fp; 148 | llong_t length = 0; 149 | 150 | if ( (fp = fopen( file, "rb" )) == NULL ) { 151 | return -1; 152 | } else { 153 | fseek( fp, 0L, SEEK_END ); 154 | length = ftell( fp ); 155 | fclose( fp ); 156 | } 157 | 158 | //printf("cel_filesize#done"); 159 | return length; 160 | } 161 | -------------------------------------------------------------------------------- /src/cel_stdio.h: -------------------------------------------------------------------------------- 1 | /** 2 | * cel memory module interface header file. 3 | * @package src/cel_stdio.h. 4 | * 5 | * @author chenxin 6 | */ 7 | #ifndef _cel_stdio_h 8 | #define _cel_stdio_h 9 | 10 | #include "cel_api.h" 11 | #include 12 | 13 | //bit macro functions 14 | #define CEL_BIT_OPEN(str, n) \ 15 | (str[(n)/CHAR_BIT] |= (0x01<<((n)%CHAR_BIT))) 16 | 17 | #define CEL_BIT_SHUT(str, n) \ 18 | (str[(n)/CHAR_BIT] &= (~(0x01<<((n)%CHAR_BIT)))) 19 | 20 | #define CEL_BIT_FETCH(str, n) \ 21 | (str[(n)/CHAR_BIT] & (0x01<<((n)%CHAR_BIT))) 22 | 23 | //swap the content between a and b for specified bytes. 24 | CEL_API void cel_mem_swap( void *, void *, int ); 25 | 26 | /** 27 | * copy specified bytes from source to the destination. 28 | * 29 | * @param _src - copy source. 30 | * @param _dst - copy destination. 31 | * @param size - bytes to copy 32 | */ 33 | CEL_API void cel_mem_copy( void *, void *, int ); 34 | 35 | /** 36 | * print the content with forms of bits. 37 | * 38 | * @param name 39 | * @param ptr 40 | * @param length - bytes of the ptr. 41 | */ 42 | CEL_API void cel_mem_bits( const char *, const void *, uint_t ); 43 | 44 | /** 45 | * store specified bits from the specified index to 46 | * the specified address. 47 | * 48 | * @param src 49 | * @param start - the start index. 50 | * @param bits - number of bits to store include the bit at start index. 51 | * @param dst - data to store. 52 | * @param idx - the start index. 53 | */ 54 | CEL_API void cel_mem_store( const void *, uint_t, uint_t, void *, uint_t ); 55 | 56 | /** 57 | * copy specified bits from a specified index of specified source. 58 | * to a given buffer. 59 | * 60 | * @param src 61 | * @param start - the start index. 62 | * @param bits - number of bits to fetch. 63 | * @param dst - data buffer. 64 | * @param idx - the start index. 65 | */ 66 | //CEL_API void cel_mem_fetch( const void *, uint_t, uint_t, void *, uint_t ); 67 | #define cel_mem_fetch(src, start, bits, dst, idx) \ 68 | cel_mem_store(src, start, bits, dst, idx) 69 | 70 | 71 | /*{{{ basic data type encode/decode interface. 72 | * */ 73 | 74 | /* get the bytes of a specified file. 75 | * 76 | * @return size of file or -1 when fail. 77 | */ 78 | CEL_API llong_t cel_filesize( char * ); 79 | #endif /*end ifndef*/ 80 | -------------------------------------------------------------------------------- /src/cel_string.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel string module interface implemente functions. 3 | * @package src/cel_string.c .
4 | * 5 | * @author chenxin 6 | * @date 2013-09-16 7 | */ 8 | 9 | #include "cel_string.h" 10 | #include 11 | 12 | CEL_API void cel_left_trim(char *str) 13 | { 14 | register int i, j; 15 | register int slen = strlen(str); 16 | for ( i = 0; i < slen; i++ ) { 17 | if ( str[i] != ' ' && str[i] != '\t' && str[i] != '\r' ) { 18 | break; 19 | } 20 | } 21 | 22 | if ( i > 0 ) { 23 | for ( j = 0; i < slen; i++, j++ ) { 24 | str[j] = str[i]; 25 | } 26 | 27 | str[j] = '\0'; 28 | } 29 | } 30 | 31 | CEL_API void cel_right_trim(char *str) 32 | { 33 | register int i; 34 | register int slen = strlen(str); 35 | for ( i = slen - 1; i >= 0; i-- ) { 36 | if ( str[i] != ' ' && str[i] != '\t' && str[i] != '\r' ) { 37 | str[i+1] = '\0'; 38 | break; 39 | } 40 | } 41 | } 42 | 43 | /* {{{ cel string block*/ 44 | CEL_API cel_string_t *new_cel_string( cstring str ) 45 | { 46 | uint_t len; 47 | cel_string_t *ptr = ( cel_string_t * ) 48 | cel_malloc( sizeof( cel_string_t ) ); 49 | if ( str == NULL ) return NULL; 50 | 51 | //Count the length of the string. 52 | len = (uint_t) strlen( str ); 53 | ptr->str = ( cstring ) cel_malloc( len ); 54 | if ( ptr->str == NULL ) { 55 | cel_free( ptr ); 56 | return NULL; 57 | } 58 | 59 | //Copy the string to the heap. 60 | memcpy( ptr->str, str, len ); 61 | ptr->len = len; 62 | 63 | return ptr; 64 | } 65 | 66 | CEL_API cel_string_t *cel_string_long( long numeric ) 67 | { 68 | return NULL; 69 | } 70 | 71 | CEL_API cel_string_t *cel_string_double( double decimal ) 72 | { 73 | return NULL; 74 | } 75 | 76 | CEL_API void free_cel_string( cel_string_t **cstr ) 77 | { 78 | if ( cstr == NULL ) return; 79 | if ( *cstr != NULL ) { 80 | cel_free( (*cstr)->str ); 81 | (*cstr)->str = NULL; 82 | 83 | cel_free( *cstr ); 84 | *cstr = NULL; 85 | } 86 | } 87 | /* }}}*/ 88 | 89 | 90 | 91 | 92 | /* {{{ cel string buffer area. 93 | * */ 94 | static cstring create_buffer( int opacity ) 95 | { 96 | cstring str = ( cstring ) cel_malloc( opacity ); 97 | if ( str == NULL ) return NULL; 98 | memset( str, 0x00, opacity ); 99 | return str; 100 | } 101 | 102 | /* resize the string buffer to a specified opacity 103 | * 104 | * @param opacity - the new opacity 105 | * @return int 1 for success and 0 for failed 106 | * */ 107 | static int resize_buffer( 108 | cel_strbuff_t *sb, int opacity ) 109 | { 110 | uint_t length; 111 | cstring str = create_buffer( opacity ); 112 | if ( str == NULL ) { 113 | //free_cel_strbuff( &sb ); 114 | return 0; 115 | } 116 | 117 | length = (opacity > sb->size) ? sb->size : opacity; 118 | memcpy( str, sb->buffer, length ); 119 | 120 | //free the old buffer. 121 | cel_free( sb->buffer ); 122 | sb->opacity = opacity; 123 | sb->buffer = str; 124 | 125 | return 1; 126 | } 127 | 128 | /* interface to create new cel_strbuff_entry 129 | * or to initialize a specified cel_strbuff_entry. 130 | * */ 131 | CEL_API cel_strbuff_t *new_cel_strbuff_opacity( int opacity ) 132 | { 133 | cel_strbuff_t *sb = ( cel_strbuff_t * ) 134 | cel_malloc( sizeof( cel_strbuff_t ) ); 135 | if ( sb == NULL ) return NULL; 136 | 137 | sb->buffer = create_buffer( opacity ); 138 | if ( sb->buffer == NULL ) { 139 | cel_free( sb ); 140 | CEL_ALLOCATE_ERROR("new_cel_strbuff_opacity", opacity); 141 | } 142 | 143 | //initialize. 144 | sb->opacity = opacity; 145 | sb->size = 0; 146 | 147 | return sb; 148 | } 149 | 150 | CEL_API cel_strbuff_t *new_cel_strbuff_string( cstring str ) 151 | { 152 | uint_t len, bytes; 153 | cel_strbuff_t *sb = (cel_strbuff_t *) cel_malloc(sizeof( cel_strbuff_t )); 154 | if ( sb == NULL ) return NULL; 155 | 156 | //Count the length. 157 | len = (uint_t) strlen( str ); 158 | bytes = len + _CEL_DEFAULT_STRBUFF_OPACITY_; 159 | 160 | sb->buffer = create_buffer( bytes ); 161 | if ( sb->buffer == NULL ) { 162 | cel_free( sb ); 163 | CEL_ALLOCATE_ERROR("new_cel_strbuff_string", bytes); 164 | } 165 | 166 | //initialize. 167 | sb->opacity = bytes; 168 | sb->size = len; 169 | memcpy( sb->buffer, str, len ); 170 | 171 | return sb; 172 | } 173 | 174 | //free the specified string buffer. 175 | CEL_API void free_cel_strbuff( cel_strbuff_t **sb ) 176 | { 177 | if ( sb == NULL ) return; 178 | if ( *sb != NULL ) { 179 | cel_strbuff_destroy(*sb); 180 | cel_free( *sb ); 181 | *sb = NULL; 182 | } 183 | } 184 | 185 | /* 186 | * create the specified string buffer 187 | * 188 | * @param cel_strbuff_t * 189 | * @param uint_t default opacity 190 | * @param cstring default string - could be NULL 191 | * @return int 1 for success and 0 for failed 192 | */ 193 | CEL_API int cel_strbuff_init(cel_strbuff_t *sb, int opacity, cstring str) 194 | { 195 | uint_t len = 0; 196 | uint_t bytes = opacity; 197 | 198 | //check the initialize string. 199 | if ( str != NULL ) { 200 | len = strlen( str ); 201 | bytes += len; 202 | } 203 | 204 | sb->buffer = create_buffer( bytes ); 205 | if ( sb->buffer == NULL ) { 206 | return 0; 207 | } 208 | 209 | //initialize 210 | sb->opacity = bytes; 211 | sb->size = len; 212 | if ( len > 0 ) memcpy( sb->buffer, str, len ); 213 | 214 | return 1; 215 | } 216 | 217 | /* 218 | * destroy the specified cel_strbuff_t 219 | * 220 | * @param cel_strbuff_t * 221 | * @return int 1 for success and 0 for failed 222 | */ 223 | CEL_API int cel_strbuff_destroy( cel_strbuff_t *sb ) 224 | { 225 | if ( sb != NULL ) { 226 | cel_free( sb->buffer ); 227 | sb->buffer = NULL; 228 | } 229 | 230 | return 1; 231 | } 232 | 233 | /* 234 | * append a new string to the buffer. 235 | * 236 | * @param cstring - the string to append to. 237 | * a macro define has replace this. 238 | * */ 239 | //CEL_API void cel_strbuff_append( cel_strbuff_t *sb, cstring str ) 240 | //{ 241 | // uint_t len = strlen( str ); 242 | // if ( sb->size + len > sb->opacity ) 243 | // resize_buffer( sb, (sb->size + len) * 2 + 1 ); 244 | // 245 | // //Copy the string insite. 246 | // memcpy( sb->buffer + sb->size, str, len ); 247 | // sb->size += len; 248 | //} 249 | 250 | /* append some bytes from a specified buffer 251 | * from a specified index 252 | * 253 | * @param cstring - the buffer to copy byte from 254 | * @param len - the number of bytes to copy 255 | * @param count - repeat times 256 | * @return 1 for success and 0 for failed 257 | * */ 258 | CEL_API int cel_strbuff_append_from( 259 | cel_strbuff_t *sb, cstring src, uint_t len, int count) 260 | { 261 | while ( count-- > 0 ) { 262 | //leave a space for the end of the buffer. 263 | //change '>' '>=' at 2013-10-12 264 | if ( sb->size + len >= sb->opacity ) { 265 | if ( resize_buffer( sb, (sb->size + len) * 2 + 1 ) == 0 ) { 266 | return 0; 267 | } 268 | } 269 | 270 | //Copy the string insite. 271 | if ( len == 1 ) { 272 | sb->buffer[sb->size++] = src[0]; 273 | } else { 274 | memcpy( sb->buffer + sb->size, src, len ); 275 | sb->size += len; 276 | } 277 | } 278 | 279 | return 1; 280 | } 281 | 282 | /* append a char to the buffer. 283 | * 284 | * @param char 285 | * @return 1 for success and 0 for failed 286 | * */ 287 | CEL_API int cel_strbuff_append_char(cel_strbuff_t *sb, char ch, int count) 288 | { 289 | while ( count-- > 0 ) { 290 | //leave a space for the end of the buffer. 291 | //change '>' '>=' at 2013-10-12 292 | if ( sb->size + 1 >= sb->opacity ) { 293 | if ( resize_buffer( sb, sb->opacity * 2 + 1 ) == 0 ) { 294 | return 0; 295 | } 296 | } 297 | 298 | //append the char 299 | sb->buffer[sb->size++] = ch; 300 | } 301 | 302 | return 1; 303 | } 304 | 305 | /* insert a new string to the buffer from the specified position. 306 | * 307 | * @param uint_t - the start position. 308 | * @param cstring - the string to insert in. 309 | * 310 | * a macro define has replace this. 311 | * */ 312 | //CEL_API void cel_strbuff_insert( 313 | // cel_strbuff_t *sb, uint_t idx, cstring str ) 314 | //{ 315 | // if ( idx > sb->size ) return; 316 | // 317 | // uint_t len = strlen( str ); 318 | // if ( sb->size + len > sb->opacity ) 319 | // resize_buffer( sb, ( sb->size + len ) * 2 + 1); 320 | // 321 | // //Copy the old mem back len step. 322 | // //do not use the memcpy here. 323 | // int i, l = (int) idx; 324 | // for ( i = sb->size - 1; i >= l; i-- ) 325 | // sb->buffer[i+len] = sb->buffer[i]; 326 | // 327 | // //Copy the new string start from idx. 328 | // memcpy( sb->buffer + idx, str, len ); 329 | // sb->size += len; 330 | //} 331 | 332 | /* insert part of buffer to the buffer 333 | * 334 | * @param uint_t - start position 335 | * @param cstring - source buffer 336 | * @param uint_t - number of bytes to copy 337 | * @return 1 for success and 0 for failed 338 | * */ 339 | CEL_API int cel_strbuff_insert_from( 340 | cel_strbuff_t *sb, uint_t idx, cstring src, uint_t len, int count) 341 | { 342 | while ( count-- > 0 ) { 343 | int i, l = (int) idx; 344 | if ( idx > sb->size ) break; 345 | 346 | //leave a space for the end of the buffer. 347 | //change '>' '>=' at 2013-10-12 348 | if ( sb->size + len >= sb->opacity ) { 349 | if ( resize_buffer( sb, ( sb->size + len ) * 2 + 1) == 0 ) { 350 | return 0; 351 | } 352 | } 353 | 354 | //Copy the old mem back len step. 355 | //do not use the memcpy here. 356 | for ( i = sb->size - 1; i >= l; i-- ) { 357 | sb->buffer[i+len] = sb->buffer[i]; 358 | } 359 | 360 | //Copy the new string start from idx. 361 | memcpy( sb->buffer + idx, src, len ); 362 | sb->size += len; 363 | idx += len; 364 | } 365 | 366 | return 1; 367 | } 368 | 369 | /* insert a char to the buffer from a specified index position. 370 | * 371 | * @param char 372 | * @return 1 for success and 0 for failed 373 | * */ 374 | CEL_API int cel_strbuff_insert_char( 375 | cel_strbuff_t *sb, uint_t idx, char ch, int count) 376 | { 377 | while ( count-- > 0 ) { 378 | if ( idx > sb->size ) return 0; 379 | 380 | //leave a space for the end of the buffer. 381 | //change '>' '>=' at 2013-10-12 382 | if ( sb->size + 1 >= sb->opacity ) { 383 | if ( resize_buffer( sb, sb->size * 2 + 1 ) == 0 ) { 384 | return 0; 385 | } 386 | } 387 | 388 | sb->buffer[sb->size++] = ch; 389 | idx++; 390 | } 391 | 392 | return 1; 393 | } 394 | 395 | /* remove specified char from a specified position from the buffer 396 | * 397 | * @param uint_t - the start index 398 | * @param uint_t - number of chars to remove 399 | * @return 1 for success and 0 for failed 400 | * */ 401 | CEL_API int cel_strbuff_remove(cel_strbuff_t *sb, uint_t idx, uint_t len) 402 | { 403 | uint_t i; 404 | if ( idx >= sb->size ) return 0; 405 | 406 | //Recount the length. 407 | if ( len > sb->size - idx ) { 408 | len = sb->size - idx; 409 | } 410 | 411 | //Copy the buffer. 412 | for ( i = idx + len; i < sb->size; i++ ) { 413 | sb->buffer[i-len] = sb->buffer[i]; 414 | } 415 | 416 | //clear the old buffer part. 417 | memset( sb->buffer + sb->size - len , 0x00, len ); 418 | sb->size -= len; 419 | 420 | return 1; 421 | } 422 | 423 | //clear the specified string buffer. 424 | CEL_API int cel_strbuff_clear( cel_strbuff_t *sb ) 425 | { 426 | memset( sb->buffer, 0x00, sb->size ); 427 | sb->size = 0; 428 | 429 | return 1; 430 | } 431 | /* end of string buffer. 432 | * }}} 433 | * */ 434 | 435 | 436 | 437 | /* {{{ utf-8 unicode convert. 438 | * */ 439 | /* check the specified word token to store encode in utf-8. 440 | * 441 | * @param ch 442 | * */ 443 | CEL_API uint_t cel_utf8_charbytes( char ch ) 444 | { 445 | register uint_t t = 0; 446 | 447 | //if ( ( ch & 0x80 ) == 0 ) return 1; 448 | if ( ch >= 0 ) return 1; 449 | 450 | for ( ; ( ch & 0x80 ) != 0; ch <<= 1 ) { 451 | t++; 452 | } 453 | 454 | return t; 455 | } 456 | 457 | /* convert the specified utf8 word to unicode. 458 | * 459 | * @param word 460 | * */ 461 | CEL_API uint_t cel_utf8_to_ucode( cstring word ) 462 | { 463 | //Count the bytes the word take. 464 | uint_t bytes = cel_utf8_charbytes( *word ); 465 | 466 | //unicode serial. 467 | uint_t code = 0; 468 | 469 | register uchar_t *bit = ( uchar_t * ) &code; 470 | register char b1, b2, b3/*, b4, b5, b6*/; 471 | 472 | switch ( bytes ) 473 | { 474 | case 1: 475 | *bit = *word; 476 | break; 477 | case 2: 478 | b1 = *(word); 479 | b2 = *(word + 1); 480 | 481 | *bit = (b1 << 6) + (b2 & 0x3F); 482 | *(bit+1) = (b1 >> 2) & 0x07; 483 | break; 484 | case 3: 485 | b1 = *(word); 486 | b2 = *(word + 1); 487 | b3 = *(word + 2); 488 | 489 | *bit = (b2 << 6) + (b3 & 0x3F); 490 | *(bit+1) = (b1 << 4) + ((b2 >> 2) & 0x0F); 491 | break; 492 | //ignore the ones that are larger than 3 bytes; 493 | } 494 | 495 | return code; 496 | } 497 | 498 | /* convert the specified unicode to utf-8 word and store them in the buffer. 499 | * 500 | * @param ucode 501 | * @param buffer 502 | * @return int - the bytes it take. 503 | * */ 504 | CEL_API int cel_ucode_to_utf8(uint_t u, cstring __word) 505 | { 506 | if ( u <= 0x0000007F ) { 507 | //U-00000000 - U-0000007F 508 | //0xxxxxxx 509 | *__word = ( u & 0x7F ); 510 | return 1; 511 | } else if ( u >= 0x00000080 && u <= 0x000007FF ) { 512 | //U-00000080 - U-000007FF 513 | //110xxxxx 10xxxxxx 514 | *( __word + 1 ) = ( u & 0x3F) | 0x80; 515 | *__word = ((u >> 6) & 0x1F) | 0xC0; 516 | return 2; 517 | } else if ( u >= 0x00000800 && u <= 0x0000FFFF ) { 518 | //U-00000800 - U-0000FFFF 519 | //1110xxxx 10xxxxxx 10xxxxxx 520 | *( __word + 2 ) = ( u & 0x3F) | 0x80; 521 | *( __word + 1 ) = ((u >> 6) & 0x3F) | 0x80; 522 | *__word = ((u >> 12) & 0x0F) | 0xE0; 523 | return 3; 524 | } else if ( u >= 0x00010000 && u <= 0x001FFFFF ) { 525 | //U-00010000 - U-001FFFFF 526 | //11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 527 | *( __word + 3 ) = ( u & 0x3F) | 0x80; 528 | *( __word + 2 ) = ((u >> 6) & 0x3F) | 0x80; 529 | *( __word + 1 ) = ((u >> 12) & 0x3F) | 0x80; 530 | *__word = ((u >> 18) & 0x07) | 0xF0; 531 | return 4; 532 | } else if ( u >= 0x00200000 && u <= 0x03FFFFFF ) { 533 | //U-00200000 - U-03FFFFFF 534 | //111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 535 | *( __word + 4 ) = ( u & 0x3F) | 0x80; 536 | *( __word + 3 ) = ((u >> 6) & 0x3F) | 0x80; 537 | *( __word + 2 ) = ((u >> 12) & 0x3F) | 0x80; 538 | *( __word + 1 ) = ((u >> 18) & 0x3F) | 0x80; 539 | *__word = ((u >> 24) & 0x03) | 0xF8; 540 | return 5; 541 | } else if ( u >= 0x04000000 && u <= 0x7FFFFFFF ) { 542 | //U-04000000 - U-7FFFFFFF 543 | //1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 544 | *( __word + 5 ) = ( u & 0x3F) | 0x80; 545 | *( __word + 4 ) = ((u >> 6) & 0x3F) | 0x80; 546 | *( __word + 3 ) = ((u >> 12) & 0x3F) | 0x80; 547 | *( __word + 2 ) = ((u >> 18) & 0x3F) | 0x80; 548 | *( __word + 1 ) = ((u >> 24) & 0x3F) | 0x80; 549 | *__word = ((u >> 30) & 0x01) | 0xFC; 550 | return 6; 551 | } 552 | 553 | return 0; 554 | } 555 | /* end of utf-8 unicoe convert. 556 | * }}} 557 | * */ 558 | -------------------------------------------------------------------------------- /src/cel_string.h: -------------------------------------------------------------------------------- 1 | /** 2 | * cel string module interface header file. 3 | * @package src/cel_string.h.
4 | * 5 | * @author chenx 6 | */ 7 | #ifndef cel_string_h 8 | #define cel_string_h 9 | 10 | #include "cel_api.h" 11 | #include 12 | 13 | CEL_API void cel_left_trim(char *); 14 | CEL_API void cel_right_trim(char *); 15 | 16 | //cel string block. 17 | typedef struct { 18 | cstring str; 19 | uint_t len; 20 | } cel_string_t; 21 | 22 | //function to create a new cel string. 23 | CEL_API cel_string_t *new_cel_string( cstring ); 24 | 25 | //function to free the specified cel string. 26 | CEL_API void free_cel_string( cel_string_t ** ); 27 | 28 | CEL_API cel_string_t *cel_string_long( long ); 29 | 30 | CEL_API cel_string_t *cel_string_double( double ); 31 | 32 | 33 | 34 | /* {{{ string buffer. 35 | * */ 36 | typedef struct { 37 | cstring buffer; 38 | uint_t opacity; /*opacity of the buffer in bytes*/ 39 | uint_t size; /*length of the string in bytes.*/ 40 | } cel_strbuff_t; 41 | 42 | //default string buffer length. 43 | #define _CEL_DEFAULT_STRBUFF_OPACITY_ 8 44 | 45 | #define cel_strbuff_opacity(sb) sb->opacity 46 | #define cel_strbuff_size(sb) sb->size 47 | #define cel_strbuff_empty(sb) (sb->size==0) 48 | 49 | /* interface to create new cel_strbuff_entry 50 | * */ 51 | //CEL_API cel_strbuff_t *new_cel_strbuff(); 52 | #define new_cel_strbuff() \ 53 | new_cel_strbuff_opacity(_CEL_DEFAULT_STRBUFF_OPACITY_) 54 | 55 | CEL_API cel_strbuff_t *new_cel_strbuff_opacity( int ); 56 | CEL_API cel_strbuff_t *new_cel_strbuff_string( cstring ); 57 | 58 | //free the specified string buffer. 59 | CEL_API void free_cel_strbuff( cel_strbuff_t ** ); 60 | 61 | /* 62 | * create the specified string buffer 63 | * 64 | * @param cel_strbuff_t 65 | * @param uint_t default opacity 66 | * @param cstring default string - could be NULL 67 | * @return int 1 for success and 0 for failed 68 | */ 69 | CEL_API int cel_strbuff_init( cel_strbuff_t *, int, cstring ); 70 | 71 | /* 72 | * destroy the specified cel_strbuff_t 73 | * 74 | * @param cel_strbuff_t 75 | * @return int 1 for success and 0 for failed 76 | */ 77 | CEL_API int cel_strbuff_destroy( cel_strbuff_t * ); 78 | 79 | /* 80 | * append a new string to the buffer. 81 | * 82 | * @param cstring - the string to append to. 83 | * */ 84 | //CEL_API void cel_strbuff_append( cel_strbuff_t, cstring ); 85 | #define cel_strbuff_append(sb, str, count) \ 86 | cel_strbuff_append_from( sb, str, strlen(str), count ) 87 | 88 | /* append some bytes from a specified buffer 89 | * 90 | * @param cstring - the buffer to copy byte from. 91 | * @param len - the number of bytes to copy. 92 | * @param count - repeat times. 93 | * @return int 1 for success and 0 for failed 94 | * */ 95 | CEL_API int cel_strbuff_append_from( cel_strbuff_t *, cstring, uint_t, int ); 96 | 97 | /* append a char to the buffer. 98 | * 99 | * @param char 100 | * @param count - repeat times. 101 | * @return 1 for success and 0 for failed 102 | * */ 103 | CEL_API int cel_strbuff_append_char( cel_strbuff_t *, char, int ); 104 | 105 | /* insert a new string to the buffer from the specified position. 106 | * 107 | * @param uint_t - the start position. 108 | * @param cstring - the string to insert in 109 | * @return 1 for success and 0 for failed 110 | * */ 111 | //CEL_API void cel_strbuff_insert( cel_strbuff_t *, uint_t, cstring ); 112 | #define cel_strbuff_insert(sb, idx, str, count)\ 113 | cel_strbuff_insert_from( sb, idx, str, strlen(str), count ) 114 | 115 | /* insert part of buffer to the buffer. 116 | * 117 | * @param uint_t - start position 118 | * @param cstring - source buffer 119 | * @param uint_t - number of bytes to copy 120 | * @param count - repeat times 121 | * @return 1 for success and 0 for failed 122 | * */ 123 | CEL_API int cel_strbuff_insert_from( cel_strbuff_t *, uint_t, cstring, uint_t, int ); 124 | 125 | /* insert a char to the buffer from a specified index position. 126 | * 127 | * @param char 128 | * @return 1 for success and 0 for failed 129 | * */ 130 | CEL_API int cel_strbuff_insert_char( cel_strbuff_t *, uint_t, char, int ); 131 | 132 | //get the char at a specified position. 133 | #define cel_strbuff_get( strbuff, idx ) strbuff->buffer[(idx)] 134 | 135 | /* remove specified char from a specified position from the buffer 136 | * 137 | * @param uint_t - the start index 138 | * @param uint_t - number of chars to remove 139 | * @return 1 for success and 0 for failed 140 | * */ 141 | CEL_API int cel_strbuff_remove( cel_strbuff_t *, uint_t, uint_t ); 142 | 143 | /* 144 | * clear the specified string buffer 145 | * 146 | * @return 1 for success and 0 for failed 147 | */ 148 | CEL_API int cel_strbuff_clear( cel_strbuff_t * ); 149 | /* end of string buffer. 150 | * }}} */ 151 | 152 | 153 | 154 | 155 | /* {{{ utf-8 unicode convert. 156 | * */ 157 | CEL_API uint_t cel_utf8_charbytes( char ); 158 | 159 | CEL_API uint_t cel_utf8_to_ucode( cstring ); 160 | 161 | CEL_API int cel_ucode_to_utf8( uint_t, cstring); 162 | /* end of utf-8 unicode conver. 163 | * }}} */ 164 | #endif /*end ifndef*/ 165 | -------------------------------------------------------------------------------- /src/gdb/Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lionsoul2014/celib/cb434e308a61485525eaa9f1d94e9f2d4340a740/src/gdb/Makefile -------------------------------------------------------------------------------- /src/mcache/Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lionsoul2014/celib/cb434e308a61485525eaa9f1d94e9f2d4340a740/src/mcache/Makefile -------------------------------------------------------------------------------- /src/test/rle/rle.decode.txt: -------------------------------------------------------------------------------- 1 | {{{{AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABCDEFGHIJKLMNOPQRSTUVWXYZ#}}}} 2 | -------------------------------------------------------------------------------- /src/test/rle/rle.encode.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lionsoul2014/celib/cb434e308a61485525eaa9f1d94e9f2d4340a740/src/test/rle/rle.encode.txt -------------------------------------------------------------------------------- /src/test/rle/rle.txt: -------------------------------------------------------------------------------- 1 | {{{{AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABCDEFGHIJKLMNOPQRSTUVWXYZ#}}}} 2 | -------------------------------------------------------------------------------- /src/test/rle/tst.rle.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lionsoul2014/celib/cb434e308a61485525eaa9f1d94e9f2d4340a740/src/test/rle/tst.rle.c -------------------------------------------------------------------------------- /src/test/tst-array.c: -------------------------------------------------------------------------------- 1 | /** 2 | * dynamic array test program . 3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_array.h" 7 | #include 8 | #include 9 | #include 10 | 11 | static int strcompare( void * str1, void * str2 ) 12 | { 13 | return strcmp( (char *) str1, (char *) str2 ); 14 | } 15 | 16 | //static int scomp( void * str1, void * str2 ) { 17 | // cel_array_t *a1 = ( cel_array_t * ) str1; 18 | // cel_array_t *a2 = ( cel_array_t * ) str2; 19 | // if ( a1->size == a2->size ) return 0; 20 | // if ( a1->size < a2->size ) return -1; 21 | // return 1; 22 | //} 23 | 24 | void rmcallback( void * value ) 25 | { 26 | printf("+-%s\n", (char *) value); 27 | } 28 | 29 | int main (int argc, char **args) 30 | { 31 | uint_t i; 32 | //create a new cel array 33 | //cel_array_t *array = new_cel_array(); 34 | cel_array_t arr, *array; 35 | cel_array_create(&arr, -1); 36 | array = &arr; 37 | 38 | char * a[]= { 39 | "chenxin", "yanzi", "zhangrenfang", "yangjiang", "panzi" 40 | }; 41 | 42 | for ( i = 0; i < 5; i++ ) 43 | { 44 | printf("+--add %s\n", a[i]); 45 | cel_array_add( array, a[i] ); 46 | } 47 | printf("size: %d\n", cel_array_size(array)); 48 | 49 | printf("\n+-----------get/del--------------+\n"); 50 | printf("get(0): %s\n", (char *) cel_array_get(array, 0)); 51 | printf("del(0): %s\n", (char *) cel_array_del(array, 0)); 52 | printf("get(0): %s\n", (char *) cel_array_get(array, 0)); 53 | 54 | printf("\n+---------------------set/get-----------------+\n"); 55 | printf("size: %d\n", cel_array_size(array)); 56 | printf("set(1): %s\n", (char *) cel_array_set( array, 1, "second" )); 57 | printf("get(1): %s\n", (char *) cel_array_get( array, 1 )); 58 | 59 | printf("\n+----------------insert-------------------+\n"); 60 | printf("insert(0): first\n"); 61 | cel_array_insert( array, 0, "first" ); 62 | printf("size: %d\n", cel_array_size( array )); 63 | printf("get(0): %s\n", (char *) cel_array_get( array, 0 )); 64 | 65 | printf("\n+----------------obj find-----------------+\n"); 66 | printf("get_obj(%s): %d\n", "yanzi", cel_array_get_obj(array, "yanzi", strcompare)); 67 | printf("del_obj(%s): %s\n", "yanzi", (char *)cel_array_del_obj(array, "yanzi", strcompare)); 68 | printf("size: %d\n", cel_array_size(array)); 69 | 70 | cel_array_destroy(array, rmcallback); 71 | 72 | //free the array 73 | //free_cel_array( &array, rmcallback ); 74 | //printf("array=NULL ? %d\n", (array==NULL)); 75 | //free_cel_array(&array, NULL); //test double free 76 | 77 | return 0; 78 | } 79 | 80 | -------------------------------------------------------------------------------- /src/test/tst-bitmap.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel bitmap test program. 3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_bitmap.h" 7 | #include 8 | 9 | int main( int argc, char *args[] ) 10 | { 11 | int a[] = {3, 5, 9, 6, 10, 30, 16, 18}; 12 | int i, length = 8; 13 | 14 | //cel_bitmap_t *bitmap = new_cel_bitmap(30); 15 | cel_bitmap_t bit, *bitmap; 16 | bitmap = &bit; 17 | cel_bitmap_create(&bit, 30); 18 | 19 | //set the bits 20 | for ( i = 0; i < length; i++ ) 21 | { 22 | cel_bitmap_set( bitmap, a[i] ); 23 | } 24 | 25 | printf("+---bitmap bucket sort: \n"); 26 | 27 | //get the bits 28 | for ( i = 0; i < bitmap->length; i++ ) 29 | { 30 | if ( cel_bitmap_get(bitmap, i) == 1 ) 31 | { 32 | //printf("1"); 33 | printf("%3d", i); 34 | } /*else { 35 | printf("0"); 36 | }*/ 37 | } 38 | putchar('\n'); 39 | 40 | cel_bitmap_remove( bitmap, 3 ); 41 | for ( i = 0; i < bitmap->length; i++ ) 42 | { 43 | if ( cel_bitmap_get(bitmap, i) == 1 ) 44 | { 45 | printf("%3d", i); 46 | } 47 | } 48 | putchar('\n'); 49 | 50 | //free_cel_bitmap( &bitmap ); 51 | cel_bitmap_destroy(bitmap); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /src/test/tst-bloomfilter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel bloom filter test program. 3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_bloomfilter.h" 7 | #include 8 | 9 | int main( int argc, char **args ) 10 | { 11 | uint_t i; 12 | 13 | //exists sets 14 | char *str[] = { 15 | "father", "mother", "chenxin", "roe", "jcseg", "friso", "robbe", "lionsoul" 16 | }; 17 | 18 | //Not exists sets 19 | char *str1[] = { 20 | "lvs", "tankwar", "syrian", "opert", "php" 21 | }; 22 | 23 | //cel bloom 24 | //there is 6 elements and we keep the m/n = 32 25 | //and we will get the smallest False positive rate. 26 | //cel_bloomfilter_t *bloom = new_cel_bloomfilter(8 * 32, 8); 27 | 28 | cel_bloomfilter_t bf, *bloom; 29 | bloom = &bf; 30 | cel_bloomfilter_create(&bf, 8 * 32, 8); 31 | 32 | printf("+---test add elements: \n"); 33 | for ( i = 0; i < 8; i++ ) 34 | { 35 | cel_bloomfilter_add(bloom, str[i]); 36 | } 37 | printf("size=%d\n", cel_bloomfilter_size(bloom)); 38 | putchar('\n'); 39 | 40 | printf("+---test check exists elements: \n"); 41 | for ( i = 0; i < 8; i++ ) 42 | { 43 | printf("exists(%s)=%d\n", str[i], cel_bloomfilter_exists(bloom, str[i])); 44 | } 45 | putchar('\n'); 46 | 47 | printf("+---test check not exists elements: \n"); 48 | for ( i = 0; i < 5; i++ ) 49 | { 50 | printf("exists(%s)=%d\n", str1[i], cel_bloomfilter_exists(bloom, str1[i])); 51 | } 52 | putchar('\n'); 53 | 54 | //free the bloom filter. 55 | //free_cel_bloomfilter(&bloom); 56 | cel_bloomfilter_destroy(bloom); 57 | 58 | return 0; 59 | } 60 | 61 | -------------------------------------------------------------------------------- /src/test/tst-crc32.c: -------------------------------------------------------------------------------- 1 | /** 2 | * crc32 test program. 3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_hash.h" 7 | #include 8 | #include 9 | 10 | int main( int argc, char **args ) 11 | { 12 | uint32_t file_crc32; 13 | uint32_t str_crc32; 14 | 15 | if ( argc != 2 ) { 16 | printf("Usage: crc32 file path\n"); 17 | return 0; 18 | } 19 | 20 | printf("+---test cel_crc32_hash: \n"); 21 | str_crc32 = cel_crc32_hash("chenxin619315@gmail.com"); 22 | printf("string crc32: 0x%x\n", str_crc32); 23 | putchar('\n'); 24 | 25 | printf("+---test cel_crc32_file: \n"); 26 | cel_crc32_file(args[1], &file_crc32); 27 | printf("file crc32: 0x%x\n", file_crc32); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /src/test/tst-format.c: -------------------------------------------------------------------------------- 1 | /** 2 | * format change test program. 3 | * 4 | * @author chenxin 5 | */ 6 | #include 7 | 8 | int main ( int argc, char *args[] ) 9 | { 10 | char buff[64]; 11 | if ( sprintf(buff, "%d%c", 32, 'A') != -1 ) 12 | printf("%s", buff); 13 | putchar('\n'); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /src/test/tst-hashmap.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel hashmap test program . 3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_hashmap.h" 7 | #include 8 | #include 9 | 10 | int main( int argc, char *args[] ) 11 | { 12 | uint_t i, length; 13 | //cel_hashmap_t *map = new_cel_hashmap(); 14 | cel_hashmap_t hmap, *map; 15 | map = &hmap; 16 | cel_hashmap_create(&hmap, 16, 0.75); 17 | 18 | char *name[] = { 19 | "chenmanwen", "yangqinghua", "chenxin", "yanzi", "yangjian", "zhangrenfang", 20 | "panzi", "zhangqiang", "webssky", "jcseg", "friso", "robbe", "lionsoul", 21 | "tankwar", "jteach" 22 | }; 23 | length = 15; 24 | 25 | //test put 26 | printf("+---test put: \n"); 27 | for ( i = 0; i < length; i++ ) 28 | { 29 | printf("put(%15s, %15s);\n", name[i], name[i]); 30 | cel_hashmap_put( map, name[i], name[i] ); 31 | } 32 | printf("length: %d, size: %d, threshold: %d\n", 33 | cel_hashmap_length(map), 34 | cel_hashmap_size(map), 35 | cel_hashmap_threshold(map)); 36 | putchar('\n'); 37 | 38 | //test get 39 | printf("+---test get: \n"); 40 | for ( i = 0; i < length; i++ ) 41 | printf("get(%15s): %15s\n", name[i], (char *)cel_hashmap_get(map, name[i])); 42 | printf("get(%15s): %15s\n", "lionsoul", (char *)cel_hashmap_get(map, "lionsoul")); 43 | putchar('\n'); 44 | 45 | //test exists 46 | printf("+---test exists: \n"); 47 | for ( i = 0; i < length; i++ ) 48 | printf("exists(%15s): %d\n", name[i], cel_hashmap_exists(map, name[i])); 49 | printf("exists(%15s): %d\n", "lionsoul", cel_hashmap_exists(map, "lionsoul")); 50 | putchar('\n'); 51 | 52 | //test set 53 | printf("+---test set: \n"); 54 | printf("set(%15s, %15s): %15s\n", "chenmanwen", "father", 55 | (char *)cel_hashmap_set(map, "chenmanwen", "father")); 56 | printf("set(%15s, %15s): %15s\n", "yangqinghua", "mother", 57 | (char *)cel_hashmap_set(map, "yangqinghua", "mother")); 58 | putchar('\n'); 59 | 60 | //test get 61 | printf("+---test get: \n"); 62 | for ( i = 0; i < length; i++ ) 63 | { 64 | printf("get(%15s): %15s\n", name[i], (char *)cel_hashmap_get(map, name[i])); 65 | } 66 | putchar('\n'); 67 | 68 | //test remove 69 | printf("+---test remove: \n"); 70 | printf("remove(%15s): %15s\n", "lionsoul", (char *)cel_hashmap_remove(map, "lionsoul", NULL)); 71 | printf("remove(%15s): %15s\n", "chenxin", (char *)cel_hashmap_remove(map, "chenxin", NULL)); 72 | printf("size: %d\n", cel_hashmap_size(map)); 73 | 74 | //free the hashmap 75 | //free_cel_hashmap(&map, NULL); 76 | cel_hashmap_destroy(map, NULL); 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /src/test/tst-ihashmap.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel ihashmap test program. 3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_hashmap.h" 7 | #include 8 | 9 | int main( int argc, char *args[] ) 10 | { 11 | //cel_ihashmap_t *imap = new_cel_ihashmap(); 12 | cel_ihashmap_t ihmap, *imap; 13 | imap = &ihmap; 14 | cel_ihashmap_create(&ihmap, 16, 0.75); 15 | 16 | printf("+---test put: \n"); 17 | cel_ihashmap_put(imap, "01", 1); 18 | cel_ihashmap_put(imap, "02", 2); 19 | cel_ihashmap_put(imap, "03", 3); 20 | cel_ihashmap_put(imap, "04", 4); 21 | cel_ihashmap_put(imap, "05", 5); 22 | cel_ihashmap_put(imap, "06", 6); 23 | cel_ihashmap_put(imap, "07", 7); 24 | cel_ihashmap_put(imap, "08", 8); 25 | printf("size=%d, length=%d\n", cel_ihashmap_size(imap), cel_ihashmap_length(imap)); 26 | 27 | printf("+---test set: \n"); 28 | printf("set(\"03\", 10)=%d\n", cel_ihashmap_set(imap, "03", 10)); 29 | printf("set(\"06\", 12)=%d\n", cel_ihashmap_set(imap, "06", 12)); 30 | 31 | uint_t i; 32 | char buff[3] = {0}; 33 | printf("+---test get: \n"); 34 | for ( i = 1; i <= 8; i++ ) 35 | { 36 | buff[0] = '0'; 37 | buff[1] = i+'0'; 38 | printf("get(\"%s\")=%d\n", buff, cel_ihashmap_get(imap, buff)); 39 | } 40 | 41 | printf("+---test remove: \n"); 42 | printf("remove(\"02\")=%d\n", cel_ihashmap_remove(imap, "02", NULL)); 43 | printf("remove(\"01\")=%d\n", cel_ihashmap_remove(imap, "01", NULL)); 44 | printf("size=%d, length=%d\n", cel_ihashmap_size(imap), cel_ihashmap_length(imap)); 45 | 46 | printf("+--test exists: \n"); 47 | for ( i = 1; i < 10; i++ ) 48 | { 49 | buff[0] = '0'; 50 | buff[1] = '0'+i; 51 | printf("exists(\"%s\")=%d\n", buff, cel_ihashmap_exists(imap, buff)); 52 | } 53 | 54 | //free_cel_ihashmap( &imap, NULL ); 55 | cel_ihashmap_destroy(imap); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /src/test/tst-intArray.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel int array list test program. 3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_array.h" 7 | 8 | static void print_array( cel_intArray_t *arr ) 9 | { 10 | uint_t i; 11 | printf("length=%d, size=%d: \n", arr->length, arr->size); 12 | for ( i = 0; i < arr->size; i++ ) 13 | printf("%d, ", cel_intArray_get( arr, i )); 14 | putchar('\n'); 15 | } 16 | 17 | int main( int argc, char *args[] ) 18 | { 19 | cel_intArray_t *arr = new_cel_intArray(); 20 | 21 | printf("+---test add: \n"); 22 | cel_intArray_add( arr, 1 ); 23 | cel_intArray_add( arr, 2 ); 24 | cel_intArray_add( arr, 3 ); 25 | cel_intArray_add( arr, 4 ); 26 | cel_intArray_add( arr, 5 ); 27 | cel_intArray_add( arr, 6 ); 28 | cel_intArray_add( arr, 7 ); 29 | cel_intArray_add( arr, 8 ); 30 | cel_intArray_add( arr, 9 ); 31 | print_array( arr ); 32 | 33 | printf("+---test del: \n"); 34 | cel_intArray_del( arr, 0 ); 35 | cel_intArray_del( arr, 0 ); 36 | cel_intArray_del( arr, 100 ); 37 | cel_intArray_del_obj( arr, 3 ); 38 | cel_intArray_del_obj( arr, 100 ); 39 | print_array( arr ); 40 | 41 | printf("+---test set: \n"); 42 | cel_intArray_set( arr, 0, 10 ); 43 | cel_intArray_set( arr, 1, 11 ); 44 | print_array( arr ); 45 | 46 | printf("+---test insert: \n"); 47 | cel_intArray_insert( arr, 0, 1 ); 48 | cel_intArray_insert( arr, 1, 2 ); 49 | print_array( arr ); 50 | 51 | free_cel_intArray( &arr ); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /src/test/tst-link.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../cel_link.h" 5 | 6 | static int string_compare( void * str1, void * str2 ) { 7 | return strcmp((char *) str1, (char *) str2); 8 | } 9 | 10 | #define __PRINT_LINK__ \ 11 | for ( i = 0; i < link->size; i++ ) \ 12 | printf("get(%d): %s\n", i, ( char * ) cel_link_get(link, i)); \ 13 | putchar('\n'); 14 | 15 | int main( int argc, char *args[] ) 16 | { 17 | uint_t i = 0; 18 | //cel_link_t *link = new_cel_link(); 19 | cel_link_t entry, *link; 20 | char * a[] = {"chenxin", "yanzi", "zhangrenfang", "yangjian", "pangzi", "last"}; 21 | 22 | cel_link_create(&entry); 23 | link = &entry; 24 | 25 | //test add 26 | printf("+---element add: \n"); 27 | for ( i = 0; i < 6; i++ ) 28 | cel_link_add_last( link, a[i]); 29 | printf("size: %d\n", cel_link_size(link)); 30 | __PRINT_LINK__ 31 | 32 | //test insert 33 | printf("+---element insert: \n"); 34 | cel_link_insert( link, 0, "yangqinghua" ); 35 | cel_link_insert( link, 0, "chenmanwen" ); 36 | printf("size: %d\n", cel_link_size(link)); 37 | __PRINT_LINK__ 38 | 39 | //test add first 40 | printf("+---element add first: \n"); 41 | cel_link_add_first( link, "first" ); 42 | printf("size: %d\n", cel_link_size(link)); 43 | __PRINT_LINK__ 44 | 45 | //test get 46 | printf("+---element get: \n"); 47 | __PRINT_LINK__ 48 | 49 | 50 | //test set 51 | printf("+---element set: \n"); 52 | printf("set(1): %s\n", (char *) cel_link_set(link, 1, "father")); 53 | printf("set(%s): %s\n", "father", 54 | (char *) cel_link_set_obj(link, "yangqinghua", "mother", string_compare)); 55 | printf("size: %d\n", cel_link_size(link)); 56 | __PRINT_LINK__ 57 | 58 | //test remove first 59 | printf("+---element remove first: \n"); 60 | for ( i = 0; i < 3; i++ ) 61 | printf("+-%d: %s\n", i, (char *) cel_link_remove_first(link)); 62 | printf("size: %d\n", cel_link_size(link)); 63 | __PRINT_LINK__ 64 | 65 | //test remove object 66 | printf("+---element remove object: \n"); 67 | printf("remove(last): %s\n", (char *)cel_link_remove_obj( link, "last", string_compare )); 68 | printf("size: %d\n", cel_link_size(link)); 69 | __PRINT_LINK__ 70 | 71 | //test remove last 72 | printf("+---element remove last: \n"); 73 | for ( i = 0; i < 5; i++ ) 74 | printf("+-%d: %s\n", i, (char *) cel_link_remove_last(link)); 75 | printf("size: %d\n", cel_link_size(link)); 76 | __PRINT_LINK__ 77 | 78 | 79 | //free_cel_link( &link, NULL ); 80 | cel_link_destroy(link, NULL); 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /src/test/tst-lzw.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel lzw comnpress module test program. 3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_compress.h" 7 | #include "../cel_array.h" 8 | #include "../cel_string.h" 9 | #include 10 | #include 11 | 12 | int main( int argc, char *args[] ) 13 | { 14 | //char * str = "ababcdefefgggghihijklmnmnopqrsoptuvwxyzxyzgg."; 15 | char *str = "AAAAAAABBBBBBABABABCDCDCDEFGHIJKLMNOPPQOPRSTUVWXYZZL"; 16 | //char * str = "the/rain/in/Spain/falls/mainly/on/the/plain"; 17 | //char * str = "where there is a will and there is a way"; 18 | //char * str = "ababcdefgefg"; 19 | //char * str = "everyone has potential energy, which is easily concealed by habits, blurred by time, by laziness(inertia)"; 20 | //char * str = "http://translate.google.cn/#zh-CN/en/%E5%BE%88%E5%AE%B9%E6%98%93"; 21 | //char * str = "测试lzw压缩算法的测试程序-研究生命起源起源, 研研究生测试算法lzw的程序."; 22 | uint_t len = strlen(str); 23 | cel_strbuff_t *ensb = new_cel_strbuff(); 24 | cel_strbuff_t *desb = new_cel_strbuff(); 25 | 26 | printf("data=%s, size=%d\n", str, len); 27 | 28 | printf("+---test encode: \n"); 29 | int r = cel_lzw_encode_string(str, len, ensb); 30 | if ( r == 0 ) 31 | printf("error: unable to compress the string."); 32 | else { 33 | printf("data=%s\n", ensb->buffer); 34 | printf("size=%d, radio=%.2f\n", ensb->size, ((float)ensb->size)/((float)len)); 35 | } 36 | putchar('\n'); 37 | 38 | 39 | printf("+---test decode: \n"); 40 | r = cel_lzw_decode_string(ensb->buffer, ensb->size, desb); 41 | if ( r == 0 ) 42 | printf("error: unable to decompress the string."); 43 | else { 44 | printf("+status: %s\n", strcmp(str, desb->buffer)==0?"ok":"no"); 45 | printf("data=%s\n", desb->buffer); 46 | printf("B::opacity=%d, size=%d\n", desb->opacity, desb->size); 47 | } 48 | putchar('\n'); 49 | 50 | free_cel_strbuff(&ensb); 51 | free_cel_strbuff(&desb); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /src/test/tst-md5.c: -------------------------------------------------------------------------------- 1 | /** 2 | * md5 test program.
3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_hash.h" 7 | #include 8 | 9 | static void md5_print( uchar_t * digest ) 10 | { 11 | uint_t j; 12 | 13 | for ( j = 0; j < 16; j++ ) 14 | printf("%02x", digest[j]); 15 | } 16 | 17 | int main( int argc, char *args[] ) 18 | { 19 | cstring str[] = {"", "a", "abc", "message digest", 20 | "123456", "153759", "619315", 21 | "abcdefghijklmnopqrstuvwxyz"}; 22 | char filename[1024] = {0}; 23 | char md5_str[33]; 24 | 25 | uint_t length = 8, i; 26 | uchar_t digest[17] = {0}; 27 | 28 | for ( i = 0; i < length; i++ ) 29 | { 30 | cel_md5_string( str[i], digest ); 31 | printf("cel_md5_string(\"%-26s\")=", str[i]); 32 | cel_md5_print(digest, md5_str); 33 | printf("%s", md5_str); 34 | putchar('\n'); 35 | } 36 | putchar('\n'); 37 | 38 | printf("Enter file path: "); 39 | scanf("%s", filename); 40 | printf("File: %s\n", filename); 41 | if ( cel_md5_file( filename, digest ) == 0 ) 42 | { 43 | printf("md5(\"%s\")=", filename); 44 | md5_print( digest ); 45 | putchar('\n'); 46 | } else 47 | printf("file not exists!"); 48 | 49 | putchar('\n'); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /src/test/tst-md5.php: -------------------------------------------------------------------------------- 1 | 4 | * 5 | * @author chenxin 6 | */ 7 | $_str = array("", "a", "abc", "message digest", 8 | "123456", "153759", "619315", "abcdefghijklmnopqrstuvwxyz"); 9 | 10 | foreach ( $_str as $_val ) 11 | { 12 | //echo "md5(\"".$_val."\")=" . md5( $_val )."\n"; 13 | printf("md5(\"%-26s\")=%s\n", $_val, md5($_val)); 14 | } 15 | ?> 16 | -------------------------------------------------------------------------------- /src/test/tst-mem.c: -------------------------------------------------------------------------------- 1 | /** 2 | * mem module test program. 3 | * 4 | * @author chenxi 5 | */ 6 | #include "../cel_mem.h" 7 | #include 8 | 9 | int main( int argc, char *args[] ) 10 | { 11 | char * a = "i love c"; 12 | char b[65] = {0}; 13 | 14 | printf("sizeof(ulong_t)=%d\n", sizeof(ulong_t)); 15 | //uchar_t * c = ( uchar_t * ) a; 16 | //printf("%d\n", CEL_BIT_FETCH(c, 16) != 0); 17 | //printf("%d\n", CEL_BIT_FETCH(c, 17) != 0); 18 | //printf("%d\n", CEL_BIT_FETCH(c, 18) != 0); 19 | //printf("%d\n", CEL_BIT_FETCH(c, 19) != 0); 20 | //printf("%d\n", CEL_BIT_FETCH(c, 20) != 0); 21 | //printf("%d\n", CEL_BIT_FETCH(c, 21) != 0); 22 | //printf("%d\n", CEL_BIT_FETCH(c, 22) != 0); 23 | //printf("%d\n", CEL_BIT_FETCH(c, 23) != 0); 24 | 25 | printf("+---test mem store: \n"); 26 | cel_mem_bits("init: ", a, 8); 27 | cel_mem_store(a, 16, 8, b, 0); 28 | cel_mem_bits("_dst: ", b, 8); 29 | printf("a=%s, b=%s\n\n", a, b); 30 | 31 | //int x = 10, y; 32 | short x = 6, y; 33 | 34 | printf("+---test mem fetch: \n"); 35 | cel_mem_fetch(&x, 0, 16, &y, 0); 36 | printf("x=%d, y=%d\n", x, y); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /src/test/tst-rle.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Run length encoding compress algorithim test program. 3 | * 4 | * @author chenxin 5 | * @date 2013-09-20 6 | */ 7 | #include "../cel_compress.h" 8 | #include "../cel_string.h" 9 | #include 10 | #include 11 | 12 | int main( int argc, char *args[] ) 13 | { 14 | uint_t inlen, flen; 15 | //char * str = "#0AAABBBBCCCCCDEFGGGGGGHHIJKLMNOPQRRSTTTTT"; 16 | //char * str = "#1234567891011"; 17 | //char * str = "ABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABA"; 18 | //char * str = "{{{{AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABCDEFGHIJKLMNOPQRSTUVWXYZ#}}}}"; 19 | char * str = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABABAB"; 20 | cel_strbuff_t ensb = new_cel_strbuff(); 21 | cel_strbuff_t desb = new_cel_strbuff(); 22 | inlen = strlen(str); 23 | printf("inlen=%d\n", inlen); 24 | 25 | printf("+---test encode: \n"); 26 | printf("rle_encode(\"%s\")\n", str); 27 | flen = cel_rle_encode_string( str, (uint_t) strlen(str), ensb ); 28 | //Count the compress 29 | printf("ensb->buffer=%s\n", ensb->buffer); 30 | printf("inlen=%d, flen=%d, bit=%.2f\n", inlen, 31 | ensb->length, ((float)ensb->length/(float)inlen)); 32 | 33 | putchar('\n'); 34 | printf("+---test decode: \n"); 35 | printf("rle_decode(\"%s\")\n", ensb->buffer); 36 | cel_rle_decode_string( ensb->buffer, ensb->length, desb ); 37 | printf("desb=%s\n", desb->buffer); 38 | 39 | free_cel_strbuff(ensb); 40 | free_cel_strbuff(desb); 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /src/test/tst-rlefile.c: -------------------------------------------------------------------------------- 1 | /** 2 | * RLE file compress test program. 3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_compress.h" 7 | #include "../cel_stdio.h" 8 | #include 9 | 10 | #define inputFileName(infile, outfile)\ 11 | printf("source file: "); \ 12 | scanf("%s", infile); \ 13 | printf("destination file: "); \ 14 | scanf("%s", outfile); 15 | 16 | int main( int argc, char *args[] ) 17 | { 18 | char infile[128]; 19 | char outfile[128]; 20 | uint_t len, flen; 21 | 22 | printf("+---test encode: \n"); 23 | inputFileName( infile, outfile ); 24 | //encode the file. 25 | len = cel_filesize(infile); 26 | if ( len == -1 ) 27 | { 28 | printf("Error: file not exists\n"); 29 | exit(0); 30 | } 31 | flen = cel_rle_encode_file( infile, outfile ); 32 | printf("srcLen=%d, dstLen=%d, bit=%.2f\n", len, flen, ((float)flen)/((float)len)); 33 | putchar('\n'); 34 | 35 | printf("+---test decode: \n"); 36 | inputFileName( infile, outfile ); 37 | 38 | //get the source file size 39 | len = cel_filesize(infile); 40 | if ( len == -1 ) 41 | { 42 | printf("Error: file not exists\n"); 43 | exit(0); 44 | } 45 | flen = cel_rle_decode_file( infile, outfile ); 46 | printf("srcLen=%d, dstLen=%d\n", len, flen ); 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /src/test/tst-sort.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel sort test program. 3 | * 4 | * test report: 5 | * 1. environment: ubuntu/2G/AMD 2.8GHZ 6 | * 2. result: (take 1000000 random integer as sample) 7 | * +------------+---------------+ 8 | * | Algorithm | Time(sec) | 9 | * +------------+---------------+ 10 | * | insertion | forget it | 11 | * +------------+---------------+ 12 | * | shell | 1.430000 | 13 | * +------------+---------------+ 14 | * | merge | 1.250000 | 15 | * +------------+---------------+ 16 | * | quick | 0.430000 | 17 | * +------------+---------------+ 18 | * | bucket(1) | 0.050000 | 19 | * +------------+---------------+ 20 | * 21 | * @author chenxin 22 | */ 23 | #include "../cel_sort.h" 24 | #include "../cel_mem.h" 25 | #include 26 | #include 27 | #include 28 | 29 | 30 | #define LEN 1000000 31 | 32 | void test_swap( void ); 33 | void random_array( uint_t *, uint_t ); 34 | int int_compare( void *, void * ); 35 | void print_r( uint_t *, uint_t ); 36 | 37 | int main( int argc, char *args[] ) 38 | { 39 | //uint_t a[] = {5, 4, 3, 9, 20, 2, 7, 17, 12, 10, 8, 15, 36, 24, 18, 16, 10, 6, 11, 13}; 40 | //uint_t LEN = 20; 41 | //test_swap(); 42 | 43 | uint_t a[LEN]; 44 | random_array(a, LEN); 45 | 46 | clock_t s = clock(); 47 | //cel_subinsert_sort( a, sizeof(int), int_compare, 0, 8 ); 48 | //cel_subinsert_sort( a, sizeof(int), int_compare, 9, 19 ); 49 | //cel_insertion_sort(a, LEN, sizeof(int), int_compare); 50 | //cel_shell_sort( a, LEN, sizeof(int), int_compare ); 51 | //cel_merge_sort( a, LEN, sizeof(int), int_compare ); 52 | //cel_quick_sort( a, LEN, sizeof(int), int_compare); 53 | cel_bucket_sort(a, LEN, 1); 54 | clock_t e = clock(); 55 | 56 | printf("Done, cost %f sec\n", ( (double)(e -s) / CLOCKS_PER_SEC) ); 57 | //print_r(a, LEN); 58 | 59 | return 0; 60 | } 61 | 62 | int int_compare( void * a, void * b ) 63 | { 64 | int * __a = (int *) a; 65 | int * __b = (int *) b; 66 | if ( *__a > * __b ) return 1; 67 | if ( *__a < * __b ) return -1; 68 | return 0; 69 | } 70 | 71 | //generate a random array. 72 | void random_array( uint_t * a, uint_t length ) 73 | { 74 | uint_t i; 75 | 76 | for ( i = 0; i < length; i++ ) 77 | a[i] = i; 78 | 79 | //set the seed of the rand. 80 | srand((unsigned) time(NULL)); 81 | for ( i = length - 1; i > 0; i-- ) 82 | cel_mem_swap(a+i, a + (rand() % i), sizeof(uint_t)); 83 | } 84 | 85 | void print_r( uint_t * a, uint_t length ) 86 | { 87 | register uint_t i; 88 | for ( i = 0; i < length; i++ ) { 89 | if ( i == 0 ) 90 | printf("%d", a[i]); 91 | else 92 | printf(", %d", a[i]); 93 | } 94 | putchar('\n'); 95 | } 96 | 97 | //test cel_swap and cel_copy 98 | void test_swap() 99 | { 100 | int a = 1, b = 2; 101 | int c; 102 | 103 | printf("+---test copy: \n"); 104 | cel_mem_copy( &a, &c, sizeof(int) ); 105 | printf("c=%d\n", c); 106 | putchar('\n'); 107 | 108 | printf("+---test swap: \n"); 109 | printf("before swap: a=%d, b=%d\n", a, b); 110 | cel_mem_swap(&a, &b, sizeof(int)); 111 | printf("after swap: a=%d, b=%d\n", a, b); 112 | } 113 | -------------------------------------------------------------------------------- /src/test/tst-strbuff.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel string module test program.
3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_string.h" 7 | 8 | int main( int argc, char * args[] ) 9 | { 10 | uint_t i; 11 | cstring str[] = {"a", "bc", "defg", "hijklmn", "opqrstyvwxyz"}; 12 | uint_t opacity = 5; 13 | 14 | 15 | //sb = new_cel_strbuff_opacity(10); 16 | //sb = new_cel_strbuff(); 17 | 18 | cel_strbuff_t *sb; 19 | sb = new_cel_strbuff_string("{"); 20 | 21 | //cel_strbuff_t buff, *sb; 22 | //cel_strbuff_create(&buff, 8, "{"); 23 | //sb = &buff; 24 | 25 | //cel_strbuff_entry strbuff; 26 | //sb = &strbuff; 27 | //cel_strbuff_init( sb, 10, "{" ); 28 | 29 | printf("sb->opacity=%d, sb->opacity=%d\n", sb->opacity, sb->opacity); 30 | 31 | printf("+---test append: \n"); 32 | for ( i = 0; i < opacity; i++ ) 33 | { 34 | cel_strbuff_append(sb, str[i], 1); 35 | printf("append(\"%-15s\")=%-15s\n", str[i], sb->buffer); 36 | } 37 | cel_strbuff_append_char(sb, '1', 2); 38 | printf("append('1')=%s\n", sb->buffer); 39 | 40 | cel_strbuff_append_char( sb, '2', 1 ); 41 | printf("append('2')=%s\n", sb->buffer); 42 | 43 | cel_strbuff_append_char( sb, '}', 1 ); 44 | printf("append('}')=%s\n", sb->buffer); 45 | 46 | printf("sb->opacity=%d, sb->opacity=%d\n", sb->opacity, sb->opacity); 47 | putchar('\n'); 48 | 49 | printf("+---test insert: \n"); 50 | cel_strbuff_insert( sb, 0, "#!", 1 ); 51 | printf("insert(0, \"#1\")=%s\n", sb->buffer); 52 | 53 | i = sb->opacity / 2; 54 | cel_strbuff_insert( sb, i, "(@)", 1 ); 55 | printf("insert(%d, \"(@)\")=%s\n", i, sb->buffer); 56 | 57 | i = sb->opacity; 58 | cel_strbuff_insert( sb, i, "!#", 1 ); 59 | printf("insert(%d, \"!#\")=%s\n", i, sb->buffer); 60 | 61 | i = sb->opacity; 62 | cel_strbuff_insert_char( sb, sb->opacity, '+', 2 ); 63 | printf("insert(%d, '+')=%s\n", i, sb->buffer ); 64 | printf("sb->opacity=%d, sb->opacity=%d\n", sb->opacity, sb->opacity); 65 | putchar('\n'); 66 | 67 | printf("+---test remove: \n"); 68 | cel_strbuff_remove( sb, 0, 2 ); 69 | printf("remove(0, 2)=%s\n", sb->buffer); 70 | 71 | i = sb->opacity / 2; 72 | cel_strbuff_remove( sb, i, 3 ); 73 | printf("remove(%d, 3)=%s\n", i, sb->buffer ); 74 | 75 | i = sb->opacity; 76 | cel_strbuff_remove( sb, i - 4, 4 ); 77 | printf("remove(%d, 2)=%s\n", i, sb->buffer ); 78 | 79 | printf("sb->opacity=%d, sb->opacity=%d\n", sb->opacity, sb->opacity); 80 | putchar('\n'); 81 | 82 | //Free the string buffer. 83 | free_cel_strbuff( &sb ); 84 | //cel_strbuff_destroy(sb); 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /src/test/tst-strbuff2.c: -------------------------------------------------------------------------------- 1 | /** 2 | * cel string buffer test program. 3 | * 4 | * @author chenxin 5 | */ 6 | #include "../cel_string.h" 7 | #include 8 | 9 | int main( int argc, char *args[] ) 10 | { 11 | cel_strbuff_t *sb = new_cel_strbuff(); 12 | char * str = "ababcdefefgggghihijklmnmnopqrsoptuvwxyzxyzgg"; 13 | 14 | //apend the string. 15 | int i; 16 | for ( i = 0; i < 44; i++ ) 17 | cel_strbuff_append_char(sb, str[i], 1); 18 | 19 | cel_strbuff_append(sb, ".", 1); 20 | printf("length=%d, size=%d\n", sb->opacity, sb->size); 21 | printf("buffer=%s\n", sb->buffer); 22 | 23 | free_cel_strbuff(&sb); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /src/test/tst-ucode.c: -------------------------------------------------------------------------------- 1 | /** 2 | * utf-8 unicode convert interface. 3 | * 4 | * @author chenxin 5 | */ 6 | #include 7 | #include 8 | #include "../cel_string.h" 9 | 10 | int main( int argc, char *args[] ) 11 | { 12 | cstring str = "平凡|执着"; 13 | int i, j, length = strlen( str ); 14 | uint_t ucode[5], bytes = 0; 15 | 16 | char buffer[13] = {0}; 17 | cstring buff = buffer; 18 | 19 | //Utf-8 to unicode. 20 | for ( i = 0, j = 0; i < length; j++ ) 21 | { 22 | //printf("bytes=%d, i=%d\n", bytes, i); 23 | ucode[j] = cel_utf8_to_ucode( &str[i] ); 24 | bytes = cel_utf8_charbytes( str[i] ); 25 | i += bytes; 26 | printf("%d, ", ucode[j]); 27 | } 28 | putchar('\n'); 29 | 30 | //Unicode to utf-8 31 | for ( i = 0; i < 5; i++ ) 32 | { 33 | j = cel_ucode_to_utf8( ucode[i], buff ); 34 | buff += j; 35 | } 36 | printf("buffer=%s\n", buffer); 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /src/test/tst.c: -------------------------------------------------------------------------------- 1 | /** 2 | * test program. 3 | * 4 | * @author chenxin 5 | */ 6 | #include 7 | #include "../cel_mem.h" 8 | 9 | int main( int argc, char *args[] ) 10 | { 11 | uint_t a = 65, i; 12 | char * str = ( char * ) &a; 13 | 14 | printf("sizeof(ullong_t)=%d\n", sizeof(ullong_t)); 15 | printf("sizeof( ulong_t)=%d\n", sizeof(ulong_t)); 16 | printf("sizeof( uint_t)=%d\n", sizeof(uint_t)); 17 | printf("sizeof(ushort_t)=%d\n", sizeof(ushort_t)); 18 | printf("sizeof( uchar_t)=%d\n", sizeof(uchar_t)); 19 | putchar('\n'); 20 | 21 | //little-endian for local store. 22 | printf("+---test char ptr: \n"); 23 | for ( i = 0; i < sizeof(uint_t); i++ ) 24 | printf("byte-%d, %2c, %p\n", i, str[i], &str[i] ); 25 | putchar('\n'); 26 | 27 | //package them in bit-endian. 28 | printf("+---test bits opt: \n"); 29 | printf("((a>>24) & 0xFF)=%2c\n", (char)((a>>24) & 0xFF)); 30 | printf("((a>>16) & 0xFF)=%2c\n", (char)((a>>16) & 0xFF)); 31 | printf("((a>> 8) & 0xFF)=%2c\n", (char)((a>> 8) & 0xFF)); 32 | printf("((a>> 0) & 0xFF)=%2c\n", (char)((a>> 0) & 0xFF)); 33 | 34 | putchar('\n'); 35 | return 0; 36 | } 37 | --------------------------------------------------------------------------------