ab_map.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661
  1. /***************************************************************************
  2. *
  3. * 通用代码
  4. * Map implements
  5. *
  6. * HashMap
  7. * TreeMap
  8. *
  9. * Author : Li Bo Feng
  10. *
  11. * Update History
  12. * DATE OWNER DESCRIPTION
  13. * ----------- ------------ -----------
  14. * 2010-01-19 Li Bo Feng Generated
  15. *
  16. ***************************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stddef.h>
  20. #include <string.h>
  21. #include "ab_map.h"
  22. //#define _DEBUG
  23. #define STRING_BUFFER_SIZE 1024
  24. #ifdef _DEBUG
  25. #define debug(args) printf args ; fflush(stdout);
  26. #endif
  27. #ifndef _DEBUG
  28. #define debug(args) {}
  29. #endif
  30. #ifndef NULL
  31. #define NULL 0
  32. #endif
  33. #define MT_HashMap 'H'
  34. #define MT_TreeMap 'T'
  35. #define MVT_Pointer 'P'
  36. #define MVT_Map 'M'
  37. #define MVT_Bytes 'B'
  38. #define MVT_String 'S'
  39. #define MVT_Long 'L'
  40. #define MVT_Double 'D'
  41. typedef void* METHOD;
  42. typedef struct _map
  43. {
  44. char id[9];
  45. int type;
  46. //
  47. METHOD before_remove_entry;
  48. METHOD after_put_entry;
  49. //
  50. MapEntry items_head;
  51. MapEntry items_tail;
  52. int items_size;
  53. //
  54. char string_buffer[STRING_BUFFER_SIZE];
  55. }*Map;
  56. #define HASH_TABLE_INITIAL_CAPACITY 16
  57. #define HASH_TABLE_MAXIMUM_CAPACITY 1 << 30
  58. #define HASH_TABLE_LOAD_FACTOR 0.75f
  59. typedef struct _hashmap_entry
  60. {
  61. struct _map_entry _;
  62. int hash;
  63. struct _hashmap_entry *hme_next;
  64. }*HashMapEntry;
  65. struct _hashmap;
  66. typedef struct _hashmap_hashing
  67. {
  68. int table_size;
  69. int threshold;
  70. HashMapEntry *table;
  71. }*Hashing;
  72. typedef struct _hashmap
  73. {
  74. struct _map _;
  75. //
  76. Hashing hashing;
  77. }*HashMap;
  78. typedef struct _treemap_entry
  79. {
  80. struct _map_entry _;
  81. //
  82. struct _treemap_entry* parent;
  83. struct _treemap_entry* left;
  84. struct _treemap_entry* right;
  85. int color;
  86. }*TreeMapEntry;
  87. typedef struct _treemap
  88. {
  89. struct _map _;
  90. //
  91. TreeMapEntry items_root;
  92. }*TreeMap;
  93. ///////////////////////////////////////////////////////////////////////////////////////
  94. // predefine
  95. ///////////////////////////////////////////////////////////////////////////////////////
  96. void* hm_get(HashMap map, char *key);
  97. void* tm_get(TreeMap map, char *key);
  98. MapEntry hm_put(HashMap map, char *key, void *value, int copy_value_length, char value_type);
  99. MapEntry tm_put(TreeMap map, char *key, void *value, int copy_value_length, char value_type);
  100. void* hm_remove(HashMap map, char *key);
  101. void* tm_remove(TreeMap map, char *key);
  102. int m_build_string(Map m, char *buf, int bufsize);
  103. ///////////////////////////////////////////////////////////////////////////////////////
  104. // memory alloc info
  105. ///////////////////////////////////////////////////////////////////////////////////////
  106. static int b_mc_flag = -1;
  107. static HashMap mem_map = 0;
  108. static long total_malloc = 0;
  109. int mc_flag()
  110. {
  111. if (b_mc_flag == -1)
  112. {
  113. char* mc = getenv("MEMORY_CHECK");
  114. b_mc_flag = mc ? atoi(mc) : 0;
  115. if (b_mc_flag < 0)
  116. {
  117. b_mc_flag = 0;
  118. }
  119. }
  120. return b_mc_flag;
  121. }
  122. void mc_print_stdout(long tmc, char *info, char* file, int line)
  123. {
  124. printf("malloc %ld, %s (%s,%d)\r\n", tmc, info, file, line);
  125. fflush(stdout);
  126. }
  127. mc_print_function mc_print = mc_print_stdout;
  128. void _mc_print(char* file, int line)
  129. {
  130. m_build_string(((Map) mem_map), ((Map) mem_map)->string_buffer,
  131. sizeof(((Map) mem_map)->string_buffer));
  132. mc_print(total_malloc, ((Map) mem_map)->string_buffer, file, line);
  133. }
  134. void* mc_malloc(int n, char* file, int line)
  135. {
  136. if (mc_flag() == 0)
  137. {
  138. return malloc(n);
  139. }
  140. if (mem_map == 0)
  141. {
  142. mem_map = (HashMap) new_mapping();
  143. }
  144. void* p = malloc(n);
  145. total_malloc += n;
  146. char pk[33];
  147. sprintf(pk, "%lX", (long) p);
  148. long v = n;
  149. hm_put(mem_map, pk, &v, sizeof(long), MVT_Long);
  150. _mc_print(file, line);
  151. return p;
  152. }
  153. void mc_free(void* p, char* file, int line)
  154. {
  155. if (mc_flag() == 0)
  156. {
  157. free(p);
  158. return;
  159. }
  160. long n = -1;
  161. if (p)
  162. {
  163. char pk[33];
  164. sprintf(pk, "%lX", (long) p);
  165. long *pn = (long*) hm_get(mem_map, pk);
  166. if (pn != 0)
  167. {
  168. n = *pn;
  169. hm_remove(mem_map, pk);
  170. }
  171. }
  172. if (n >= 0)
  173. {
  174. total_malloc -= n;
  175. free(p);
  176. }
  177. _mc_print(file, line);
  178. }
  179. ///////////////////////////////////////////////////////////////////////////////////////
  180. // global map define
  181. ///////////////////////////////////////////////////////////////////////////////////////
  182. static int id = 0;
  183. static HashMap global_map = NULL;
  184. ///////////////////////////////////////////////////////////////////////////////////////
  185. // map entry
  186. ///////////////////////////////////////////////////////////////////////////////////////
  187. MapEntry m_new_entry(Map m, MapEntry prev, MapEntry next, size_t mesize, char *key, void *value,
  188. int copy_value_length, char value_type)
  189. {
  190. size_t meksize = strlen(key);
  191. int entry_size = mesize + meksize + 1;
  192. MapEntry e = (MapEntry) malloc(entry_size);
  193. memset(e, 0, entry_size);
  194. e->entry_size = entry_size;
  195. e->key = (char*) ((char*) e + mesize);
  196. strcpy(e->key, key);
  197. e->copy_value_length = copy_value_length;
  198. e->value_type = value_type;
  199. if (copy_value_length >= 0)
  200. {
  201. e->value = malloc(copy_value_length + 1);
  202. if (copy_value_length > 0)
  203. {
  204. memcpy(e->value, value, copy_value_length);
  205. }
  206. memset(e->value + copy_value_length, 0, 1);
  207. }
  208. else
  209. {
  210. e->value = value;
  211. }
  212. e->next = next;
  213. e->prev = prev;
  214. if (prev)
  215. {
  216. prev->next = e;
  217. }
  218. else
  219. {
  220. m->items_head = e;
  221. }
  222. if (next)
  223. {
  224. next->prev = e;
  225. }
  226. else
  227. {
  228. m->items_tail = e;
  229. }
  230. m->items_size++;
  231. m->string_buffer[0] = 0;
  232. debug(("m_new_entry %lX %s\r\n", (long)e, e->key));
  233. return e;
  234. }
  235. void m_replace_entry_value(MapEntry entry, void* value, int copy_value_length, char value_type)
  236. {
  237. if (copy_value_length > 0 && entry->copy_value_length == copy_value_length)
  238. {
  239. memcpy(entry->value, value, entry->copy_value_length);
  240. }
  241. else
  242. {
  243. if (entry->copy_value_length > 0)
  244. {
  245. free(entry->value);
  246. }
  247. entry->copy_value_length = copy_value_length;
  248. if (copy_value_length > 0)
  249. {
  250. entry->value = malloc(copy_value_length + 1);
  251. memcpy(entry->value, value, copy_value_length);
  252. memset(entry->value + copy_value_length, 0, 1);
  253. }
  254. else
  255. {
  256. entry->value = value;
  257. }
  258. }
  259. entry->value_type = value_type;
  260. }
  261. void m_delete_entry(Map m, MapEntry e)
  262. {
  263. debug(("hm_delete_entry %lX %s\r\n", (long)e, e->key));
  264. if (m)
  265. {
  266. if (m->before_remove_entry != NULL)
  267. {
  268. ((event_map_entry) m->before_remove_entry)((long) m, e->key, e->value);
  269. }
  270. if (e->next)
  271. {
  272. e->next->prev = e->prev;
  273. }
  274. else
  275. {
  276. m->items_tail = e->prev;
  277. }
  278. if (e->prev)
  279. {
  280. e->prev->next = e->next;
  281. }
  282. else
  283. {
  284. m->items_head = e->next;
  285. }
  286. m->items_size--;
  287. m->string_buffer[0] = 0;
  288. }
  289. else
  290. {
  291. // it's lonely entry, hashing has been seperate from map.
  292. }
  293. if (e->copy_value_length > 0)
  294. {
  295. free(e->value);
  296. }
  297. free(e);
  298. }
  299. ///////////////////////////////////////////////////////////////////////////////////////
  300. // new/delete map
  301. ///////////////////////////////////////////////////////////////////////////////////////
  302. Map m_new_map(size_t map_struct_size, int id, event_map_entry before_remove_entry,
  303. event_map_entry after_put_entry)
  304. {
  305. debug(("m_new_map entry\r\n"));
  306. Map m = (Map) malloc(map_struct_size);
  307. memset(m, 0, map_struct_size);
  308. snprintf(m->id, 8, "%X", id);
  309. m->before_remove_entry = (METHOD) before_remove_entry;
  310. m->after_put_entry = (METHOD) after_put_entry;
  311. return m;
  312. }
  313. void m_delete_map(Map m)
  314. {
  315. free(m);
  316. }
  317. ///////////////////////////////////////////////////////////////////////////////////////
  318. // map to string
  319. ///////////////////////////////////////////////////////////////////////////////////////
  320. int m_build_string(Map m, char *buf, int bufsize);
  321. int m_me_value_string(MapEntry me, char *buf, int bufsize)
  322. {
  323. if (me->value_type == MVT_Map)
  324. {
  325. Map submap;
  326. if (me->copy_value_length >= 0)
  327. {
  328. long pm = *(long*) me->value;
  329. submap = (Map) pm;
  330. }
  331. else
  332. {
  333. submap = (Map) me->value;
  334. }
  335. int rc = m_build_string(submap, buf, bufsize - 2);
  336. strcat(buf, ", ");
  337. return rc + 2;
  338. }
  339. else
  340. {
  341. char sbv[STRING_BUFFER_SIZE];
  342. int sbvsize = sizeof(sbv);
  343. switch (me->value_type)
  344. {
  345. case MVT_String:
  346. snprintf(sbv, sbvsize, "%s", (char*) me->value);
  347. break;
  348. case MVT_Long:
  349. snprintf(sbv, sbvsize, "%ld", *(long*) me->value);
  350. break;
  351. case MVT_Double:
  352. snprintf(sbv, sbvsize, "%lf", *(double*) me->value);
  353. break;
  354. default:
  355. snprintf(sbv, sbvsize, "H%lX", (long) me->value);
  356. break;
  357. }
  358. if (strlen(sbv) < bufsize - 2)
  359. {
  360. strcpy(buf, sbv);
  361. strcat(buf, ", ");
  362. return strlen(buf);
  363. }
  364. else
  365. {
  366. strncpy(buf, sbv, bufsize - 5);
  367. strcat(buf + bufsize - 5, "..., ");
  368. return bufsize;
  369. }
  370. }
  371. return 0;
  372. }
  373. int m_me_string(MapEntry me, char *buf, int bufsize)
  374. {
  375. if (strlen(me->key) + 5 < bufsize)
  376. {
  377. sprintf(buf, "%s:", me->key);
  378. int nk = strlen(buf);
  379. buf += nk;
  380. bufsize -= nk;
  381. int nv = m_me_value_string(me, buf, bufsize);
  382. if (nv < 0)
  383. {
  384. return nv;
  385. }
  386. return nk + nv;
  387. }
  388. else
  389. {
  390. return -1;
  391. }
  392. }
  393. int m_map_string(Map m, char *buf, int bufsize)
  394. {
  395. int bi = 0;
  396. int rc = 0;
  397. MapEntry e;
  398. e = m->items_head;
  399. while (e)
  400. {
  401. rc = m_me_string(e, buf + bi, bufsize - bi);
  402. if (rc < 0)
  403. {
  404. return rc;
  405. }
  406. bi += rc;
  407. e = e->next;
  408. }
  409. return bi;
  410. }
  411. int m_build_string(Map m, char *buf, int bufsize)
  412. {
  413. memset(buf, 0, bufsize);
  414. strcat(buf, "{");
  415. int rc = m_map_string(m, buf + 1, bufsize - 5);
  416. if (rc < 0)
  417. {
  418. strcat(buf, "...");
  419. }
  420. else if (rc > 1)
  421. {
  422. // cut off the tail ", "
  423. buf[rc - 1] = 0;
  424. buf[rc] = 0;
  425. }
  426. strcat(buf, "}");
  427. return strlen(buf);
  428. }
  429. ///////////////////////////////////////////////////////////////////////////////////////
  430. // tree map new/delete/get/put/remove
  431. ///////////////////////////////////////////////////////////////////////////////////////
  432. TreeMap tm_new_map(int id, event_map_entry before_remove_entry, event_map_entry after_put_entry)
  433. {
  434. TreeMap m = (TreeMap) m_new_map(sizeof(struct _treemap), id, before_remove_entry,
  435. after_put_entry);
  436. ((Map) m)->type = MT_TreeMap;
  437. m->items_root = NULL;
  438. return m;
  439. }
  440. void tm_delete_map(TreeMap m)
  441. {
  442. debug(("tm_delete_map entry %lX\r\n", (long)m));
  443. MapEntry me = ((Map) m)->items_head;
  444. while (me)
  445. {
  446. MapEntry nme = me->next;
  447. // delete directly, do without rebalance.
  448. m_delete_entry(((Map) m), me);
  449. me = nme;
  450. }
  451. m_delete_map((Map) m);
  452. }
  453. TreeMapEntry tm_new_entry(TreeMap m, MapEntry prev, MapEntry next, char *key, void *value,
  454. int copy_value_length, char value_type)
  455. {
  456. TreeMapEntry hme = (TreeMapEntry) m_new_entry((Map) m, (MapEntry) prev, (MapEntry) next,
  457. sizeof(struct _treemap_entry), key, value, copy_value_length, value_type);
  458. return hme;
  459. }
  460. void tm_right_rotate(TreeMap tm, TreeMapEntry x)
  461. {
  462. TreeMapEntry y = x->left;
  463. x->left = y->right;
  464. if (y->right != NULL)
  465. {
  466. y->right->parent = x;
  467. }
  468. y->parent = x->parent;
  469. if (x->parent == NULL)
  470. {
  471. tm->items_root = y;
  472. }
  473. else
  474. {
  475. if (x == x->parent->right)
  476. {
  477. x->parent->right = y;
  478. }
  479. else
  480. {
  481. x->parent->left = y;
  482. }
  483. }
  484. y->right = x;
  485. x->parent = y;
  486. }
  487. void tm_left_rotate(TreeMap tm, TreeMapEntry x)
  488. {
  489. TreeMapEntry y = x->right;
  490. x->right = y->left;
  491. if (y->left != NULL)
  492. {
  493. y->left->parent = x;
  494. }
  495. y->parent = x->parent;
  496. if (x->parent == NULL)
  497. {
  498. tm->items_root = y;
  499. }
  500. else
  501. {
  502. if (x == x->parent->left)
  503. {
  504. x->parent->left = y;
  505. }
  506. else
  507. {
  508. x->parent->right = y;
  509. }
  510. }
  511. y->left = x;
  512. x->parent = y;
  513. }
  514. void tm_balance(TreeMap tm, TreeMapEntry x)
  515. {
  516. TreeMapEntry y;
  517. x->color = 1;
  518. while (x != tm->items_root && x->parent->color)
  519. {
  520. if (x->parent == x->parent->parent->left)
  521. {
  522. y = x->parent->parent->right;
  523. if (y != NULL && y->color)
  524. {
  525. x->parent->color = 0;
  526. y->color = 0;
  527. x->parent->parent->color = 1;
  528. x = x->parent->parent;
  529. }
  530. else
  531. {
  532. if (x == x->parent->right)
  533. {
  534. x = x->parent;
  535. tm_left_rotate(tm, x);
  536. }
  537. x->parent->color = 0;
  538. x->parent->parent->color = 1;
  539. tm_right_rotate(tm, x->parent->parent);
  540. }
  541. }
  542. else
  543. {
  544. y = x->parent->parent->left;
  545. if (y != NULL && y->color)
  546. {
  547. x->parent->color = 0;
  548. y->color = 0;
  549. x->parent->parent->color = 1;
  550. x = x->parent->parent;
  551. }
  552. else
  553. {
  554. if (x == x->parent->left)
  555. {
  556. x = x->parent;
  557. tm_right_rotate(tm, x);
  558. }
  559. x->parent->color = 0;
  560. x->parent->parent->color = 1;
  561. tm_left_rotate(tm, x->parent->parent);
  562. }
  563. }
  564. }
  565. tm->items_root->color = 0;
  566. }
  567. void tm_fixup(TreeMap tm, TreeMapEntry x)
  568. {
  569. TreeMapEntry w;
  570. while (x != tm->items_root && !x->color)
  571. {
  572. if (x == x->parent->left)
  573. {
  574. w = x->parent->right;
  575. if (w == NULL)
  576. {
  577. x = x->parent;
  578. continue;
  579. }
  580. if (w->color)
  581. {
  582. w->color = 0;
  583. x->parent->color = 1;
  584. tm_left_rotate(tm, x->parent);
  585. w = x->parent->right;
  586. if (w == NULL)
  587. {
  588. x = x->parent;
  589. continue;
  590. }
  591. }
  592. if ((w->left == NULL || !w->left->color) && (w->right == NULL || !w->right->color))
  593. {
  594. w->color = 1;
  595. x = x->parent;
  596. }
  597. else
  598. {
  599. if (w->right == NULL || !w->right->color)
  600. {
  601. w->left->color = 0;
  602. w->color = 1;
  603. tm_right_rotate(tm, w);
  604. w = x->parent->right;
  605. }
  606. w->color = x->parent->color;
  607. x->parent->color = 0;
  608. w->right->color = 0;
  609. tm_left_rotate(tm, x->parent);
  610. x = tm->items_root;
  611. }
  612. }
  613. else
  614. {
  615. w = x->parent->left;
  616. if (w == NULL)
  617. {
  618. x = x->parent;
  619. continue;
  620. }
  621. if (w->color)
  622. {
  623. w->color = 0;
  624. x->parent->color = 1;
  625. tm_right_rotate(tm, x->parent);
  626. w = x->parent->left;
  627. if (w == NULL)
  628. {
  629. x = x->parent;
  630. continue;
  631. }
  632. }
  633. if ((w->left == NULL || !w->left->color) && (w->right == NULL || !w->right->color))
  634. {
  635. w->color = 1;
  636. x = x->parent;
  637. }
  638. else
  639. {
  640. if (w->left == NULL || !w->left->color)
  641. {
  642. w->right->color = 0;
  643. w->color = 1;
  644. tm_left_rotate(tm, w);
  645. w = x->parent->left;
  646. }
  647. w->color = x->parent->color;
  648. x->parent->color = 0;
  649. w->left->color = 0;
  650. tm_right_rotate(tm, x->parent);
  651. x = tm->items_root;
  652. }
  653. }
  654. }
  655. x->color = 0;
  656. }
  657. void tm_attach_to_parent_no_fixup(TreeMap tm, TreeMapEntry toDelete, TreeMapEntry toConnect)
  658. {
  659. // assert toConnect!=NULL
  660. TreeMapEntry parent = toDelete->parent;
  661. toConnect->parent = parent;
  662. if (parent == NULL)
  663. {
  664. tm->items_root = toConnect;
  665. }
  666. else if (toDelete == parent->left)
  667. {
  668. parent->left = toConnect;
  669. }
  670. else
  671. {
  672. parent->right = toConnect;
  673. }
  674. }
  675. void tm_attach_to_parent(TreeMap tm, TreeMapEntry toDelete, TreeMapEntry toConnect)
  676. {
  677. // assert toConnect!=NULL
  678. tm_attach_to_parent_no_fixup(tm, toDelete, toConnect);
  679. if (!toDelete->color)
  680. {
  681. tm_fixup(tm, toConnect);
  682. }
  683. }
  684. void tm_attach_null_to_parent(TreeMap tm, TreeMapEntry toDelete)
  685. {
  686. TreeMapEntry parent = toDelete->parent;
  687. if (parent == NULL)
  688. {
  689. tm->items_root = NULL;
  690. }
  691. else
  692. {
  693. if (toDelete == parent->left)
  694. {
  695. parent->left = NULL;
  696. }
  697. else
  698. {
  699. parent->right = NULL;
  700. }
  701. if (!toDelete->color)
  702. {
  703. tm_fixup(tm, parent);
  704. }
  705. }
  706. }
  707. void tm_delete_entry(TreeMap tm, TreeMapEntry tme)
  708. {
  709. if (tme->right == NULL)
  710. {
  711. if (tme->left != NULL)
  712. {
  713. tm_attach_to_parent(tm, tme, tme->left);
  714. }
  715. else
  716. {
  717. tm_attach_null_to_parent(tm, tme);
  718. }
  719. }
  720. else if (tme->left == NULL)
  721. {
  722. // tme->right != NULL
  723. tm_attach_to_parent(tm, tme, tme->right);
  724. }
  725. else
  726. {
  727. // Here tme->left!=nul && tme->right!=NULL
  728. // tme->next should replace tme in tree
  729. // tme->next!=NULL by tree logic.
  730. // tme->next->left==NULL by tree logic.
  731. // tme->next->right may be NULL or non-NULL
  732. TreeMapEntry toMoveUp = (TreeMapEntry) ((MapEntry) tme)->next;
  733. if (toMoveUp->right == NULL)
  734. {
  735. tm_attach_null_to_parent(tm, toMoveUp);
  736. }
  737. else
  738. {
  739. tm_attach_to_parent(tm, toMoveUp, toMoveUp->right);
  740. }
  741. // Here toMoveUp is ready to replace tme
  742. toMoveUp->left = tme->left;
  743. if (tme->left != NULL)
  744. {
  745. tme->left->parent = toMoveUp;
  746. }
  747. toMoveUp->right = tme->right;
  748. if (tme->right != NULL)
  749. {
  750. tme->right->parent = toMoveUp;
  751. }
  752. tm_attach_to_parent_no_fixup(tm, tme, toMoveUp);
  753. toMoveUp->color = tme->color;
  754. }
  755. m_delete_entry((Map) tm, (MapEntry) tme);
  756. }
  757. TreeMapEntry tm_put_entry(TreeMap tm, char *key, void *value, int copy_value_length,
  758. char value_type)
  759. {
  760. int cmp;
  761. TreeMapEntry ntme;
  762. TreeMapEntry ptme = NULL;
  763. TreeMapEntry tme = tm->items_root;
  764. if (tme == NULL)
  765. {
  766. ntme = (TreeMapEntry) tm_new_entry(tm, NULL, NULL, key, value, copy_value_length,
  767. value_type);
  768. tm->items_root = ntme;
  769. return ntme;
  770. }
  771. while (tme != NULL)
  772. {
  773. cmp = strcmp(key, ((MapEntry) tme)->key);
  774. if (cmp == 0)
  775. {
  776. return tme;
  777. }
  778. if (cmp < 0)
  779. {
  780. ptme = tme;
  781. tme = ptme->left;
  782. if (tme == NULL)
  783. {
  784. ntme = (TreeMapEntry) tm_new_entry(tm, ((MapEntry) ptme)->prev, ((MapEntry) ptme),
  785. key, value, copy_value_length, value_type);
  786. ptme->left = ntme;
  787. ntme->parent = ptme;
  788. }
  789. }
  790. else
  791. {
  792. ptme = tme;
  793. tme = tme->right;
  794. if (tme == NULL)
  795. {
  796. ntme = (TreeMapEntry) tm_new_entry(tm, ((MapEntry) ptme), ((MapEntry) ptme)->next,
  797. key, value, copy_value_length, value_type);
  798. ptme->right = ntme;
  799. ntme->parent = ptme;
  800. }
  801. }
  802. }
  803. tm_balance(tm, ntme);
  804. return ntme;
  805. }
  806. MapEntry tm_put(TreeMap tm, char *key, void *value, int copy_value_length, char value_type)
  807. {
  808. #ifdef _DEBUG
  809. {
  810. m_build_string((Map) tm, ((Map) tm)->string_buffer, sizeof(((Map) tm)->string_buffer));
  811. debug(("tm_put %s into %s\r\n", key, ((Map)tm)->string_buffer));
  812. }
  813. #endif
  814. int n = ((Map) tm)->items_size;
  815. TreeMapEntry tme = (TreeMapEntry) tm_put_entry(tm, key, value, copy_value_length, value_type);
  816. if (n == ((Map) tm)->items_size)
  817. {
  818. if (((Map) tm)->before_remove_entry)
  819. {
  820. ((event_map_entry) ((Map) tm)->before_remove_entry)(((long) tm), ((MapEntry) tme)->key,
  821. ((MapEntry) tme)->value);
  822. }
  823. m_replace_entry_value((MapEntry) tme, value, copy_value_length, value_type);
  824. if (((Map) tm)->after_put_entry)
  825. {
  826. ((event_map_entry) ((Map) tm)->after_put_entry)(((long) tm), ((MapEntry) tme)->key,
  827. ((MapEntry) tme)->value);
  828. }
  829. debug(("tm_put returned exsiting entry %lX\r\n", (long)tme));
  830. }
  831. else
  832. {
  833. if (((Map) tm)->after_put_entry)
  834. {
  835. ((event_map_entry) ((Map) tm)->after_put_entry)(((long) tm), ((MapEntry) tme)->key,
  836. ((MapEntry) tme)->value);
  837. }
  838. debug(("tm_put returned new entry %lX\r\n", (long)tme));
  839. }
  840. return ((MapEntry) tme);
  841. }
  842. TreeMapEntry tm_get_entry(TreeMap tm, char *key)
  843. {
  844. int cmp;
  845. TreeMapEntry tme = tm->items_root;
  846. while (tme != NULL)
  847. {
  848. cmp = strcmp(key, ((MapEntry) tme)->key);
  849. if (cmp == 0)
  850. {
  851. return tme;
  852. }
  853. if (cmp < 0)
  854. {
  855. tme = tme->left;
  856. }
  857. else
  858. {
  859. tme = tme->right;
  860. }
  861. }
  862. return NULL;
  863. }
  864. TreeMapEntry tm_get_nearby_entry(TreeMap tm, char *key)
  865. {
  866. int cmp;
  867. TreeMapEntry tme = tm->items_root;
  868. TreeMapEntry rtme = tme;
  869. while (tme != NULL)
  870. {
  871. cmp = strcmp(key, ((MapEntry) tme)->key);
  872. if (cmp == 0)
  873. {
  874. return tme;
  875. }
  876. if (cmp < 0)
  877. {
  878. rtme = (TreeMapEntry) ((MapEntry) tme)->prev;
  879. tme = tme->left;
  880. }
  881. else
  882. {
  883. rtme = tme;
  884. tme = tme->right;
  885. }
  886. }
  887. return rtme;
  888. }
  889. void* tm_get(TreeMap map, char *key)
  890. {
  891. TreeMapEntry tme = tm_get_entry(map, key);
  892. void*value = NULL;
  893. if (tme)
  894. {
  895. value = ((MapEntry) tme)->value;
  896. }
  897. return value;
  898. }
  899. void* tm_remove(TreeMap map, char *key)
  900. {
  901. TreeMapEntry tme = tm_get_entry(map, key);
  902. void*value = NULL;
  903. if (tme)
  904. {
  905. if (((MapEntry) tme)->copy_value_length < 0)
  906. {
  907. value = ((MapEntry) tme)->value;
  908. }
  909. tm_delete_entry(map, tme);
  910. }
  911. return value;
  912. }
  913. void tm_put_all_map(TreeMap dest, Map src)
  914. {
  915. MapEntry me = src->items_head;
  916. while (me)
  917. {
  918. tm_put(dest, me->key, me->value, me->copy_value_length, me->value_type);
  919. me = me->next;
  920. }
  921. }
  922. ///////////////////////////////////////////////////////////////////////////////////////
  923. // hash calculate method define
  924. ///////////////////////////////////////////////////////////////////////////////////////
  925. int hash(char* key)
  926. {
  927. char c;
  928. int h = 0;
  929. while ((c = *key++) != 0)
  930. {
  931. h = 31 * h + (unsigned int) c;
  932. }
  933. return h ^ (h << 20) ^ (h << 12) ^ (h << 7) ^ (h << 4);
  934. }
  935. ///////////////////////////////////////////////////////////////////////////////////////
  936. // hash map hashing
  937. ///////////////////////////////////////////////////////////////////////////////////////
  938. Hashing hm_new_hashing(int table_size)
  939. {
  940. int hashing_bytes_size = sizeof(struct _hashmap_hashing)
  941. + (sizeof(HashMapEntry *)) * table_size;
  942. Hashing m = (Hashing) malloc(hashing_bytes_size);
  943. memset(m, 0, hashing_bytes_size);
  944. m->table = (HashMapEntry*) (((char*) m) + sizeof(struct _hashmap_hashing));
  945. m->table_size = table_size;
  946. m->threshold = table_size * HASH_TABLE_LOAD_FACTOR;
  947. return m;
  948. }
  949. void hm_delete_hashing(Hashing hashing)
  950. {
  951. free(hashing);
  952. }
  953. ///////////////////////////////////////////////////////////////////////////////////////
  954. // hash map new/delete/get/put/remove
  955. ///////////////////////////////////////////////////////////////////////////////////////
  956. HashMap hm_new_map(int id, event_map_entry before_remove_entry, event_map_entry after_put_entry)
  957. {
  958. HashMap m = (HashMap) m_new_map(sizeof(struct _hashmap), id, before_remove_entry,
  959. after_put_entry);
  960. ((Map) m)->type = MT_HashMap;
  961. m->hashing = hm_new_hashing(HASH_TABLE_INITIAL_CAPACITY);
  962. return m;
  963. }
  964. void hm_increase_capacity(HashMap m)
  965. {
  966. Hashing old_hashing = m->hashing;
  967. int new_size = old_hashing->table_size << 1;
  968. if (new_size >= HASH_TABLE_MAXIMUM_CAPACITY)
  969. {
  970. new_size = HASH_TABLE_MAXIMUM_CAPACITY;
  971. old_hashing->threshold = (int) (((unsigned int) (1) << 31) - 1); // MAX_INTEGER
  972. return;
  973. }
  974. debug(("hm_increase_capacity %d\r\n", new_size));
  975. m->hashing = hm_new_hashing(new_size);
  976. // transfer entries
  977. HashMapEntry *old_table = old_hashing->table;
  978. HashMapEntry *new_table = m->hashing->table;
  979. int j;
  980. for (j = 0; j < old_hashing->table_size; j++)
  981. {
  982. HashMapEntry hme = old_table[j];
  983. while (hme)
  984. {
  985. HashMapEntry next = hme->hme_next;
  986. int i = hme->hash & (new_size - 1);
  987. hme->hme_next = new_table[i];
  988. new_table[i] = hme;
  989. hme = next;
  990. }
  991. }
  992. hm_delete_hashing(old_hashing);
  993. }
  994. void hm_delete_map(HashMap m)
  995. {
  996. debug(("hm_delete_map entry %lX\r\n", (long)m));
  997. HashMapEntry *table = m->hashing->table;
  998. int table_size = m->hashing->table_size;
  999. int j;
  1000. for (j = 0; j < table_size; j++)
  1001. {
  1002. HashMapEntry hme = table[j];
  1003. while (hme)
  1004. {
  1005. HashMapEntry next = hme->hme_next;
  1006. m_delete_entry((Map) m, (MapEntry) hme);
  1007. hme = next;
  1008. }
  1009. }
  1010. hm_delete_hashing(m->hashing);
  1011. m_delete_map((Map) m);
  1012. }
  1013. HashMapEntry hm_new_entry(HashMap map, int h, int table_index, char *key, void *value,
  1014. int copy_value_length, char value_type)
  1015. {
  1016. HashMapEntry hme = (HashMapEntry) m_new_entry(((Map) map), ((Map) map)->items_tail, NULL,
  1017. sizeof(struct _hashmap_entry), key, value, copy_value_length, value_type);
  1018. HashMapEntry next = map->hashing->table[table_index];
  1019. hme->hash = h;
  1020. hme->hme_next = next;
  1021. map->hashing->table[table_index] = hme;
  1022. if (((Map) map)->items_size >= map->hashing->threshold)
  1023. {
  1024. hm_increase_capacity(map);
  1025. }
  1026. debug(("hm new entry key %s, %016X, %d\r\n", key, h, table_index));
  1027. return hme;
  1028. }
  1029. HashMapEntry hm_get_entry(Hashing hashing, int h, int table_index, char*key)
  1030. {
  1031. debug(("find key %s, %016X, %d\r\n", key, h, table_index));
  1032. HashMapEntry hme = hashing->table[table_index];
  1033. #ifdef _DEBUG
  1034. int i = 0;
  1035. #endif
  1036. for (; hme; hme = hme->hme_next)
  1037. {
  1038. debug(("find key %s\r\n", key));
  1039. if (hme->hash == h && strcmp(((MapEntry) hme)->key, key) == 0)
  1040. {
  1041. debug( ("found key %s, hash %016X, index %d, no %d\r\n", key, h, table_index, i));
  1042. return hme;
  1043. }
  1044. #ifdef _DEBUG
  1045. i++;
  1046. #endif
  1047. }
  1048. return NULL;
  1049. }
  1050. MapEntry hm_put(HashMap map, char *key, void *value, int copy_value_length, char value_type)
  1051. {
  1052. #ifdef _DEBUG
  1053. {
  1054. m_build_string((Map) map, ((Map) map)->string_buffer, sizeof(((Map) map)->string_buffer));
  1055. debug(("hm_put %s into %s\r\n", key, ((Map)map)->string_buffer));
  1056. }
  1057. #endif
  1058. int h = hash(key);
  1059. int table_index = h & (map->hashing->table_size - 1);
  1060. debug(("hash %s ==> %016X, %d\r\n", key, h, table_index));
  1061. HashMapEntry entry = hm_get_entry(map->hashing, h, table_index, key);
  1062. if (entry)
  1063. {
  1064. if (((Map) map)->before_remove_entry)
  1065. {
  1066. ((event_map_entry) ((Map) map)->before_remove_entry)(((long) map),
  1067. ((MapEntry) entry)->key, ((MapEntry) entry)->value);
  1068. }
  1069. m_replace_entry_value((MapEntry) entry, value, copy_value_length, value_type);
  1070. if (((Map) map)->after_put_entry)
  1071. {
  1072. ((event_map_entry) ((Map) map)->after_put_entry)(((long) map), ((MapEntry) entry)->key,
  1073. ((MapEntry) entry)->value);
  1074. }
  1075. debug(("hm_put returned exsiting entry %lX\r\n", (long)entry));
  1076. return (MapEntry) entry;
  1077. }
  1078. entry = hm_new_entry(map, h, table_index, key, value, copy_value_length, value_type);
  1079. if (((Map) map)->after_put_entry)
  1080. {
  1081. ((event_map_entry) ((Map) map)->after_put_entry)(((long) map), ((MapEntry) entry)->key,
  1082. ((MapEntry) entry)->value);
  1083. }
  1084. debug(("hm_put returned new entry %lX\r\n", (long)entry));
  1085. return (MapEntry) entry;
  1086. }
  1087. void* hm_get(HashMap map, char *key)
  1088. {
  1089. #ifdef _DEBUG
  1090. {
  1091. debug(("hm_get %s from %lX\r\n", key, (long)map));
  1092. m_build_string(((Map) map), ((Map) map)->string_buffer, sizeof(((Map) map)->string_buffer));
  1093. debug(("hm_get %s from %s\r\n", key, ((Map)map)->string_buffer));
  1094. }
  1095. #endif
  1096. int h = hash(key);
  1097. Hashing hashing = map->hashing;
  1098. int table_index = h & (hashing->table_size - 1);
  1099. debug(("hash %s ==> %016X, %d\r\n", key, h, table_index));
  1100. HashMapEntry entry = hm_get_entry(hashing, h, table_index, key);
  1101. if (entry)
  1102. {
  1103. return ((MapEntry) entry)->value;
  1104. }
  1105. return NULL;
  1106. }
  1107. void* hm_remove(HashMap map, char *key)
  1108. {
  1109. #ifdef _DEBUG
  1110. {
  1111. m_build_string(((Map) map), ((Map) map)->string_buffer, sizeof(((Map) map)->string_buffer));
  1112. debug(("hm_remove %s from %s\r\n", key, ((Map)map)->string_buffer));
  1113. }
  1114. #endif
  1115. int h = hash(key);
  1116. int table_index = h & (map->hashing->table_size - 1);
  1117. debug(("hash %s ==> %016X, %d\r\n", key, h, table_index));
  1118. HashMapEntry prev = NULL;
  1119. HashMapEntry hme = map->hashing->table[table_index];
  1120. while (hme)
  1121. {
  1122. if (hme->hash == h && strcmp(((MapEntry) hme)->key, key) == 0)
  1123. {
  1124. void* value =
  1125. (((MapEntry) hme)->copy_value_length >= 0) ? NULL : ((MapEntry) hme)->value;
  1126. if (prev)
  1127. {
  1128. prev->hme_next = hme->hme_next;
  1129. }
  1130. else
  1131. {
  1132. map->hashing->table[table_index] = hme->hme_next;
  1133. }
  1134. m_delete_entry((Map) map, (MapEntry) hme);
  1135. return value;
  1136. }
  1137. prev = hme;
  1138. hme = hme->hme_next;
  1139. }
  1140. return NULL;
  1141. }
  1142. void hm_put_all_map(HashMap dest, Map src)
  1143. {
  1144. MapEntry me = src->items_head;
  1145. while (me)
  1146. {
  1147. hm_put(dest, me->key, me->value, me->copy_value_length, me->value_type);
  1148. me = me->next;
  1149. }
  1150. }
  1151. ///////////////////////////////////////////////////////////////////////////////////////
  1152. // map event define
  1153. ///////////////////////////////////////////////////////////////////////////////////////
  1154. int on_before_delete_mapping(long mapid, char*key, void*value)
  1155. {
  1156. debug(("on_before_delete_mapping %s\r\n", key));
  1157. if (mapid == ((long) global_map))
  1158. {
  1159. Map map = (Map) value;
  1160. if (map->type == MT_HashMap)
  1161. {
  1162. hm_delete_map((HashMap) map);
  1163. }
  1164. else if (map->type == MT_TreeMap)
  1165. {
  1166. tm_delete_map((TreeMap) map);
  1167. }
  1168. else
  1169. {
  1170. }
  1171. }
  1172. return 0;
  1173. }
  1174. int on_after_new_mapping(long mapid, char*key, void*value)
  1175. {
  1176. debug(("on_after_new_mapping %s\r\n", key));
  1177. return 0;
  1178. }
  1179. int on_before_remove_entry(long mapid, char*key, void*value)
  1180. {
  1181. debug(("on_before_remove_entry %s\r\n", key));
  1182. return 0;
  1183. }
  1184. int on_after_put_entry(long mapid, char*key, void*value)
  1185. {
  1186. debug(("on_after_put_entry %s\r\n", key));
  1187. return 0;
  1188. }
  1189. ///////////////////////////////////////////////////////////////////////////////////////
  1190. /**
  1191. * init mapping, it is auto called while new_mapping
  1192. */
  1193. void init_mapping()
  1194. {
  1195. if (global_map == NULL)
  1196. {
  1197. id = 0;
  1198. global_map = hm_new_map(id++, on_before_delete_mapping, on_after_new_mapping);
  1199. }
  1200. }
  1201. long new_mapping()
  1202. {
  1203. if (global_map == NULL)
  1204. {
  1205. init_mapping();
  1206. }
  1207. debug(("new_mapping entry\r\n"));
  1208. HashMap m = hm_new_map(id++, on_before_remove_entry, on_after_put_entry);
  1209. hm_put(global_map, ((Map) m)->id, (Map) m, -1, MVT_Map);
  1210. #ifdef _DEBUG
  1211. m_build_string(((Map) global_map), ((Map) global_map)->string_buffer,
  1212. sizeof(((Map) global_map)->string_buffer));
  1213. debug(("global_map map %s\r\n", ((Map)global_map)->string_buffer));
  1214. #endif
  1215. debug(("new_mapping exit %lX\r\n", (long) m));
  1216. return (long) m;
  1217. }
  1218. long new_sorted_mapping()
  1219. {
  1220. if (global_map == NULL)
  1221. {
  1222. init_mapping();
  1223. }
  1224. debug(("new_sorted_mapping entry\r\n"));
  1225. TreeMap m = tm_new_map(id++, on_before_remove_entry, on_after_put_entry);
  1226. hm_put(global_map, ((Map) m)->id, (Map) m, -1, MVT_Map);
  1227. #ifdef _DEBUG
  1228. m_build_string(((Map) global_map), ((Map) global_map)->string_buffer,
  1229. sizeof(((Map) global_map)->string_buffer));
  1230. debug(("global_map map %s\r\n", ((Map)global_map)->string_buffer));
  1231. #endif
  1232. debug(("new_sorted_mapping exit\r\n"));
  1233. return (long) m;
  1234. }
  1235. int is_map(Map m)
  1236. {
  1237. debug(("is_map %lX\r\n", (long)m));
  1238. debug(("m->id %s\r\n", m->id));
  1239. debug(("global_map v %lX\r\n", (long)(hm_get(global_map, m->id))));
  1240. return (hm_get(global_map, m->id) == m);
  1241. }
  1242. void _put_mapping(long mapid, char* key, void* value, int copy_value_length, char value_type)
  1243. {
  1244. if (global_map == NULL)
  1245. {
  1246. return;
  1247. }
  1248. if (key == NULL)
  1249. {
  1250. return;
  1251. }
  1252. if (value == NULL)
  1253. {
  1254. copy_value_length = -1;
  1255. }
  1256. debug(("put_mapping entry\r\n"));
  1257. Map map = (Map) mapid;
  1258. debug(("put_mapping m=%lX\r\n", (long)map));
  1259. if (is_map(map))
  1260. {
  1261. if (map->type == MT_HashMap)
  1262. {
  1263. hm_put((HashMap) map, key, value, copy_value_length, value_type);
  1264. }
  1265. else
  1266. {
  1267. tm_put((TreeMap) map, key, value, copy_value_length, value_type);
  1268. }
  1269. }
  1270. debug(("put_mapping exit\r\n"));
  1271. }
  1272. void put_mapping(long mapid, char* key, void* value, int copy_value_length)
  1273. {
  1274. _put_mapping(mapid, key, value, copy_value_length,
  1275. copy_value_length < 0 ? MVT_Pointer : MVT_Bytes);
  1276. }
  1277. void put_string_mapping(long mapid, char* key, char* value)
  1278. {
  1279. _put_mapping(mapid, key, value, strlen(value), MVT_String);
  1280. }
  1281. void put_submap_mapping(long mapid, char* key, long value)
  1282. {
  1283. _put_mapping(mapid, key, &value, sizeof(value), MVT_Map);
  1284. }
  1285. void put_long_mapping(long mapid, char* key, long value)
  1286. {
  1287. _put_mapping(mapid, key, &value, sizeof(value), MVT_Long);
  1288. }
  1289. void put_double_mapping(long mapid, char* key, double value)
  1290. {
  1291. _put_mapping(mapid, key, &value, sizeof(value), MVT_Double);
  1292. }
  1293. char* get_string_mapping(long mapid, char* key)
  1294. {
  1295. return (char*) get_mapping(mapid, key);
  1296. }
  1297. long get_submap_mapping(long mapid, char* key)
  1298. {
  1299. return get_long_mapping(mapid, key);
  1300. }
  1301. long get_long_mapping(long mapid, char* key)
  1302. {
  1303. long *smid = (long*) get_mapping(mapid, key);
  1304. if (smid)
  1305. {
  1306. return *smid;
  1307. }
  1308. return 0;
  1309. }
  1310. double get_double_mapping(long mapid, char* key)
  1311. {
  1312. double *smid = (double*) get_mapping(mapid, key);
  1313. if (smid)
  1314. {
  1315. return *smid;
  1316. }
  1317. return 0;
  1318. }
  1319. void* get_mapping(long mapid, char* key)
  1320. {
  1321. if (global_map == NULL)
  1322. {
  1323. return NULL;
  1324. }
  1325. if (key == NULL)
  1326. {
  1327. return NULL;
  1328. }
  1329. debug(("get_mapping entry\r\n"));
  1330. Map map = (Map) mapid;
  1331. debug(("get_mapping m=%lX\r\n", mapid));
  1332. void *value = NULL;
  1333. if (is_map(map))
  1334. {
  1335. debug(("get_mapping %lX is a map\r\n", mapid));
  1336. if (map->type == MT_HashMap)
  1337. {
  1338. value = hm_get((HashMap) map, key);
  1339. }
  1340. else
  1341. {
  1342. value = tm_get((TreeMap) map, key);
  1343. }
  1344. }
  1345. debug(("get_mapping exit\r\n"));
  1346. return value;
  1347. }
  1348. void* remove_mapping(long mapid, char* key)
  1349. {
  1350. if (global_map == NULL)
  1351. {
  1352. return NULL;
  1353. }
  1354. if (key == NULL)
  1355. {
  1356. return NULL;
  1357. }
  1358. debug(("remove_mapping entry\r\n"));
  1359. Map map = (Map) mapid;
  1360. debug(("remove_mapping m=%lX\r\n", (long)map));
  1361. void *value = NULL;
  1362. if (is_map(map))
  1363. {
  1364. if (map->type == MT_HashMap)
  1365. {
  1366. value = hm_remove((HashMap) map, key);
  1367. }
  1368. else
  1369. {
  1370. value = tm_remove((TreeMap) map, key);
  1371. }
  1372. }
  1373. debug(("remove_mapping exit\r\n"));
  1374. return value;
  1375. }
  1376. void delete_mapping(long mapid)
  1377. {
  1378. if (global_map == NULL)
  1379. {
  1380. return;
  1381. }
  1382. debug(("delete_mapping entry\r\n"));
  1383. Map map = (Map) mapid;
  1384. debug(("delete_mapping m=%lX\r\n", (long)map));
  1385. if (is_map(map))
  1386. {
  1387. hm_remove(global_map, map->id);
  1388. }
  1389. #ifdef _DEBUG
  1390. m_build_string(((Map) global_map), ((Map) global_map)->string_buffer,
  1391. sizeof(((Map) global_map)->string_buffer));
  1392. debug(("global_map map %s\r\n", ((Map)global_map)->string_buffer));
  1393. #endif
  1394. debug(("delete_mapping exit\r\n"));
  1395. }
  1396. void clear_mapping()
  1397. {
  1398. if (global_map == NULL)
  1399. {
  1400. return;
  1401. }
  1402. debug(("clear_mapping entry\r\n"));
  1403. hm_delete_map(global_map);
  1404. global_map = NULL;
  1405. debug(("clear_mapping exit\r\n"));
  1406. }
  1407. MapEntry items_mapping(long mapid)
  1408. {
  1409. if (global_map == NULL)
  1410. {
  1411. return NULL;
  1412. }
  1413. debug(("items_mapping entry\r\n"));
  1414. Map map = (Map) mapid;
  1415. debug(("items_mapping m=%lX\r\n", (long)map));
  1416. MapEntry value = NULL;
  1417. if (is_map(map))
  1418. {
  1419. value = map->items_head;
  1420. }
  1421. debug(("items_mapping exit\r\n"));
  1422. return value;
  1423. }
  1424. int size_mapping(long mapid)
  1425. {
  1426. if (global_map == NULL)
  1427. {
  1428. return 0;
  1429. }
  1430. debug(("size_mapping entry\r\n"));
  1431. Map map = (Map) mapid;
  1432. debug(("size_mapping m=%lX\r\n", (long)map));
  1433. int value = 0;
  1434. if (is_map(map))
  1435. {
  1436. value = map->items_size;
  1437. }
  1438. debug(("size_mapping exit\r\n"));
  1439. return value;
  1440. }
  1441. MapEntry nearby_item_sorted_mapping(long mapid, char* key)
  1442. {
  1443. if (global_map == NULL)
  1444. {
  1445. return NULL;
  1446. }
  1447. if (key == NULL)
  1448. {
  1449. return NULL;
  1450. }
  1451. debug(("nearby_item_sorted_mapping entry\r\n"));
  1452. Map map = (Map) mapid;
  1453. debug(("nearby_item_sorted_mapping m=%lX\r\n", (long)map));
  1454. MapEntry value = NULL;
  1455. if (is_map(map) && map->type == MT_TreeMap)
  1456. {
  1457. value = (MapEntry) tm_get_nearby_entry((TreeMap) map, key);
  1458. }
  1459. debug(("nearby_item_sorted_mapping exit\r\n"));
  1460. return value;
  1461. }
  1462. void put_all_mapping(long destid, long srcid)
  1463. {
  1464. if (global_map == NULL)
  1465. {
  1466. return;
  1467. }
  1468. debug(("clone_mapping entry\r\n"));
  1469. Map destmap = (Map) destid;
  1470. Map srcmap = (Map) srcid;
  1471. debug(("clone_mapping from m=%lX to m=%lX\r\n", (long)srcmap,(long)destmap));
  1472. if (is_map(srcmap) && is_map(destmap))
  1473. {
  1474. if (destmap->type == MT_HashMap)
  1475. {
  1476. hm_put_all_map((HashMap) destmap, srcmap);
  1477. }
  1478. else
  1479. {
  1480. tm_put_all_map((TreeMap) destmap, srcmap);
  1481. }
  1482. }
  1483. debug(("clone_mapping exit\r\n"));
  1484. }
  1485. char* string_mapping(long mapid)
  1486. {
  1487. if (global_map == NULL)
  1488. {
  1489. return NULL;
  1490. }
  1491. debug(("string_mapping entry\r\n"));
  1492. Map map = (Map) mapid;
  1493. debug(("string_mapping m=%lX\r\n", (long)map));
  1494. char *svalue = NULL;
  1495. if (is_map(map))
  1496. {
  1497. if (map->string_buffer[0] == 0)
  1498. {
  1499. m_build_string(map, map->string_buffer, sizeof(map->string_buffer));
  1500. }
  1501. svalue = map->string_buffer;
  1502. }
  1503. debug(("string_mapping exit\r\n"));
  1504. return svalue;
  1505. }
  1506. #include <time.h>
  1507. void performace_test_mapping()
  1508. {
  1509. // referrence java testing code:
  1510. // public static void main(String[] args)
  1511. // {
  1512. // HashMap hm = new HashMap();
  1513. // long t = System.currentTimeMillis();
  1514. // for(int i = 0; i < 5000000; i++)
  1515. // {
  1516. // hm.put("K" + i, "K" + i);
  1517. // }
  1518. // System.out.println("put :" + (System.currentTimeMillis() - t));
  1519. // t = System.currentTimeMillis();
  1520. // for(int i = 0; i < 5000000; i++)
  1521. // {
  1522. // String k = "K" + i;
  1523. // String v = (String)hm.get(k);
  1524. // if (!k.equals(v))
  1525. // {
  1526. // System.out.println("??");
  1527. // }
  1528. // }
  1529. // System.out.println("get :" + (System.currentTimeMillis() - t));
  1530. // }
  1531. // java testing result:
  1532. // hashmap put :14601
  1533. // hashmap get :9268
  1534. // treemap put :12932
  1535. // treemap get :5492
  1536. // c testing result:
  1537. // hashmap put :5319
  1538. // hashmap get :2028
  1539. // treemap put :5803
  1540. // treemap get :2839
  1541. int i;
  1542. char key[100];
  1543. char value[100];
  1544. long hmid = new_sorted_mapping();
  1545. double t = (double) clock() / CLOCKS_PER_SEC * 1000;
  1546. for (i = 0; i < 5000000; i++)
  1547. {
  1548. sprintf(key, "K%d", i);
  1549. sprintf(value, "K%d", i);
  1550. put_string_mapping(hmid, key, value);
  1551. }
  1552. printf("put :%lf\r\n", ((double) clock() / CLOCKS_PER_SEC * 1000 - t));
  1553. t = (double) clock() / CLOCKS_PER_SEC * 1000;
  1554. for (i = 0; i < 5000000; i++)
  1555. {
  1556. sprintf(key, "K%d", i);
  1557. char* v = get_string_mapping(hmid, key);
  1558. if (strcmp(v, key))
  1559. {
  1560. printf("??");
  1561. }
  1562. }
  1563. printf("get :%lf\r\n", ((double) clock() / CLOCKS_PER_SEC * 1000 - t));
  1564. clear_mapping();
  1565. }