CVector.h
// // cvector.h // GKApp // // Created by 王明輝 on 16/4/15. // Copyright (c) 2016年 GK. All rights reserved. // #ifndef GCVECTOR_H #define GCVECTOR_H #include "gtypes.h" #include "seg_types.h" #define MIN_LEN 256 //#define CVEFAILED -1 //#define CVEERRORPARAM -2 #define CVESUCCESS 0 //#define CVEPUSHBACK 1 //#define CVEPOPBACK 2 #define CVEINSERT 3 #define CVERM 4 #define EXPANED_VAL 1 #define REDUSED_VAL 2 /** * 錯誤碼枚舉類型 * 定義錯誤碼枚舉值 */ typedef enum tagCVECTORSTATUS { CV_ERR_OK =0x00000000, /**< 操作成功 */ CV_ERR_INVALID_PARAM =0x00000001, /**< 參數無效 */ CV_ERR_NO_MEMORY =0x00000002, /**< 內存不足 */ CV_ERR_NO_DATA =0x00000003, /**< 無相關數據 */ CV_ERR_VER_INCOMPATIBLE =0x00000004, /**< 版本不匹配 */ CV_ERR_PBACK =0x00000007, /**< 沒有后退 */ CV_ERR_PUSHBACK =0x00000008, CV_ERR_INSERT =0x00000009, /**< 插值失敗,回滾數據 */ CV_ERR_FAILED =0xffffffff /**< 操作失敗,暫無具體錯誤碼 */ } CVECTORSTATUS; typedef enum tagCVECTORSTRUCT { CV_BASE_STRUCT =0x00000000, /**< CVECTOR 對象 */ CV_TEMP_STRUCT =0x00000030, /**< TEMPCVECTOR */ } CVECTORSTRUCTS; typedef struct tagCVector { void *cv_pdata; Gint32 cv_len;//元素個數 Gint32 cv_tot_len;//空間總長度 Gint32 cv_size;//元素長度 CVECTORSTRUCTS cv_struct;//type }tagCVector, *CVector; typedef void *CIterator; //typedef struct tagCVector *CVector; #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ /** ********************************************************************** \brief 進行比較的函數回調 \details 該函數用於進行比較的函數回調 \param[in] Target 需要比較的對象A \param[in] arrayObj 需要比較的對象B \return 1:pvTarget>arrayObj 0:等於 -1:pvTarget<arrayObj **********************************************************************/ typedef Gint32 (*CIteratorCompareFunc)(void* pvTarget, CIterator arrayObj); /** ********************************************************************** \brief 創建CVector對象 \details 創建CVector對象 \param[in] hCVector 需要創建的CVector對象 \param[in] len: 元素的長度 \param[in] size: 單個元素的字節 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ CVECTORSTATUS CVector_Create(const CVector hCVector,Gint32 len ,const Gint32 size); /** ********************************************************************** \brief 釋放CVector對象 \details 釋放CVector對象 \param[in] hCVector 需要釋放的CVector對象 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ CVECTORSTATUS CVector_Destroy(const CVector hCVector); /** ********************************************************************** \brief 判斷是否為空 \details 判斷是Vector否為空 \param[in] hCVector CVector對象 \return Gtrue 空 Gfalse 非空 **********************************************************************/ Gbool CVector_Empty(const CVector hCVector); /** ********************************************************************** \brief 清空緩存 \details 清空緩存 \param[in] hCVector CVector對象 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ CVECTORSTATUS CVector_Clear(const CVector hCVector); /** ********************************************************************** \brief 重置結構體的元素空間占用 \details 重置結構體的元素空間,重新設定長度,及元素的空間占用 \param[in] hCVector CVector對象 \param[in] len 元素的長度 \param[in] size 單個元素的字節 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ CVECTORSTATUS CVector_Resize(const CVector hCVector,Gint32 len ,Gint32 nSize); /** ********************************************************************** \brief 返回指定元素個數 \details 返回指定元素個數 \param[in] hCVector CVector對象 \return 返回元素個數 **********************************************************************/ Gint32 CVector_Length(const CVector hCVector); /** ********************************************************************** \brief 將數據插入到CVector \details 將數據插入CVector的最后位置 memb = GNULL 時將插入nLen個元素的'0'數據,CVector_NewEmpty就是利用這個特性完成的添加空數據功能 \param[in] hCVector CVector對象 \param[in] memb 插入數據的指針 \param[in] nLen 元素的長度 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ CVECTORSTATUS CVector_Pushback(const CVector hCVector, void *memb,Gint32 nLen); /** ********************************************************************** \brief 獲取最后一個元素並刪除該元素 \details 獲取最后一個元素並刪除該元素 \param[in] hCVector CVector對象 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ CVECTORSTATUS CVector_Popback(const CVector hCVector); /** ********************************************************************** \brief 創建空元素 \details 在Vector中添加空元素並返回該元素所對應的指針 \param[in] hCVector CVector對象 \param[in] pvIter 元素對象 \return 返回當前元素對象指針 **********************************************************************/ CIterator CVector_NewEmpty(const CVector hCVector); /** ********************************************************************** \brief 在index位置插入到指定元素 \details 在index位置插入到指定元素 \param[in] hCVector CVector對象 \param[in] nIdx 插入位置 \param[in] memb 需要插入的元素指針 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ CVECTORSTATUS CVector_Insert_Idx(const CVector hCVector, Gint32 nIdx, void *memb); /** ********************************************************************** \brief 在pvIter位置插入到指定元素 \details 在pvIter位置插入到指定元素 \param[in] hCVector CVector對象 \param[in] pvIter 插入位置 \param[in] memb 需要插入的元素指針 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ CVECTORSTATUS CVector_Insert(const CVector pstVector, CIterator pvIter, void *memb); /** ********************************************************************** \brief 刪除指定元素 \details 刪除指定元素 \param[in] hCVector CVector對象 \param[in] memb 需要插入的元素指針 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ //刪除指定元素 CVECTORSTATUS CVector_Rm(const CVector hCVector, CIterator pvIter); /** ********************************************************************** \brief 刪除指定位置的元素 \details 刪除指定位置的元素 \param[in] hCVector CVector對象 \param[in] nIdx 插入位置 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ CVECTORSTATUS CVector_Rm_Idx(const CVector hCVector, Gint32 nIdx); /** ********************************************************************** \brief 根據元素返回該元素所對應的位置 \details 根據元素返回該元素所對應的位置 \param[in] hCVector CVector對象 \param[in] pvIter 元素對象 \return 返回當前元素所在位置 **********************************************************************/ Gint32 CVector_At(const CVector hCVector, CIterator pvIter); /** ********************************************************************** \brief 根據索引Id返回該元素所對應的指針 \details 根據索引Id返回該元素所對應的指針 \param[in] hCVector CVector對象 \param[in] nIdx 索引ID \return 返回指定元素 **********************************************************************/ CIterator CVector_At_Idx(const CVector hCVector, Gint32 nIdx); /** ********************************************************************** \brief 元素交換 \details 元素交換 \param[in] hCVector CVector對象 \param[in] pvIter1 需要交換的元素 \param[in] pvIter2 需要交換的元素 \return CV_ERR_OK 成功 CV_ERR_INVALID_PARAM 參數錯誤 CV_ERR_FAILED 失敗 **********************************************************************/ CVECTORSTATUS CVector_Swap(const CVector cv, CIterator pvIter1, CIterator pvIter2); //----------------擴展-----------------------------// /** ********************************************************************** \brief 求兩個Gint32數組的非集 \details 該函數用於求兩個Gint32數組的非集 \param[in] pstMutableCache 緩存數組 \param[in] pvnIndexB 需要合並的數組 \param[in] nIdxCntB 需要合並數組A長度 \return 返回合並是否成功 **********************************************************************/ CVECTORSTATUS CVector_Negation(CVector pstVector, void *pvnIndexB, Gint32 nIdxCntB); /** ********************************************************************** \brief 搜索結果索引排序 \details 搜索結果索引排序 \param[in] pvnList 數組, 無序 \param[in] nStartIdx 起始下標 \param[in] nSortCnt 滿足排序要求的最小個數,優化效率,為0時為全排 \param[in] bRepeat 是否去重 \param[in] pstCompare_func 排序來確認是升序還是降序 \param[out] pvnList 數組, 有序 \retval 排序后個數 **********************************************************************/ Gint32 CVector_Sort(const CVector pstVector,Gint32 nStartIdx, Gint32 nSortCnt,Gbool bRepeat, CIteratorCompareFunc pstCompare_func); /** ********************************************************************** \brief 求兩個Gint32數組的並集 \details 該函數用於求兩個Gint32數組的並集 \param[in] pstMutableCache 緩存數組 \param[in] pvnIndexB 需要合並的數組 \param[in] nIdxCntB 需要合並數組A長度 \return 返回合並是否成功 **********************************************************************/ CVECTORSTATUS CVector_Merge(CVector pstVector,void *pvnIndexB,Gint32 nIdxCntB); /** ********************************************************************** \brief 求兩個Gint32數組的交集 \details 該函數用於求兩個Gint32數組的交集 \param[in] pstMutableCache 緩存數組 \param[in] pvnIndexB 需要合並的數組 \param[in] nIdxCntB 需要合並數組A長度 \return 返回合並是否成功 **********************************************************************/ CVECTORSTATUS CVector_Intersect(CVector pstVector,void *pvnIndexB,Gint32 nIdxCntB); //注: 針對的是調用 Sort 排好序的 Array 容器才可以使用 //[in] poArray: 源數據數組指針 //[in] nArrayCnt: 源數據數組個數 //[in] nStructSize: 單個數組元素結構體占用的空間數 //[in] Target: 需要比較的對象 //[Out] pnOutPos: 結果所在位置 // 返回值: 返回找到的對象 CIterator CVector_IteratorFindByObj(CVector pstVector,CIteratorCompareFunc compare_func,void * pvTarget, Gint32 * pnOutPos); /* for test */ void cv_info(const CVector cv); void cv_print(const CVector cv); void CVector_Printf(const CVector cv); void CVector_Test(); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* EOF file cvector.h */
CVector.c
1 // 2 // cvector.c 3 // GKApp 4 // 5 // Created by wmh on 16/4/15. 6 // Copyright © 2016年 GK. All rights reserved. 7 // 8 9 #include "segl_cvector.h" 10 #include "segl_tempcache.h" 11 #include "segl_utils.h" 12 #include "segl_cvectorcompare.h" 13 #include "libgdc.h" 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 //#include <unistd.h> 18 #define EXPANED_VAL 1 19 #define REDUSED_VAL 2 20 21 //typedef void *CIterator; 22 23 24 //#define CWARNING_ITER(cv, iter, file, func, line) \ 25 //do {\ 26 //if ((CVector_Begin(cv) > iter) || (CVector_End(cv) <= iter)) {\ 27 //fprintf(stderr, "var(" #iter ") warng out of range, "\ 28 //"at file:%s func:%s line:%d!!/n", file, func, line);\ 29 //return CV_ERR_FAILED;\ 30 //}\ 31 //} while (0) 32 33 //CWARNING_ITER(pstVector, iter, __FILE__, __func__, __LINE__); 34 35 #ifdef GLOG_ON 36 #define CWARNING_CVECTOR(pstCVector,ret) \ 37 do {\ 38 if (pstCVector == GNULL) {\ 39 GLOG_POI_DEBUG(GT("pstCVector is NULL, at file:%s line:%s\n"), __GFILE__, __GLINE__);\ 40 return ret;\ 41 }\ 42 } while (0) 43 #else 44 #define CWARNING_CVECTOR(pstCVector,ret) \ 45 do {\ 46 if (pstCVector == GNULL) {\ 47 return ret;\ 48 }\ 49 } while (0) 50 #endif 51 52 #ifdef GLOG_ON 53 #define CWARNING_ITER(pstCVector, iter,ret) \ 54 do {\ 55 if ((pstCVector == GNULL) ||(CVector_Begin(pstCVector) > iter) || (CVector_End(pstCVector) <= iter)) {\ 56 GLOG_POI_DEBUG(GT("pstCVector is NULL or var(iter) warng out of range, at file:%s line:%s\n"), __GFILE__, __GLINE__);\ 57 return ret;\ 58 }\ 59 } while (0) 60 61 #define CWARNING_ITERS(pstCVector, iter,iter2,ret) \ 62 do {\ 63 if ((pstCVector == GNULL) ||(CVector_Begin(pstCVector) > iter) || (CVector_End(pstCVector) <= iter)||(CVector_Begin(pstCVector) > iter2) || (CVector_End(pstCVector) <= iter2)) {\ 64 GLOG_POI_DEBUG(GT("pstCVector is NULL or var(iter) warng out of range, at file:%s line:%s\n"), __GFILE__, __GLINE__);\ 65 return ret;\ 66 }\ 67 } while (0) 68 69 70 71 #else 72 #define CWARNING_ITER(pstCVector, iter,ret) \ 73 do {\ 74 if ((pstCVector == GNULL) ||(CVector_Begin(pstCVector) > iter) || (CVector_End(pstCVector) <= iter)) {\ 75 return ret;\ 76 }\ 77 } while (0) 78 79 #define CWARNING_ITERS(pstCVector, iter,iter2,ret) \ 80 do {\ 81 if ((pstCVector == GNULL) ||(CVector_Begin(pstCVector) > iter) || (CVector_End(pstCVector) <= iter)||(CVector_Begin(pstCVector) > iter2) || (CVector_End(pstCVector) <= iter2)) {\ 82 return ret;\ 83 }\ 84 } while (0) 85 86 #endif 87 88 //Gint32 CVector_val(const CVector cv, CIterator iter, void *memb); 89 //返回第一個元素 90 CIterator CVector_Begin(const CVector pstCVector); 91 //返回最后一個元素 92 CIterator CVector_End(const CVector pstCVector); 93 //返回下一個元素 94 CIterator CVector_Next(const CVector pstCVector, CIterator iter); 95 96 CVECTORSTATUS CVector_Create(const CVector pstCVector,Gint32 nLen ,const Gint32 nSize) 97 { 98 CWARNING_CVECTOR(pstCVector,CV_ERR_INVALID_PARAM); 99 100 if (nSize <= 0 || nLen <= 0) 101 { 102 return CV_ERR_INVALID_PARAM; 103 } 104 105 if(nLen * nSize > pstCVector->cv_tot_len) 106 { 107 if(pstCVector->cv_tot_len>0) 108 { 109 Gfree(pstCVector->cv_pdata); 110 } 111 pstCVector->cv_pdata = Gmalloc(nLen * nSize); 112 Gmemset(pstCVector->cv_pdata, 0, nLen * nSize); 113 pstCVector->cv_tot_len = nLen * nSize; 114 } 115 116 if (pstCVector->cv_pdata == GNULL) 117 { 118 return CV_ERR_FAILED; 119 } 120 pstCVector->cv_size = nSize; 121 pstCVector->cv_len = 0; 122 return CV_ERR_OK; 123 } 124 CVECTORSTATUS CVector_Resize(const CVector pstCVector, Gint32 nLen, Gint32 nSize) 125 { 126 CVECTORSTATUS ret; 127 CWARNING_CVECTOR(pstCVector,CV_ERR_INVALID_PARAM); 128 if (nSize <= 0) 129 { 130 return CV_ERR_INVALID_PARAM; 131 } 132 133 if(nLen * nSize > pstCVector->cv_tot_len) 134 { 135 CVector_Destroy(pstCVector); 136 ret = CVector_Create(pstCVector, nLen, nSize); 137 if(ret != CV_ERR_OK){ 138 return ret; 139 } 140 } 141 pstCVector->cv_size = nSize; 142 pstCVector->cv_len = 0; 143 return CV_ERR_OK; 144 } 145 146 CVECTORSTATUS CVector_Destroy(CVector pstCVector) 147 { 148 CWARNING_CVECTOR(pstCVector,CV_ERR_INVALID_PARAM); 149 150 if (pstCVector->cv_pdata != GNULL) 151 { 152 Gfree(pstCVector->cv_pdata); 153 pstCVector->cv_pdata = GNULL; 154 } 155 pstCVector->cv_tot_len = 0; 156 pstCVector->cv_size = 0; 157 pstCVector->cv_len = 0; 158 159 return CV_ERR_OK; 160 } 161 162 Gint32 CVector_Length(const CVector pstCVector) 163 { 164 CWARNING_CVECTOR(pstCVector, 0); 165 return pstCVector->cv_len; 166 } 167 //創建空元素 168 CIterator CVector_NewEmpty(const CVector pstVector) 169 { 170 CWARNING_CVECTOR(pstVector, GNULL); 171 if(CV_ERR_OK == CVector_Pushback(pstVector, GNULL, 1)) 172 { 173 if(pstVector->cv_len > 0) 174 { 175 return CVector_At_Idx(pstVector, pstVector->cv_len - 1); 176 } 177 } 178 return GNULL; 179 } 180 181 CVECTORSTATUS CVector_Pushback(const CVector pstVector, void *memb, Gint32 len) 182 { 183 Gint32 tot_len = 0; 184 void *cv_pdata= GNULL; 185 CWARNING_CVECTOR(pstVector,CV_ERR_INVALID_PARAM); 186 if(len <=0) 187 { 188 return CV_ERR_INVALID_PARAM; 189 } 190 tot_len = (pstVector->cv_len+len) * pstVector->cv_size; 191 if( tot_len >= pstVector->cv_tot_len) 192 { 193 tot_len += 50 * pstVector->cv_size; 194 cv_pdata = Gmalloc(tot_len); 195 Gmemset(cv_pdata,0,tot_len); 196 Gmemcpy(cv_pdata,pstVector->cv_pdata, pstVector->cv_tot_len); 197 198 if (GNULL == cv_pdata) 199 { 200 return CV_ERR_PUSHBACK; 201 } 202 Gfree(pstVector->cv_pdata); 203 pstVector->cv_pdata = cv_pdata; 204 pstVector->cv_tot_len = tot_len; 205 } 206 if(memb != GNULL) 207 { 208 Gmemcpy((Gint8*)pstVector->cv_pdata + pstVector->cv_len * pstVector->cv_size, memb, len * pstVector->cv_size); 209 } 210 else 211 { //置空內存,減少錯誤 by wmh 212 Gmemset((Gint8*)pstVector->cv_pdata + pstVector->cv_len * pstVector->cv_size,0,len * pstVector->cv_size); 213 } 214 pstVector->cv_len += len; 215 return CV_ERR_OK; 216 } 217 218 CVECTORSTATUS CVector_Popback(const CVector pstVector) 219 { 220 CWARNING_CVECTOR(pstVector,CV_ERR_INVALID_PARAM); 221 222 if (pstVector->cv_len <= 0) 223 { 224 return CV_ERR_PBACK; 225 } 226 pstVector->cv_len--; 227 return CV_ERR_OK; 228 } 229 230 231 232 CIterator CVector_Begin(const CVector pstVector) 233 { 234 CWARNING_CVECTOR(pstVector,GNULL); 235 return pstVector->cv_pdata; 236 } 237 238 CIterator CVector_End(const CVector pstVector) 239 { 240 CWARNING_CVECTOR(pstVector,GNULL); 241 if(pstVector->cv_pdata == GNULL) 242 { 243 return GNULL; 244 } 245 246 return (Gint8*)pstVector->cv_pdata + (pstVector->cv_size * pstVector->cv_len); 247 } 248 249 CVECTORSTATUS cvmemove_foreward(const CVector pstVector, void *from, void *to) 250 { 251 Gint32 size = 0; 252 void *p = GNULL; 253 CWARNING_CVECTOR(pstVector,CV_ERR_INVALID_PARAM); 254 size = pstVector->cv_size; 255 for (p = to; p >= from; ) 256 { 257 Gmemcpy((Gint8*)p + size, p, size); 258 p = (Gint8*)p-size; 259 } 260 return CV_ERR_OK; 261 } 262 263 CVECTORSTATUS cvmemove_backward(const CVector pstVector, void *from, void *to) 264 { 265 CWARNING_CVECTOR(pstVector,CV_ERR_INVALID_PARAM); 266 Gmemcpy(from, (Gint8*)from + pstVector->cv_size, (Gint8*)to - (Gint8*)from); 267 return CV_ERR_OK; 268 } 269 270 CVECTORSTATUS CVector_Insert_Idx(const CVector pstCVector, Gint32 index, void *memb) 271 { 272 CIterator iter = GNULL; 273 CWARNING_CVECTOR(pstCVector,CV_ERR_INVALID_PARAM); 274 if( index < 0 || memb == GNULL) 275 { 276 return CV_ERR_INVALID_PARAM; 277 } 278 if (index >= pstCVector->cv_tot_len) 279 { 280 pstCVector->cv_len = index + 1; 281 while (pstCVector->cv_len >= pstCVector->cv_tot_len) pstCVector->cv_tot_len <<= EXPANED_VAL; 282 pstCVector->cv_pdata = Grealloc(pstCVector->cv_pdata, pstCVector->cv_tot_len * pstCVector->cv_size); 283 iter = (Gint8*)pstCVector->cv_pdata + pstCVector->cv_size * index; 284 Gmemcpy(iter, memb, pstCVector->cv_size); 285 } 286 else 287 { 288 iter = (Gint8*)pstCVector->cv_pdata + pstCVector->cv_size * index; 289 CVector_Insert(pstCVector, iter, memb); 290 } 291 return 0; 292 } 293 CVECTORSTATUS CVector_Insert(const CVector pstVector, CIterator iter, void *memb) 294 { 295 CWARNING_ITER(pstVector, iter,CV_ERR_INVALID_PARAM); 296 if( memb == GNULL) 297 { 298 return CV_ERR_INVALID_PARAM; 299 } 300 301 if (pstVector->cv_len >= pstVector->cv_tot_len) 302 { 303 void *pd_sav = pstVector->cv_pdata; 304 pstVector->cv_tot_len <<= EXPANED_VAL; 305 pstVector->cv_pdata = Grealloc(pstVector->cv_pdata, pstVector->cv_tot_len * pstVector->cv_size); 306 307 if (!pstVector->cv_pdata) 308 { 309 pstVector->cv_pdata = pd_sav; 310 pstVector->cv_tot_len >>= EXPANED_VAL; 311 return CV_ERR_INSERT; 312 } 313 } 314 315 cvmemove_foreward(pstVector, iter, (Gint8*)pstVector->cv_pdata + pstVector->cv_len * pstVector->cv_size); 316 Gmemcpy(iter, memb, pstVector->cv_size); 317 pstVector->cv_len++; 318 319 return CV_ERR_OK; 320 } 321 322 CIterator CVector_Next(const CVector pstCVector, CIterator iter) 323 { 324 CWARNING_ITER(pstCVector, iter,GNULL); 325 return (Gint8*)iter + pstCVector->cv_size; 326 } 327 328 Gint32 CVector_Val(const CVector pstCVector, CIterator iter, void *memb) 329 { 330 CWARNING_ITER(pstCVector, iter,GNULL); 331 Gmemcpy(memb, iter, pstCVector->cv_size); 332 return 0; 333 } 334 335 Gint32 CVector_At(const CVector pstCVector, CIterator iterator) 336 { 337 Gint32 nRet = -1; 338 CWARNING_ITER(pstCVector, iterator,nRet); 339 340 nRet=(Gint32)((Gint8*)iterator - (Gint8*)CVector_Begin(pstCVector))/pstCVector->cv_size; 341 if(nRet >=0) 342 { 343 return nRet; 344 } 345 else 346 { 347 return -1; 348 } 349 } 350 351 CIterator CVector_At_Idx(const CVector pstCVector, Gint32 index) 352 { 353 CWARNING_CVECTOR(pstCVector,GNULL); 354 if(index < 0) 355 { 356 return GNULL; 357 } 358 if(index < pstCVector->cv_len) 359 { 360 return (Gint8*)pstCVector->cv_pdata + index * pstCVector->cv_size; 361 } 362 else 363 { 364 return GNULL; 365 } 366 } 367 CVECTORSTATUS CVector_Swap(const CVector pstVector, CIterator itera1, CIterator itera2) 368 { 369 CIterator tempIterator = GNULL; 370 CWARNING_ITERS(pstVector, itera1,itera2,CV_ERR_INVALID_PARAM); 371 372 tempIterator = Gmalloc(pstVector->cv_size); 373 if (tempIterator == GNULL) 374 { 375 return (CV_ERR_FAILED); 376 } 377 Gmemcpy(tempIterator,itera1,pstVector->cv_size); 378 Gmemcpy(itera1,itera2,pstVector->cv_size); 379 Gmemcpy(itera2,tempIterator,pstVector->cv_size); 380 Gfree(tempIterator); 381 tempIterator = GNULL; 382 return CV_ERR_OK; 383 } 384 CVECTORSTATUS CVector_Rm(const CVector pstVector, CIterator iter) 385 { 386 CIterator from = GNULL; 387 CIterator end = GNULL; 388 CWARNING_ITER(pstVector, iter,CV_ERR_INVALID_PARAM); 389 from = iter; 390 end = CVector_End(pstVector); 391 if(from == GNULL || from >= end) 392 { 393 return CV_ERR_INVALID_PARAM; 394 } 395 Gmemcpy(from, (Gint8*)from + pstVector->cv_size, (Gint8*)end - (Gint8*)from); 396 pstVector->cv_len--; 397 398 if ((pstVector->cv_tot_len >= (MIN_LEN << REDUSED_VAL)) 399 && (pstVector->cv_len <= (pstVector->cv_tot_len >> REDUSED_VAL))) 400 { 401 void *pd_sav = pstVector->cv_pdata; 402 pstVector->cv_tot_len >>= EXPANED_VAL; 403 pstVector->cv_pdata = realloc(pstVector->cv_pdata, pstVector->cv_tot_len * pstVector->cv_size); 404 405 if (!pstVector->cv_pdata) 406 { 407 pstVector->cv_tot_len <<= EXPANED_VAL; 408 pstVector->cv_pdata = pd_sav; 409 return CVERM; 410 } 411 } 412 413 return CV_ERR_OK; 414 } 415 416 CVECTORSTATUS CVector_Rm_Idx(const CVector pstVector, Gint32 index) 417 { 418 CIterator iter = GNULL; 419 CWARNING_CVECTOR(pstVector,CV_ERR_INVALID_PARAM); 420 if(index <0 ) 421 { 422 return CV_ERR_INVALID_PARAM; 423 } 424 iter = (Gint8*)pstVector->cv_pdata + pstVector->cv_size * index; 425 CWARNING_ITER(pstVector, iter,CV_ERR_INVALID_PARAM); 426 return CVector_Rm(pstVector, iter); 427 } 428 Gbool CVector_Empty(const CVector pstVector) 429 { 430 CWARNING_CVECTOR(pstVector,Gtrue); 431 if(pstVector->cv_len <= 0) 432 { 433 return Gtrue; 434 } 435 else 436 { 437 return Gfalse; 438 } 439 } 440 441 CVECTORSTATUS CVector_Clear(const CVector pstVector) 442 { 443 CWARNING_CVECTOR(pstVector,CV_ERR_INVALID_PARAM); 444 pstVector->cv_len=0; 445 return CV_ERR_OK; 446 } 447 448 void CVector_Printf(const CVector pstVector) 449 { 450 #ifdef GLOG_ON 451 if(pstVector != GNULL) 452 { 453 GLOG_POI_DEBUG(GT("addr=%d,cv_pdata=%d,cv_len=%d,cv_size=%d,cv_tot_len=%d\n"),pstVector,pstVector->cv_pdata,pstVector->cv_len,pstVector->cv_size,pstVector->cv_tot_len); 454 } 455 else 456 { 457 GLOG_POI_DEBUG(GT("CVector is NULL")); 458 } 459 #endif 460 } 461 462 void CVector_Test() 463 { 464 #ifdef GLOG_POI_RELEASE 465 Gint32 i=0; 466 Gint32 x = 0; 467 Gint32 value=10; 468 Gint32 nIndex=22; 469 Gint32 pnOutPos=0; 470 Gint32 stats = 0; 471 Gint32 nPoiCnt=205; 472 Gint32 nSortCut; 473 Gint32 *iterator; 474 CVector pstVector=Gmalloc(sizeof(tagCVector)); 475 Gmemset(pstVector,0,sizeof(tagCVector)); 476 stats = CVector_Create(pstVector,100,sizeof(Gint32)); 477 if(pstVector!= GNULL) 478 { 479 for(i=0;i<200;i++) 480 { 481 value++; 482 CVector_Pushback(pstVector, &value, 1); 483 } 484 value=13; 485 CVector_Pushback(pstVector, &value, 1); 486 value=17; 487 CVector_Pushback(pstVector, &value, 1); 488 value=19; 489 CVector_Pushback(pstVector, &value, 1); 490 value=21; 491 CVector_Pushback(pstVector, &value, 1); 492 value=11; 493 CVector_Pushback(pstVector, &value, 1); 494 // for(x=0;x<CVector_Length(pstVector);x++) 495 // { 496 // iterator= CVector_At_Idx(pstVector,x); 497 // GLOG_POI_DEBUG(GT("CVector x=%d,value=%d\n"),x,(*iterator)); 498 // } 499 GLOG_POI_DEBUG(GT("CVector.Sort() start\n")); 500 501 //nPoiCnt = SEG_SortUp(pstVector->cv_pdata, 0, nPoiCnt - 1, nPoiCnt); 502 nSortCut=CVector_Sort(pstVector,0,0,Gtrue,CVector_Gint32DescCompareFunc); 503 GLOG_POI_DEBUG(GT("CVector.Sort() end nSortCut=%d\n"),nSortCut); 504 CVector_Printf(pstVector); 505 for(i=0;i<CVector_Length(pstVector);i++) 506 { 507 iterator= CVector_At_Idx(pstVector,i); 508 GLOG_POI_DEBUG(GT("CVector i=%d,value=%d\n"),i,(*iterator)); 509 } 510 { 511 Gint32* volati= CVector_IteratorFindByObj(pstVector,CVector_Gint32DescCompareFunc,&nIndex, &pnOutPos); 512 GLOG_POI_DEBUG(GT("CVector FindByObj,index=%d,value=%d\n"),pnOutPos,(*volati)); 513 } 514 } 515 else 516 { 517 GLOG_POI_DEBUG(GT("CVector is NULL")); 518 } 519 #endif 520 } 521 522 //void cv_info(const CVector cv) 523 //{ 524 // printf("/n/ntot :%s : %d/n", __func__, cv->cv_tot_len); 525 // //printf("len :%s : %d/n", __func__, cv->cv_len); 526 // //printf("size:%s : %d/n/n", __func__, cv->cv_size); 527 // return; 528 //} 529 // 530 //void cv_print(const CVector cv) 531 //{ 532 // Gint32 num; 533 // CIterator iter; 534 // 535 // if (CVector_length(cv) == 0) 536 // fprintf(stderr, "file:%s func:%s line:%d error, null length CVector!!/n", __FILE__, __func__, __LINE__); 537 // 538 // for (iter = CVector_begin(cv); 539 // iter != CVector_end(cv); 540 // iter = CVector_next(cv, iter)) 541 // { 542 // //CVector_iter_val(cv, iter, &num); 543 // //printf("var:%d at:%d/n", num, CVector_iter_at(cv, iter)); 544 // } 545 // 546 // return; 547 //}