PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/d/druntime/rt/aaA.d

https://bitbucket.org/goshawk/gdc/
D | 907 lines | 588 code | 114 blank | 205 comment | 116 complexity | 44ac586ccb2b42cc252d0212f17d562b MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. /**
  2. * Implementation of associative arrays.
  3. *
  4. * Copyright: Copyright Digital Mars 2000 - 2010.
  5. * License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
  6. * Authors: Walter Bright, Sean Kelly
  7. */
  8. /* Copyright Digital Mars 2000 - 2010.
  9. * Distributed under the Boost Software License, Version 1.0.
  10. * (See accompanying file LICENSE_1_0.txt or copy at
  11. * http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. module rt.aaA;
  14. private
  15. {
  16. import core.stdc.stdarg;
  17. import core.stdc.string;
  18. import core.stdc.stdio;
  19. enum BlkAttr : uint
  20. {
  21. FINALIZE = 0b0000_0001,
  22. NO_SCAN = 0b0000_0010,
  23. NO_MOVE = 0b0000_0100,
  24. APPENDABLE = 0b0000_1000,
  25. NO_INTERIOR = 0b0001_0000,
  26. ALL_BITS = 0b1111_1111
  27. }
  28. extern (C) void* gc_malloc( size_t sz, uint ba = 0 );
  29. extern (C) void gc_free( void* p );
  30. // Convenience function to make sure the NO_INTERIOR gets set on the
  31. // aaA arrays.
  32. aaA*[] newaaA(size_t len)
  33. {
  34. auto ptr = cast(aaA**) gc_malloc(
  35. len * (aaA*).sizeof, BlkAttr.NO_INTERIOR);
  36. auto ret = ptr[0..len];
  37. ret[] = null;
  38. return ret;
  39. }
  40. }
  41. // Auto-rehash and pre-allocate - Dave Fladebo
  42. static immutable size_t[] prime_list = [
  43. 31UL,
  44. 97UL, 389UL,
  45. 1_543UL, 6_151UL,
  46. 24_593UL, 98_317UL,
  47. 393_241UL, 1_572_869UL,
  48. 6_291_469UL, 25_165_843UL,
  49. 100_663_319UL, 402_653_189UL,
  50. 1_610_612_741UL, 4_294_967_291UL,
  51. // 8_589_934_513UL, 17_179_869_143UL
  52. ];
  53. /* This is the type of the return value for dynamic arrays.
  54. * It should be a type that is returned in registers.
  55. * Although DMD will return types of Array in registers,
  56. * gcc will not, so we instead use a 'long'.
  57. */
  58. alias void[] ArrayRet_t;
  59. struct Array
  60. {
  61. size_t length;
  62. void* ptr;
  63. }
  64. struct aaA
  65. {
  66. aaA *next;
  67. hash_t hash;
  68. /* key */
  69. /* value */
  70. }
  71. struct BB
  72. {
  73. aaA*[] b;
  74. size_t nodes; // total number of aaA nodes
  75. TypeInfo keyti; // TODO: replace this with TypeInfo_AssociativeArray when available in _aaGet()
  76. aaA*[4] binit; // initial value of b[]
  77. }
  78. /* This is the type actually seen by the programmer, although
  79. * it is completely opaque.
  80. */
  81. struct AA
  82. {
  83. BB* a;
  84. }
  85. /**********************************
  86. * Align to next pointer boundary, so that
  87. * GC won't be faced with misaligned pointers
  88. * in value.
  89. */
  90. size_t aligntsize(size_t tsize)
  91. {
  92. version (D_LP64)
  93. // Size of key needed to align value on 16 bytes
  94. return (tsize + 15) & ~(15);
  95. else
  96. return (tsize + size_t.sizeof - 1) & ~(size_t.sizeof - 1);
  97. }
  98. extern (C):
  99. /*************************************************
  100. * Invariant for aa.
  101. */
  102. /+
  103. void _aaInvAh(aaA*[] aa)
  104. {
  105. for (size_t i = 0; i < aa.length; i++)
  106. {
  107. if (aa[i])
  108. _aaInvAh_x(aa[i]);
  109. }
  110. }
  111. private int _aaCmpAh_x(aaA *e1, aaA *e2)
  112. { int c;
  113. c = e1.hash - e2.hash;
  114. if (c == 0)
  115. {
  116. c = e1.key.length - e2.key.length;
  117. if (c == 0)
  118. c = memcmp((char *)e1.key, (char *)e2.key, e1.key.length);
  119. }
  120. return c;
  121. }
  122. private void _aaInvAh_x(aaA *e)
  123. {
  124. hash_t key_hash;
  125. aaA *e1;
  126. aaA *e2;
  127. key_hash = getHash(e.key);
  128. assert(key_hash == e.hash);
  129. while (1)
  130. { int c;
  131. e1 = e.left;
  132. if (e1)
  133. {
  134. _aaInvAh_x(e1); // ordinary recursion
  135. do
  136. {
  137. c = _aaCmpAh_x(e1, e);
  138. assert(c < 0);
  139. e1 = e1.right;
  140. } while (e1 != null);
  141. }
  142. e2 = e.right;
  143. if (e2)
  144. {
  145. do
  146. {
  147. c = _aaCmpAh_x(e, e2);
  148. assert(c < 0);
  149. e2 = e2.left;
  150. } while (e2 != null);
  151. e = e.right; // tail recursion
  152. }
  153. else
  154. break;
  155. }
  156. }
  157. +/
  158. /****************************************************
  159. * Determine number of entries in associative array.
  160. */
  161. size_t _aaLen(AA aa)
  162. in
  163. {
  164. //printf("_aaLen()+\n");
  165. //_aaInv(aa);
  166. }
  167. out (result)
  168. {
  169. size_t len = 0;
  170. if (aa.a)
  171. {
  172. foreach (e; aa.a.b)
  173. {
  174. while (e)
  175. { len++;
  176. e = e.next;
  177. }
  178. }
  179. }
  180. assert(len == result);
  181. //printf("_aaLen()-\n");
  182. }
  183. body
  184. {
  185. return aa.a ? aa.a.nodes : 0;
  186. }
  187. /*************************************************
  188. * Get pointer to value in associative array indexed by key.
  189. * Add entry for key if it is not already there.
  190. */
  191. // retained for backwards compatibility
  192. void* _aaGet(AA* aa, TypeInfo keyti, size_t valuesize, ...)
  193. {
  194. return _aaGetp(aa, keyti, valuesize, cast(void*)(&valuesize + 1));
  195. }
  196. void* _aaGetp(AA* aa, TypeInfo keyti, size_t valuesize, void* pkey)
  197. in
  198. {
  199. assert(aa);
  200. }
  201. out (result)
  202. {
  203. assert(result);
  204. assert(aa.a);
  205. assert(aa.a.b.length);
  206. //assert(_aaInAh(*aa.a, key));
  207. }
  208. body
  209. {
  210. size_t i;
  211. aaA *e;
  212. //printf("keyti = %p\n", keyti);
  213. //printf("aa = %p\n", aa);
  214. immutable keytitsize = keyti.tsize();
  215. if (!aa.a)
  216. { aa.a = new BB();
  217. aa.a.b = aa.a.binit;
  218. }
  219. //printf("aa = %p\n", aa);
  220. //printf("aa.a = %p\n", aa.a);
  221. aa.a.keyti = keyti;
  222. auto key_hash = keyti.getHash(pkey);
  223. //printf("hash = %d\n", key_hash);
  224. i = key_hash % aa.a.b.length;
  225. auto pe = &aa.a.b[i];
  226. while ((e = *pe) !is null)
  227. {
  228. if (key_hash == e.hash)
  229. {
  230. auto c = keyti.compare(pkey, e + 1);
  231. if (c == 0)
  232. goto Lret;
  233. }
  234. pe = &e.next;
  235. }
  236. // Not found, create new elem
  237. //printf("create new one\n");
  238. size_t size = aaA.sizeof + aligntsize(keytitsize) + valuesize;
  239. e = cast(aaA *) gc_malloc(size);
  240. e.next = null;
  241. e.hash = key_hash;
  242. ubyte* ptail = cast(ubyte*)(e + 1);
  243. memcpy(ptail, pkey, keytitsize);
  244. memset(ptail + aligntsize(keytitsize), 0, valuesize); // zero value
  245. *pe = e;
  246. auto nodes = ++aa.a.nodes;
  247. //printf("length = %d, nodes = %d\n", aa.a.b.length, nodes);
  248. if (nodes > aa.a.b.length * 4)
  249. {
  250. //printf("rehash\n");
  251. _aaRehash(aa,keyti);
  252. }
  253. Lret:
  254. return cast(void *)(e + 1) + aligntsize(keytitsize);
  255. }
  256. /*************************************************
  257. * Get pointer to value in associative array indexed by key.
  258. * Returns null if it is not already there.
  259. */
  260. void* _aaGetRvalue(AA aa, TypeInfo keyti, size_t valuesize, ...)
  261. {
  262. return _aaGetRvaluep(aa, keyti, valuesize, cast(void*)(&valuesize + 1));
  263. }
  264. void* _aaGetRvaluep(AA aa, TypeInfo keyti, size_t valuesize, void* pkey)
  265. {
  266. //printf("_aaGetRvalue(valuesize = %u)\n", valuesize);
  267. if (!aa.a)
  268. return null;
  269. auto keysize = aligntsize(keyti.tsize());
  270. auto len = aa.a.b.length;
  271. if (len)
  272. {
  273. auto key_hash = keyti.getHash(pkey);
  274. //printf("hash = %d\n", key_hash);
  275. size_t i = key_hash % len;
  276. auto e = aa.a.b[i];
  277. while (e !is null)
  278. {
  279. if (key_hash == e.hash)
  280. {
  281. auto c = keyti.compare(pkey, e + 1);
  282. if (c == 0)
  283. return cast(void *)(e + 1) + keysize;
  284. }
  285. e = e.next;
  286. }
  287. }
  288. return null; // not found, caller will throw exception
  289. }
  290. /*************************************************
  291. * Determine if key is in aa.
  292. * Returns:
  293. * null not in aa
  294. * !=null in aa, return pointer to value
  295. */
  296. void* _aaIn(AA aa, TypeInfo keyti, ...)
  297. {
  298. return _aaInp(aa, keyti, cast(void*)(&keyti + 1));
  299. }
  300. void* _aaInp(AA aa, TypeInfo keyti, void* pkey)
  301. in
  302. {
  303. }
  304. out (result)
  305. {
  306. //assert(result == 0 || result == 1);
  307. }
  308. body
  309. {
  310. if (aa.a)
  311. {
  312. //printf("_aaIn(), .length = %d, .ptr = %x\n", aa.a.length, cast(uint)aa.a.ptr);
  313. auto len = aa.a.b.length;
  314. if (len)
  315. {
  316. auto key_hash = keyti.getHash(pkey);
  317. //printf("hash = %d\n", key_hash);
  318. const i = key_hash % len;
  319. auto e = aa.a.b[i];
  320. while (e !is null)
  321. {
  322. if (key_hash == e.hash)
  323. {
  324. auto c = keyti.compare(pkey, e + 1);
  325. if (c == 0)
  326. return cast(void *)(e + 1) + aligntsize(keyti.tsize());
  327. }
  328. e = e.next;
  329. }
  330. }
  331. }
  332. // Not found
  333. return null;
  334. }
  335. /*************************************************
  336. * Delete key entry in aa[].
  337. * If key is not in aa[], do nothing.
  338. */
  339. void _aaDel(AA aa, TypeInfo keyti, ...)
  340. {
  341. return _aaDelp(aa, keyti, cast(void*)(&keyti + 1));
  342. }
  343. void _aaDelp(AA aa, TypeInfo keyti, void* pkey)
  344. {
  345. aaA *e;
  346. if (aa.a && aa.a.b.length)
  347. {
  348. auto key_hash = keyti.getHash(pkey);
  349. //printf("hash = %d\n", key_hash);
  350. size_t i = key_hash % aa.a.b.length;
  351. auto pe = &aa.a.b[i];
  352. while ((e = *pe) !is null) // null means not found
  353. {
  354. if (key_hash == e.hash)
  355. {
  356. auto c = keyti.compare(pkey, e + 1);
  357. if (c == 0)
  358. {
  359. *pe = e.next;
  360. aa.a.nodes--;
  361. gc_free(e);
  362. break;
  363. }
  364. }
  365. pe = &e.next;
  366. }
  367. }
  368. }
  369. /********************************************
  370. * Produce array of values from aa.
  371. */
  372. ArrayRet_t _aaValues(AA aa, size_t keysize, size_t valuesize)
  373. {
  374. size_t resi;
  375. Array a;
  376. auto alignsize = aligntsize(keysize);
  377. if (aa.a)
  378. {
  379. a.length = _aaLen(aa);
  380. a.ptr = cast(byte*) gc_malloc(a.length * valuesize,
  381. valuesize < (void*).sizeof ? BlkAttr.NO_SCAN : 0);
  382. resi = 0;
  383. foreach (e; aa.a.b)
  384. {
  385. while (e)
  386. {
  387. memcpy(a.ptr + resi * valuesize,
  388. cast(byte*)e + aaA.sizeof + alignsize,
  389. valuesize);
  390. resi++;
  391. e = e.next;
  392. }
  393. }
  394. assert(resi == a.length);
  395. }
  396. return *cast(ArrayRet_t*)(&a);
  397. }
  398. /********************************************
  399. * Rehash an array.
  400. */
  401. void* _aaRehash(AA* paa, TypeInfo keyti)
  402. in
  403. {
  404. //_aaInvAh(paa);
  405. }
  406. out (result)
  407. {
  408. //_aaInvAh(result);
  409. }
  410. body
  411. {
  412. //printf("Rehash\n");
  413. if (paa.a)
  414. {
  415. BB newb;
  416. auto aa = paa.a;
  417. auto len = _aaLen(*paa);
  418. if (len)
  419. { size_t i;
  420. for (i = 0; i < prime_list.length - 1; i++)
  421. {
  422. if (len <= prime_list[i])
  423. break;
  424. }
  425. len = prime_list[i];
  426. newb.b = newaaA(len);
  427. foreach (e; aa.b)
  428. {
  429. while (e)
  430. { auto enext = e.next;
  431. const j = e.hash % len;
  432. e.next = newb.b[j];
  433. newb.b[j] = e;
  434. e = enext;
  435. }
  436. }
  437. if (aa.b.ptr == aa.binit.ptr)
  438. aa.binit[] = null;
  439. else
  440. delete aa.b;
  441. newb.nodes = aa.nodes;
  442. newb.keyti = aa.keyti;
  443. }
  444. *paa.a = newb;
  445. }
  446. return (*paa).a;
  447. }
  448. /********************************************
  449. * Produce array of N byte keys from aa.
  450. */
  451. ArrayRet_t _aaKeys(AA aa, size_t keysize)
  452. {
  453. auto len = _aaLen(aa);
  454. if (!len)
  455. return null;
  456. auto res = (cast(byte*) gc_malloc(len * keysize,
  457. !(aa.a.keyti.flags() & 1) ? BlkAttr.NO_SCAN : 0))[0 .. len * keysize];
  458. size_t resi = 0;
  459. foreach (e; aa.a.b)
  460. {
  461. while (e)
  462. {
  463. memcpy(&res[resi * keysize], cast(byte*)(e + 1), keysize);
  464. resi++;
  465. e = e.next;
  466. }
  467. }
  468. assert(resi == len);
  469. Array a;
  470. a.length = len;
  471. a.ptr = res.ptr;
  472. return *cast(ArrayRet_t*)(&a);
  473. }
  474. unittest
  475. {
  476. int[string] aa;
  477. aa["hello"] = 3;
  478. assert(aa["hello"] == 3);
  479. aa["hello"]++;
  480. assert(aa["hello"] == 4);
  481. assert(aa.length == 1);
  482. string[] keys = aa.keys;
  483. assert(keys.length == 1);
  484. assert(memcmp(keys[0].ptr, cast(char*)"hello", 5) == 0);
  485. int[] values = aa.values;
  486. assert(values.length == 1);
  487. assert(values[0] == 4);
  488. aa.rehash;
  489. assert(aa.length == 1);
  490. assert(aa["hello"] == 4);
  491. aa["foo"] = 1;
  492. aa["bar"] = 2;
  493. aa["batz"] = 3;
  494. assert(aa.keys.length == 4);
  495. assert(aa.values.length == 4);
  496. foreach(a; aa.keys)
  497. {
  498. assert(a.length != 0);
  499. assert(a.ptr != null);
  500. //printf("key: %.*s -> value: %d\n", a.length, a.ptr, aa[a]);
  501. }
  502. foreach(v; aa.values)
  503. {
  504. assert(v != 0);
  505. //printf("value: %d\n", v);
  506. }
  507. }
  508. /**********************************************
  509. * 'apply' for associative arrays - to support foreach
  510. */
  511. // dg is D, but _aaApply() is C
  512. extern (D) alias int delegate(void *) dg_t;
  513. int _aaApply(AA aa, size_t keysize, dg_t dg)
  514. { int result;
  515. //printf("_aaApply(aa = x%llx, keysize = %d, dg = x%llx)\n", aa.a, keysize, dg);
  516. auto alignsize = aligntsize(keysize);
  517. if (aa.a)
  518. {
  519. Loop:
  520. foreach (e; aa.a.b)
  521. {
  522. while (e)
  523. {
  524. result = dg(cast(void *)(e + 1) + alignsize);
  525. if (result)
  526. break Loop;
  527. e = e.next;
  528. }
  529. }
  530. }
  531. return result;
  532. }
  533. // dg is D, but _aaApply2() is C
  534. extern (D) alias int delegate(void *, void *) dg2_t;
  535. int _aaApply2(AA aa, size_t keysize, dg2_t dg)
  536. { int result;
  537. //printf("_aaApply(aa = x%llx, keysize = %d, dg = x%llx)\n", aa.a, keysize, dg);
  538. auto alignsize = aligntsize(keysize);
  539. if (aa.a)
  540. {
  541. Loop:
  542. foreach (e; aa.a.b)
  543. {
  544. while (e)
  545. {
  546. result = dg(cast(void *)(e + 1), cast(void *)(e + 1) + alignsize);
  547. if (result)
  548. break Loop;
  549. e = e.next;
  550. }
  551. }
  552. }
  553. return result;
  554. }
  555. /***********************************
  556. * Construct an associative array of type ti from
  557. * length pairs of key/value pairs.
  558. */
  559. version (GNU) {} else
  560. extern (C)
  561. BB* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, size_t length, ...)
  562. {
  563. auto valuesize = ti.next.tsize(); // value size
  564. auto keyti = ti.key;
  565. auto keysize = keyti.tsize(); // key size
  566. BB* result;
  567. //printf("_d_assocarrayliteralT(keysize = %d, valuesize = %d, length = %d)\n", keysize, valuesize, length);
  568. //printf("tivalue = %.*s\n", ti.next.classinfo.name);
  569. if (length == 0 || valuesize == 0 || keysize == 0)
  570. {
  571. ;
  572. }
  573. else
  574. {
  575. va_list q;
  576. version(X86_64) va_start(q, __va_argsave); else va_start(q, length);
  577. result = new BB();
  578. result.keyti = keyti;
  579. size_t i;
  580. for (i = 0; i < prime_list.length - 1; i++)
  581. {
  582. if (length <= prime_list[i])
  583. break;
  584. }
  585. auto len = prime_list[i];
  586. result.b = newaaA(len);
  587. size_t keystacksize = (keysize + int.sizeof - 1) & ~(int.sizeof - 1);
  588. size_t valuestacksize = (valuesize + int.sizeof - 1) & ~(int.sizeof - 1);
  589. size_t keytsize = aligntsize(keysize);
  590. for (size_t j = 0; j < length; j++)
  591. { void* pkey = q;
  592. q += keystacksize;
  593. void* pvalue = q;
  594. q += valuestacksize;
  595. aaA* e;
  596. auto key_hash = keyti.getHash(pkey);
  597. //printf("hash = %d\n", key_hash);
  598. i = key_hash % len;
  599. auto pe = &result.b[i];
  600. while (1)
  601. {
  602. e = *pe;
  603. if (!e)
  604. {
  605. // Not found, create new elem
  606. //printf("create new one\n");
  607. e = cast(aaA *) cast(void*) new void[aaA.sizeof + keytsize + valuesize];
  608. memcpy(e + 1, pkey, keysize);
  609. e.hash = key_hash;
  610. *pe = e;
  611. result.nodes++;
  612. break;
  613. }
  614. if (key_hash == e.hash)
  615. {
  616. auto c = keyti.compare(pkey, e + 1);
  617. if (c == 0)
  618. break;
  619. }
  620. pe = &e.next;
  621. }
  622. memcpy(cast(void *)(e + 1) + keytsize, pvalue, valuesize);
  623. }
  624. va_end(q);
  625. }
  626. return result;
  627. }
  628. extern (C)
  629. BB* _d_assocarrayliteralTp(TypeInfo_AssociativeArray ti, void[] keys, void[] values)
  630. {
  631. auto valuesize = ti.next.tsize(); // value size
  632. auto keyti = ti.key;
  633. auto keysize = keyti.tsize(); // key size
  634. auto length = keys.length;
  635. BB* result;
  636. //printf("_d_assocarrayliteralTp(keysize = %d, valuesize = %d, length = %d)\n", keysize, valuesize, length);
  637. //printf("tivalue = %.*s\n", ti.next.classinfo.name);
  638. assert(length == values.length);
  639. if (length == 0 || valuesize == 0 || keysize == 0)
  640. {
  641. ;
  642. }
  643. else
  644. {
  645. result = new BB();
  646. result.keyti = keyti;
  647. size_t i;
  648. for (i = 0; i < prime_list.length - 1; i++)
  649. {
  650. if (length <= prime_list[i])
  651. break;
  652. }
  653. auto len = prime_list[i];
  654. result.b = newaaA(len);
  655. size_t keytsize = aligntsize(keysize);
  656. for (size_t j = 0; j < length; j++)
  657. { auto pkey = keys.ptr + j * keysize;
  658. auto pvalue = values.ptr + j * valuesize;
  659. aaA* e;
  660. auto key_hash = keyti.getHash(pkey);
  661. //printf("hash = %d\n", key_hash);
  662. i = key_hash % len;
  663. auto pe = &result.b[i];
  664. while (1)
  665. {
  666. e = *pe;
  667. if (!e)
  668. {
  669. // Not found, create new elem
  670. //printf("create new one\n");
  671. e = cast(aaA *) cast(void*) new void[aaA.sizeof + keytsize + valuesize];
  672. memcpy(e + 1, pkey, keysize);
  673. e.hash = key_hash;
  674. *pe = e;
  675. result.nodes++;
  676. break;
  677. }
  678. if (key_hash == e.hash)
  679. {
  680. auto c = keyti.compare(pkey, e + 1);
  681. if (c == 0)
  682. break;
  683. }
  684. pe = &e.next;
  685. }
  686. memcpy(cast(void *)(e + 1) + keytsize, pvalue, valuesize);
  687. }
  688. }
  689. return result;
  690. }
  691. /***********************************
  692. * Compare AA contents for equality.
  693. * Returns:
  694. * 1 equal
  695. * 0 not equal
  696. */
  697. int _aaEqual(TypeInfo tiRaw, AA e1, AA e2)
  698. {
  699. //printf("_aaEqual()\n");
  700. //printf("keyti = %.*s\n", ti.key.classinfo.name);
  701. //printf("valueti = %.*s\n", ti.next.classinfo.name);
  702. if (e1.a is e2.a)
  703. return 1;
  704. size_t len = _aaLen(e1);
  705. if (len != _aaLen(e2))
  706. return 0;
  707. // Check for Bug 5925. ti_raw could be a TypeInfo_Const, we need to unwrap
  708. // it until reaching a real TypeInfo_AssociativeArray.
  709. TypeInfo_AssociativeArray ti;
  710. while (true)
  711. {
  712. if ((ti = cast(TypeInfo_AssociativeArray)tiRaw) !is null)
  713. break;
  714. else if (auto tiConst = cast(TypeInfo_Const)tiRaw) {
  715. // The member in object_.d and object.di differ. This is to ensure
  716. // the file can be compiled both independently in unittest and
  717. // collectively in generating the library. Fixing object.di
  718. // requires changes to std.format in Phobos, fixing object_.d
  719. // makes Phobos's unittest fail, so this hack is employed here to
  720. // avoid irrelevant changes.
  721. static if (is(typeof(&tiConst.base) == TypeInfo*))
  722. tiRaw = tiConst.base;
  723. else
  724. tiRaw = tiConst.next;
  725. } else
  726. assert(0); // ???
  727. }
  728. /* Algorithm: Visit each key/value pair in e1. If that key doesn't exist
  729. * in e2, or if the value in e1 doesn't match the one in e2, the arrays
  730. * are not equal, and exit early.
  731. * After all pairs are checked, the arrays must be equal.
  732. */
  733. auto keyti = ti.key;
  734. auto valueti = ti.next;
  735. const keysize = aligntsize(keyti.tsize());
  736. const len2 = e2.a.b.length;
  737. int _aaKeys_x(aaA* e)
  738. {
  739. do
  740. {
  741. auto pkey = cast(void*)(e + 1);
  742. auto pvalue = pkey + keysize;
  743. //printf("key = %d, value = %g\n", *cast(int*)pkey, *cast(double*)pvalue);
  744. // We have key/value for e1. See if they exist in e2
  745. auto key_hash = keyti.getHash(pkey);
  746. //printf("hash = %d\n", key_hash);
  747. const i = key_hash % len2;
  748. auto f = e2.a.b[i];
  749. while (1)
  750. {
  751. //printf("f is %p\n", f);
  752. if (f is null)
  753. return 0; // key not found, so AA's are not equal
  754. if (key_hash == f.hash)
  755. {
  756. //printf("hash equals\n");
  757. auto c = keyti.compare(pkey, f + 1);
  758. if (c == 0)
  759. { // Found key in e2. Compare values
  760. //printf("key equals\n");
  761. auto pvalue2 = cast(void *)(f + 1) + keysize;
  762. if (valueti.equals(pvalue, pvalue2))
  763. {
  764. //printf("value equals\n");
  765. break;
  766. }
  767. else
  768. return 0; // values don't match, so AA's are not equal
  769. }
  770. }
  771. f = f.next;
  772. }
  773. // Look at next entry in e1
  774. e = e.next;
  775. } while (e !is null);
  776. return 1; // this subtree matches
  777. }
  778. foreach (e; e1.a.b)
  779. {
  780. if (e)
  781. { if (_aaKeys_x(e) == 0)
  782. return 0;
  783. }
  784. }
  785. return 1; // equal
  786. }