 |
Changeset 2084
- Timestamp:
- 04/19/07 20:21:06
(2 years ago)
- Author:
- sean
- Message:
Merged in changes from DMD 1.013:
* An AA fix that was applied only to DMD because it involves the addition of new functions.
* A switch fix that was applied to both DMD and GDC because the changes should work for both.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r1617 |
r2084 |
|
| 34 | 34 | private |
|---|
| 35 | 35 | { |
|---|
| | 36 | import tango.stdc.stdarg; |
|---|
| 36 | 37 | import tango.stdc.string; |
|---|
| 37 | 38 | |
|---|
| … | … | |
| 96 | 97 | { |
|---|
| 97 | 98 | BB* a; |
|---|
| 98 | | version (X86_64) |
|---|
| 99 | | { |
|---|
| 100 | | } |
|---|
| 101 | | else |
|---|
| 102 | | { |
|---|
| 103 | | // This is here only to retain binary compatibility with the |
|---|
| 104 | | // old way we did AA's. Should eventually be removed. |
|---|
| 105 | | //int reserved; |
|---|
| 106 | | } |
|---|
| 107 | 99 | } |
|---|
| 108 | 100 | |
|---|
| … | … | |
| 761 | 753 | return result; |
|---|
| 762 | 754 | } |
|---|
| | 755 | |
|---|
| | 756 | |
|---|
| | 757 | /*********************************** |
|---|
| | 758 | * Construct an associative array of type ti from |
|---|
| | 759 | * length pairs of key/value pairs. |
|---|
| | 760 | */ |
|---|
| | 761 | |
|---|
| | 762 | extern (C) |
|---|
| | 763 | BB* _d_assocarrayliteralT(TypeInfo_AssociativeArray ti, size_t length, ...) |
|---|
| | 764 | { |
|---|
| | 765 | auto valuesize = ti.next.tsize(); // value size |
|---|
| | 766 | auto keyti = ti.key; |
|---|
| | 767 | auto keysize = keyti.tsize(); // key size |
|---|
| | 768 | BB* result; |
|---|
| | 769 | |
|---|
| | 770 | //printf("_d_assocarrayliteralT(keysize = %d, valuesize = %d, length = %d)\n", keysize, valuesize, length); |
|---|
| | 771 | //writefln("tivalue = %s", ti.next.classinfo.name); |
|---|
| | 772 | if (length == 0 || valuesize == 0 || keysize == 0) |
|---|
| | 773 | { |
|---|
| | 774 | ; |
|---|
| | 775 | } |
|---|
| | 776 | else |
|---|
| | 777 | { |
|---|
| | 778 | va_list q; |
|---|
| | 779 | va_start!(size_t)(q, length); |
|---|
| | 780 | |
|---|
| | 781 | result = new BB(); |
|---|
| | 782 | size_t i; |
|---|
| | 783 | |
|---|
| | 784 | for (i = 0; i < prime_list.length - 1; i++) |
|---|
| | 785 | { |
|---|
| | 786 | if (length <= prime_list[i]) |
|---|
| | 787 | break; |
|---|
| | 788 | } |
|---|
| | 789 | auto len = prime_list[i]; |
|---|
| | 790 | result.b = new aaA*[len]; |
|---|
| | 791 | |
|---|
| | 792 | size_t keystacksize = (keysize + int.sizeof - 1) & ~(int.sizeof - 1); |
|---|
| | 793 | size_t valuestacksize = (valuesize + int.sizeof - 1) & ~(int.sizeof - 1); |
|---|
| | 794 | |
|---|
| | 795 | size_t keytsize = aligntsize(keysize); |
|---|
| | 796 | |
|---|
| | 797 | for (size_t j = 0; j < length; j++) |
|---|
| | 798 | { void* pkey = q; |
|---|
| | 799 | q += keystacksize; |
|---|
| | 800 | void* pvalue = q; |
|---|
| | 801 | q += valuestacksize; |
|---|
| | 802 | aaA* e; |
|---|
| | 803 | |
|---|
| | 804 | auto key_hash = keyti.getHash(pkey); |
|---|
| | 805 | //printf("hash = %d\n", key_hash); |
|---|
| | 806 | i = key_hash % len; |
|---|
| | 807 | auto pe = &result.b[i]; |
|---|
| | 808 | while (1) |
|---|
| | 809 | { |
|---|
| | 810 | e = *pe; |
|---|
| | 811 | if (!e) |
|---|
| | 812 | { |
|---|
| | 813 | // Not found, create new elem |
|---|
| | 814 | //printf("create new one\n"); |
|---|
| | 815 | e = cast(aaA *) cast(void*) new void[aaA.sizeof + keytsize + valuesize]; |
|---|
| | 816 | memcpy(e + 1, pkey, keysize); |
|---|
| | 817 | e.hash = key_hash; |
|---|
| | 818 | *pe = e; |
|---|
| | 819 | result.nodes++; |
|---|
| | 820 | break; |
|---|
| | 821 | } |
|---|
| | 822 | if (key_hash == e.hash) |
|---|
| | 823 | { |
|---|
| | 824 | auto c = keyti.compare(pkey, e + 1); |
|---|
| | 825 | if (c == 0) |
|---|
| | 826 | break; |
|---|
| | 827 | pe = (c < 0) ? &e.left : &e.right; |
|---|
| | 828 | } |
|---|
| | 829 | else |
|---|
| | 830 | pe = (key_hash < e.hash) ? &e.left : &e.right; |
|---|
| | 831 | } |
|---|
| | 832 | memcpy(cast(void *)(e + 1) + keytsize, pvalue, valuesize); |
|---|
| | 833 | } |
|---|
| | 834 | |
|---|
| | 835 | va_end(q); |
|---|
| | 836 | } |
|---|
| | 837 | return result; |
|---|
| | 838 | } |
|---|
| r1072 |
r2084 |
|
| 1 | 1 | /* |
|---|
| 2 | | * Copyright (C) 2004 by Digital Mars, www.digitalmars.com |
|---|
| | 2 | * Copyright (C) 2004-2007 by Digital Mars, www.digitalmars.com |
|---|
| 3 | 3 | * Written by Walter Bright |
|---|
| 4 | 4 | * |
|---|
| … | … | |
| 102 | 102 | body |
|---|
| 103 | 103 | { |
|---|
| 104 | | //printf("body _d_switch_string()\n"); |
|---|
| | 104 | //printf("body _d_switch_string(%.*s)\n", ca); |
|---|
| 105 | 105 | int low; |
|---|
| 106 | 106 | int high; |
|---|
| … | … | |
| 112 | 112 | high = table.length; |
|---|
| 113 | 113 | |
|---|
| 114 | | /* |
|---|
| 115 | | // Print table |
|---|
| 116 | | printf("ca[] = '%s'\n", (char *)ca); |
|---|
| 117 | | for (mid = 0; mid < high; mid++) |
|---|
| 118 | | { |
|---|
| 119 | | pca = table[mid]; |
|---|
| 120 | | printf("table[%d] = %d, '%s'\n", mid, pca.length, (char *)pca); |
|---|
| 121 | | } |
|---|
| 122 | | */ |
|---|
| | 114 | version (none) |
|---|
| | 115 | { |
|---|
| | 116 | // Print table |
|---|
| | 117 | printf("ca[] = '%s'\n", cast(char *)ca); |
|---|
| | 118 | for (mid = 0; mid < high; mid++) |
|---|
| | 119 | { |
|---|
| | 120 | pca = table[mid]; |
|---|
| | 121 | printf("table[%d] = %d, '%.*s'\n", mid, pca.length, pca); |
|---|
| | 122 | } |
|---|
| | 123 | } |
|---|
| 123 | 124 | if (high && |
|---|
| 124 | 125 | ca.length >= table[0].length && |
|---|
| … | … | |
| 139 | 140 | if (c == 0) |
|---|
| 140 | 141 | { |
|---|
| 141 | | c = cast(byte)c1 - cast(byte)pca[0]; |
|---|
| | 142 | c = cast(ubyte)c1 - cast(ubyte)pca[0]; |
|---|
| 142 | 143 | if (c == 0) |
|---|
| 143 | 144 | { |
|---|
| r1072 |
r2084 |
|
| 1 | 1 | /* |
|---|
| 2 | | * Copyright (C) 2004 by Digital Mars, www.digitalmars.com |
|---|
| | 2 | * Copyright (C) 2004-2007 by Digital Mars, www.digitalmars.com |
|---|
| 3 | 3 | * Written by Walter Bright |
|---|
| 4 | 4 | * |
|---|
| … | … | |
| 41 | 41 | |
|---|
| 42 | 42 | int _d_switch_string(char[][] table, char[] ca) |
|---|
| 43 | | in |
|---|
| 44 | | { |
|---|
| 45 | | //printf("in _d_switch_string()\n"); |
|---|
| 46 | | assert(table.length >= 0); |
|---|
| 47 | | assert(ca.length >= 0); |
|---|
| 48 | | |
|---|
| 49 | | // Make sure table[] is sorted correctly |
|---|
| 50 | | int j; |
|---|
| 51 | | |
|---|
| 52 | | for (j = 1; j < table.length; j++) |
|---|
| 53 | | { |
|---|
| 54 | | int len1 = table[j - 1].length; |
|---|
| 55 | | int len2 = table[j].length; |
|---|
| 56 | | |
|---|
| 57 | | assert(len1 <= len2); |
|---|
| 58 | | if (len1 == len2) |
|---|
| 59 | | { |
|---|
| 60 | | int ci; |
|---|
| 61 | | |
|---|
| 62 | | ci = memcmp(table[j - 1].ptr, table[j].ptr, len1); |
|---|
| 63 | | assert(ci < 0); // ci==0 means a duplicate |
|---|
| 64 | | } |
|---|
| 65 | | } |
|---|
| 66 | | } |
|---|
| 67 | | out (result) |
|---|
| 68 | | { |
|---|
| 69 | | int i; |
|---|
| 70 | | int cj; |
|---|
| 71 | | |
|---|
| 72 | | //printf("out _d_switch_string()\n"); |
|---|
| 73 | | if (result == -1) |
|---|
| 74 | | { |
|---|
| 75 | | // Not found |
|---|
| 76 | | for (i = 0; i < table.length; i++) |
|---|
| 77 | | { |
|---|
| 78 | | if (table[i].length == ca.length) |
|---|
| 79 | | { cj = memcmp(table[i].ptr, ca.ptr, ca.length); |
|---|
| 80 | | assert(cj != 0); |
|---|
| 81 | | } |
|---|
| 82 | | } |
|---|
| 83 | | } |
|---|
| 84 | | else |
|---|
| 85 | | { |
|---|
| 86 | | assert(0 <= result && result < table.length); |
|---|
| 87 | | for (i = 0; 1; i++) |
|---|
| 88 | | { |
|---|
| 89 | | assert(i < table.length); |
|---|
| 90 | | if (table[i].length == ca.length) |
|---|
| | 43 | in |
|---|
| | 44 | { |
|---|
| | 45 | //printf("in _d_switch_string()\n"); |
|---|
| | 46 | assert(table.length >= 0); |
|---|
| | 47 | assert(ca.length >= 0); |
|---|
| | 48 | |
|---|
| | 49 | // Make sure table[] is sorted correctly |
|---|
| | 50 | int j; |
|---|
| | 51 | |
|---|
| | 52 | for (j = 1; j < table.length; j++) |
|---|
| | 53 | { |
|---|
| | 54 | int len1 = table[j - 1].length; |
|---|
| | 55 | int len2 = table[j].length; |
|---|
| | 56 | |
|---|
| | 57 | assert(len1 <= len2); |
|---|
| | 58 | if (len1 == len2) |
|---|
| | 59 | { |
|---|
| | 60 | int ci; |
|---|
| | 61 | |
|---|
| | 62 | ci = memcmp(table[j - 1].ptr, table[j].ptr, len1); |
|---|
| | 63 | assert(ci < 0); // ci==0 means a duplicate |
|---|
| | 64 | } |
|---|
| | 65 | } |
|---|
| | 66 | } |
|---|
| | 67 | out (result) |
|---|
| | 68 | { |
|---|
| | 69 | int i; |
|---|
| | 70 | int cj; |
|---|
| | 71 | |
|---|
| | 72 | //printf("out _d_switch_string()\n"); |
|---|
| | 73 | if (result == -1) |
|---|
| | 74 | { |
|---|
| | 75 | // Not found |
|---|
| | 76 | for (i = 0; i < table.length; i++) |
|---|
| | 77 | { |
|---|
| | 78 | if (table[i].length == ca.length) |
|---|
| | 79 | { cj = memcmp(table[i].ptr, ca.ptr, ca.length); |
|---|
| | 80 | assert(cj != 0); |
|---|
| | 81 | } |
|---|
| | 82 | } |
|---|
| | 83 | } |
|---|
| | 84 | else |
|---|
| | 85 | { |
|---|
| | 86 | assert(0 <= result && result < table.length); |
|---|
| | 87 | for (i = 0; 1; i++) |
|---|
| | 88 | { |
|---|
| | 89 | assert(i < table.length); |
|---|
| | 90 | if (table[i].length == ca.length) |
|---|
| | 91 | { |
|---|
| | 92 | cj = memcmp(table[i].ptr, ca.ptr, ca.length); |
|---|
| | 93 | if (cj == 0) |
|---|
| 91 | 94 | { |
|---|
| 92 | | cj = memcmp(table[i].ptr, ca.ptr, ca.length); |
|---|
| 93 | | if (cj == 0) |
|---|
| 94 | | { |
|---|
| 95 | | assert(i == result); |
|---|
| 96 | | break; |
|---|
| 97 | | } |
|---|
| 98 | | } |
|---|
| 99 | | } |
|---|
| 100 | | } |
|---|
| 101 | | } |
|---|
| 102 | | body |
|---|
| 103 | | { |
|---|
| 104 | | //printf("body _d_switch_string()\n"); |
|---|
| 105 | | int low; |
|---|
| 106 | | int high; |
|---|
| 107 | | int mid; |
|---|
| 108 | | int c; |
|---|
| 109 | | char[] pca; |
|---|
| 110 | | |
|---|
| 111 | | low = 0; |
|---|
| 112 | | high = table.length; |
|---|
| 113 | | |
|---|
| 114 | | /* |
|---|
| | 95 | assert(i == result); |
|---|
| | 96 | break; |
|---|
| | 97 | } |
|---|
| | 98 | } |
|---|
| | 99 | } |
|---|
| | 100 | } |
|---|
| | 101 | } |
|---|
| | 102 | body |
|---|
| | 103 | { |
|---|
| | 104 | //printf("body _d_switch_string(%.*s)\n", ca); |
|---|
| | 105 | int low; |
|---|
| | 106 | int high; |
|---|
| | 107 | int mid; |
|---|
| | 108 | int c; |
|---|
| | 109 | char[] pca; |
|---|
| | 110 | |
|---|
| | 111 | low = 0; |
|---|
| | 112 | high = table.length; |
|---|
| | 113 | |
|---|
| | 114 | version (none) |
|---|
| | 115 | { |
|---|
| 115 | 116 | // Print table |
|---|
| 116 | | printf("ca[] = '%s'\n", (char *)ca); |
|---|
| | 117 | printf("ca[] = '%s'\n", cast(char *)ca); |
|---|
| 117 | 118 | for (mid = 0; mid < high; mid++) |
|---|
| 118 | 119 | { |
|---|
| 119 | 120 | pca = table[mid]; |
|---|
| 120 | | printf("table[%d] = %d, '%s'\n", mid, pca.length, (char *)pca); |
|---|
| 121 | | } |
|---|
| 122 | | */ |
|---|
| 123 | | if (high && |
|---|
| 124 | | ca.length >= table[0].length && |
|---|
| 125 | | ca.length <= table[high - 1].length) |
|---|
| 126 | | { |
|---|
| 127 | | // Looking for 0 length string, which would only be at the beginning |
|---|
| 128 | | if (ca.length == 0) |
|---|
| 129 | | return 0; |
|---|
| 130 | | |
|---|
| 131 | | char c1 = ca[0]; |
|---|
| 132 | | |
|---|
| 133 | | // Do binary search |
|---|
| 134 | | while (low < high) |
|---|
| 135 | | { |
|---|
| 136 | | mid = (low + high) >> 1; |
|---|
| 137 | | pca = table[mid]; |
|---|
| 138 | | c = ca.length - pca.length; |
|---|
| | 121 | printf("table[%d] = %d, '%.*s'\n", mid, pca.length, pca); |
|---|
| | 122 | } |
|---|
| | 123 | } |
|---|
| | 124 | if (high && |
|---|
| | 125 | ca.length >= table[0].length && |
|---|
| | 126 | ca.length <= table[high - 1].length) |
|---|
| | 127 | { |
|---|
| | 128 | // Looking for 0 length string, which would only be at the beginning |
|---|
| | 129 | if (ca.length == 0) |
|---|
| | 130 | return 0; |
|---|
| | 131 | |
|---|
| | 132 | char c1 = ca[0]; |
|---|
| | 133 | |
|---|
| | 134 | // Do binary search |
|---|
| | 135 | while (low < high) |
|---|
| | 136 | { |
|---|
| | 137 | mid = (low + high) >> 1; |
|---|
| | 138 | pca = table[mid]; |
|---|
| | 139 | c = ca.length - pca.length; |
|---|
| | 140 | if (c == 0) |
|---|
| | 141 | { |
|---|
| | 142 | c = cast(ubyte)c1 - cast(ubyte)pca[0]; |
|---|
| 139 | 143 | if (c == 0) |
|---|
| 140 | 144 | { |
|---|
| 141 | | c = cast(byte)c1 - cast(byte)pca[0]; |
|---|
| | 145 | c = memcmp(ca.ptr, pca.ptr, ca.length); |
|---|
| 142 | 146 | if (c == 0) |
|---|
| 143 | | { |
|---|
| 144 | | c = memcmp(ca.ptr, pca.ptr, ca.length); |
|---|
| 145 | | if (c == 0) |
|---|
| 146 | | { //printf("found %d\n", mid); |
|---|
| 147 | | return mid; |
|---|
| 148 | | } |
|---|
| | 147 | { //printf("found %d\n", mid); |
|---|
| | 148 | return mid; |
|---|
| 149 | 149 | } |
|---|
| 150 | 150 | } |
|---|
| 151 | | if (c < 0) |
|---|
| 152 | | { |
|---|
| 153 | | high = mid; |
|---|
| 154 | | } |
|---|
| 155 | | else |
|---|
| 156 | | { |
|---|
| 157 | | low = mid + 1; |
|---|
| 158 | | } |
|---|
| 159 | | } |
|---|
| 160 | | } |
|---|
| 161 | | |
|---|
| 162 | | //printf("not found\n"); |
|---|
| 163 | | return -1; // not found |
|---|
| 164 | | } |
|---|
| | 151 | } |
|---|
| | 152 | if (c < 0) |
|---|
| | 153 | { |
|---|
| | 154 | high = mid; |
|---|
| | 155 | } |
|---|
| | 156 | else |
|---|
| | 157 | { |
|---|
| | 158 | low = mid + 1; |
|---|
| | 159 | } |
|---|
| | 160 | } |
|---|
| | 161 | } |
|---|
| | 162 | |
|---|
| | 163 | //printf("not found\n"); |
|---|
| | 164 | return -1; // not found |
|---|
| | 165 | } |
|---|
| 165 | 166 | |
|---|
| 166 | 167 | unittest |
|---|
Download in other formats:
|
 |