 |
Changeset 3742
- Timestamp:
- 07/10/08 06:23:22
(5 months ago)
- Author:
- larsivi
- Message:
Merged more fixes from trunk.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3697 |
r3742 |
|
| 374 | 374 | /*************************************** |
|---|
| 375 | 375 | * Support for array equality test. |
|---|
| | 376 | * Returns: |
|---|
| | 377 | * 1 equal |
|---|
| | 378 | * 0 not equal |
|---|
| 376 | 379 | */ |
|---|
| 377 | 380 | |
|---|
| 378 | 381 | extern (C) int _adEq(Array a1, Array a2, TypeInfo ti) |
|---|
| 379 | 382 | { |
|---|
| 380 | | /+ |
|---|
| 381 | | + TODO: Re-enable once the correct TypeInfo is passed: |
|---|
| 382 | | + http://d.puremagic.com/issues/show_bug.cgi?id=2161 |
|---|
| 383 | | + |
|---|
| 384 | | debug(adi) printf("_adEq(a1.length = %d, a2.length = %d)\n", a1.length, a2.length); |
|---|
| 385 | | |
|---|
| 386 | | if (a1.length != a2.length) |
|---|
| 387 | | return 0; // not equal |
|---|
| 388 | | if (a1.ptr == a2.ptr) |
|---|
| 389 | | return 1; // equal |
|---|
| 390 | | |
|---|
| 391 | | // We should really have a ti.isPOD() check for this |
|---|
| 392 | | if (ti.tsize() != 1) |
|---|
| 393 | | return ti.equals(&a1, &a2); |
|---|
| 394 | | return memcmp(a1.ptr, a2.ptr, a1.length) == 0; |
|---|
| 395 | | +/ |
|---|
| 396 | 383 | debug(adi) printf("_adEq(a1.length = %d, a2.length = %d)\n", a1.length, a2.length); |
|---|
| 397 | 384 | if (a1.length != a2.length) |
|---|
| … | … | |
| 413 | 400 | } |
|---|
| 414 | 401 | |
|---|
| | 402 | extern (C) int _adEq2(Array a1, Array a2, TypeInfo ti) |
|---|
| | 403 | { |
|---|
| | 404 | debug(adi) printf("_adEq2(a1.length = %d, a2.length = %d)\n", a1.length, a2.length); |
|---|
| | 405 | if (a1.length != a2.length) |
|---|
| | 406 | return 0; // not equal |
|---|
| | 407 | if (!ti.equals(&a1, &a2)) |
|---|
| | 408 | return 0; |
|---|
| | 409 | return 1; |
|---|
| | 410 | } |
|---|
| 415 | 411 | unittest |
|---|
| 416 | 412 | { |
|---|
| … | … | |
| 432 | 428 | extern (C) int _adCmp(Array a1, Array a2, TypeInfo ti) |
|---|
| 433 | 429 | { |
|---|
| 434 | | /+ |
|---|
| 435 | | + TODO: Re-enable once the correct TypeInfo is passed: |
|---|
| 436 | | + http://d.puremagic.com/issues/show_bug.cgi?id=2161 |
|---|
| 437 | | + |
|---|
| 438 | | debug(adi) printf("adCmp()\n"); |
|---|
| 439 | | |
|---|
| 440 | | if (a1.ptr == a2.ptr && |
|---|
| 441 | | a1.length == a2.length) |
|---|
| 442 | | return 0; |
|---|
| 443 | | |
|---|
| 444 | | auto len = a1.length; |
|---|
| 445 | | if (a2.length < len) |
|---|
| 446 | | len = a2.length; |
|---|
| 447 | | |
|---|
| 448 | | // We should really have a ti.isPOD() check for this |
|---|
| 449 | | if (ti.tsize() != 1) |
|---|
| 450 | | return ti.compare(&a1, &a2); |
|---|
| 451 | | auto c = memcmp(a1.ptr, a2.ptr, len); |
|---|
| 452 | | if (c) |
|---|
| 453 | | return c; |
|---|
| 454 | | if (a1.length == a2.length) |
|---|
| 455 | | return 0; |
|---|
| 456 | | return a1.length > a2.length ? 1 : -1; |
|---|
| 457 | | +/ |
|---|
| 458 | 430 | debug(adi) printf("adCmp()\n"); |
|---|
| 459 | 431 | auto len = a1.length; |
|---|
| … | … | |
| 484 | 456 | } |
|---|
| 485 | 457 | |
|---|
| | 458 | extern (C) int _adCmp2(Array a1, Array a2, TypeInfo ti) |
|---|
| | 459 | { |
|---|
| | 460 | debug(adi) printf("_adCmp2(a1.length = %d, a2.length = %d)\n", a1.length, a2.length); |
|---|
| | 461 | return ti.compare(&a1, &a2); |
|---|
| | 462 | } |
|---|
| 486 | 463 | unittest |
|---|
| 487 | 464 | { |
|---|
| r3031 |
r3742 |
|
| 66 | 66 | arraycast.obj \ |
|---|
| 67 | 67 | arraycat.obj \ |
|---|
| | 68 | arraydouble.obj \ |
|---|
| | 69 | arrayfloat.obj \ |
|---|
| | 70 | arrayreal.obj \ |
|---|
| 68 | 71 | cast.obj \ |
|---|
| 69 | 72 | complex.obj \ |
|---|
| … | … | |
| 89 | 92 | OBJ_UTIL= \ |
|---|
| 90 | 93 | util\console.obj \ |
|---|
| | 94 | util\cpuid.obj \ |
|---|
| 91 | 95 | util\ctype.obj \ |
|---|
| 92 | 96 | util\string.obj \ |
|---|
| r3233 |
r3742 |
|
| 1684 | 1684 | * |
|---|
| 1685 | 1685 | * Returns: |
|---|
| 1686 | | * The number of unique elements in buf. |
|---|
| 1687 | | */ |
|---|
| 1688 | | size_t unique( Elem[] buf, Pred2E pred = Pred2E.init ); |
|---|
| | 1686 | * The number of distinct sub-sequences in buf. |
|---|
| | 1687 | */ |
|---|
| | 1688 | size_t distinct( Elem[] buf, Pred2E pred = Pred2E.init ); |
|---|
| 1689 | 1689 | } |
|---|
| 1690 | 1690 | else |
|---|
| 1691 | 1691 | { |
|---|
| 1692 | | template unique_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| | 1692 | template distinct_( Elem, Pred = IsEqual!(Elem) ) |
|---|
| 1693 | 1693 | { |
|---|
| 1694 | 1694 | static assert( isCallableType!(Pred) ); |
|---|
| … | … | |
| 1727 | 1727 | |
|---|
| 1728 | 1728 | |
|---|
| 1729 | | template unique( Buf ) |
|---|
| 1730 | | { |
|---|
| 1731 | | size_t unique( Buf buf ) |
|---|
| 1732 | | { |
|---|
| 1733 | | return unique_!(ElemTypeOf!(Buf)).fn( buf ); |
|---|
| 1734 | | } |
|---|
| 1735 | | } |
|---|
| 1736 | | |
|---|
| 1737 | | |
|---|
| 1738 | | template unique( Buf, Pred ) |
|---|
| 1739 | | { |
|---|
| 1740 | | size_t unique( Buf buf, Pred pred ) |
|---|
| 1741 | | { |
|---|
| 1742 | | return unique_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| | 1729 | template distinct( Buf ) |
|---|
| | 1730 | { |
|---|
| | 1731 | size_t distinct( Buf buf ) |
|---|
| | 1732 | { |
|---|
| | 1733 | return distinct_!(ElemTypeOf!(Buf)).fn( buf ); |
|---|
| | 1734 | } |
|---|
| | 1735 | } |
|---|
| | 1736 | |
|---|
| | 1737 | |
|---|
| | 1738 | template distinct( Buf, Pred ) |
|---|
| | 1739 | { |
|---|
| | 1740 | size_t distinct( Buf buf, Pred pred ) |
|---|
| | 1741 | { |
|---|
| | 1742 | return distinct_!(ElemTypeOf!(Buf), Pred).fn( buf, pred ); |
|---|
| 1743 | 1743 | } |
|---|
| 1744 | 1744 | } |
|---|
| … | … | |
| 1751 | 1751 | void test( char[] buf, char[] pat ) |
|---|
| 1752 | 1752 | { |
|---|
| 1753 | | assert( unique( buf ) == pat.length ); |
|---|
| | 1753 | assert( distinct( buf ) == pat.length ); |
|---|
| 1754 | 1754 | foreach( pos, cur; pat ) |
|---|
| 1755 | 1755 | { |
|---|
| r3734 |
r3742 |
|
| 118 | 118 | } |
|---|
| 119 | 119 | |
|---|
| 120 | | debug(UnitTest) { |
|---|
| 121 | | |
|---|
| 122 | | unittest { |
|---|
| 123 | | |
|---|
| 124 | | assert(isPointerType!(void*)); |
|---|
| 125 | | assert(!isPointerType!(char[])); |
|---|
| 126 | | assert(isPointerType!(char[]*)); |
|---|
| 127 | | assert(!isPointerType!(char*[])); |
|---|
| 128 | | assert(isPointerType!(real*)); |
|---|
| 129 | | assert(!isPointerType!(uint)); |
|---|
| 130 | | |
|---|
| | 120 | debug( UnitTest ) |
|---|
| | 121 | { |
|---|
| | 122 | unittest |
|---|
| | 123 | { |
|---|
| | 124 | assert( isPointerType!(void*) ); |
|---|
| | 125 | assert( !isPointerType!(char[]) ); |
|---|
| | 126 | assert( isPointerType!(char[]*) ); |
|---|
| | 127 | assert( !isPointerType!(char*[]) ); |
|---|
| | 128 | assert( isPointerType!(real*) ); |
|---|
| | 129 | assert( !isPointerType!(uint) ); |
|---|
| | 130 | |
|---|
| 131 | 131 | class Ham |
|---|
| 132 | 132 | { |
|---|
| 133 | 133 | void* a; |
|---|
| 134 | 134 | } |
|---|
| 135 | | |
|---|
| 136 | | assert(!isPointerType!(Ham)); |
|---|
| 137 | | |
|---|
| | 135 | |
|---|
| | 136 | assert( !isPointerType!(Ham) ); |
|---|
| | 137 | |
|---|
| 138 | 138 | union Eggs |
|---|
| 139 | 139 | { |
|---|
| 140 | 140 | void* a; |
|---|
| 141 | | uint b; |
|---|
| | 141 | uint b; |
|---|
| 142 | 142 | }; |
|---|
| 143 | | |
|---|
| 144 | | assert(!isPointerType!(Eggs)); |
|---|
| 145 | | assert(isPointerType!(Eggs*)); |
|---|
| 146 | | |
|---|
| | 143 | |
|---|
| | 144 | assert( !isPointerType!(Eggs) ); |
|---|
| | 145 | assert( isPointerType!(Eggs*) ); |
|---|
| | 146 | |
|---|
| 147 | 147 | struct Bacon {}; |
|---|
| 148 | | |
|---|
| 149 | | assert(!isPointerType!(Bacon)); |
|---|
| | 148 | |
|---|
| | 149 | assert( !isPointerType!(Bacon) ); |
|---|
| 150 | 150 | |
|---|
| 151 | 151 | } |
|---|
| … | … | |
| 176 | 176 | * Evaluates to true if T is a static array type. |
|---|
| 177 | 177 | */ |
|---|
| 178 | | template isStaticArrayType(T : T[U], size_t U) |
|---|
| 179 | | { |
|---|
| 180 | | const bool isStaticArrayType = true; |
|---|
| 181 | | } |
|---|
| 182 | | |
|---|
| 183 | | template isStaticArrayType(T) |
|---|
| 184 | | { |
|---|
| 185 | | const bool isStaticArrayType = false; |
|---|
| 186 | | } |
|---|
| 187 | | |
|---|
| 188 | | debug (UnitTest) { |
|---|
| 189 | | |
|---|
| 190 | | unittest { |
|---|
| 191 | | assert(isStaticArrayType!(char[15])); |
|---|
| 192 | | assert(!isStaticArrayType!(char[])); |
|---|
| 193 | | |
|---|
| 194 | | assert(isDynamicArrayType!(char[])); |
|---|
| 195 | | assert(!isDynamicArrayType!(char[15])); |
|---|
| | 178 | version( GNU ) |
|---|
| | 179 | { |
|---|
| | 180 | // GDC should also be able to use the other version, but it probably |
|---|
| | 181 | // relies on a frontend fix in one of the latest DMD versions - will |
|---|
| | 182 | // remove this when GDC is ready. For now, this code pass the unittests. |
|---|
| | 183 | private template isStaticArrayTypeInst( T ) |
|---|
| | 184 | { |
|---|
| | 185 | const T isStaticArrayTypeInst = void; |
|---|
| | 186 | } |
|---|
| | 187 | |
|---|
| | 188 | template isStaticArrayType( T ) |
|---|
| | 189 | { |
|---|
| | 190 | static if( is( typeof(T.length) ) && !is( typeof(T) == typeof(T.init) ) ) |
|---|
| | 191 | { |
|---|
| | 192 | const bool isStaticArrayType = is( T == typeof(T[0])[isStaticArrayTypeInst!(T).length] ); |
|---|
| | 193 | } |
|---|
| | 194 | else |
|---|
| | 195 | { |
|---|
| | 196 | const bool isStaticArrayType = false; |
|---|
| | 197 | } |
|---|
| | 198 | } |
|---|
| | 199 | } |
|---|
| | 200 | else |
|---|
| | 201 | { |
|---|
| | 202 | template isStaticArrayType( T : T[U], size_t U ) |
|---|
| | 203 | { |
|---|
| | 204 | const bool isStaticArrayType = true; |
|---|
| | 205 | } |
|---|
| | 206 | |
|---|
| | 207 | template isStaticArrayType( T ) |
|---|
| | 208 | { |
|---|
| | 209 | const bool isStaticArrayType = false; |
|---|
| | 210 | } |
|---|
| | 211 | } |
|---|
| | 212 | |
|---|
| | 213 | debug( UnitTest ) |
|---|
| | 214 | { |
|---|
| | 215 | unittest |
|---|
| | 216 | { |
|---|
| | 217 | assert( isStaticArrayType!(char[5][2]) ); |
|---|
| | 218 | assert( !isDynamicArrayType!(char[5][2]) ); |
|---|
| | 219 | |
|---|
| | 220 | assert( isStaticArrayType!(char[15]) ); |
|---|
| | 221 | assert( !isStaticArrayType!(char[]) ); |
|---|
| | 222 | |
|---|
| | 223 | assert( isDynamicArrayType!(char[]) ); |
|---|
| | 224 | assert( !isDynamicArrayType!(char[15]) ); |
|---|
| 196 | 225 | } |
|---|
| 197 | 226 | } |
|---|
| r3713 |
r3742 |
|
| 126 | 126 | { |
|---|
| 127 | 127 | assert(data); |
|---|
| | 128 | assert(buff.length >= allocateEncodeSize(data)); |
|---|
| 128 | 129 | } |
|---|
| 129 | 130 | body |
|---|
| … | … | |
| 160 | 161 | |
|---|
| 161 | 162 | return rtn; |
|---|
| | 163 | } |
|---|
| | 164 | |
|---|
| | 165 | /******************************************************************************* |
|---|
| | 166 | |
|---|
| | 167 | encodes data and returns as an ASCII base64 string. |
|---|
| | 168 | |
|---|
| | 169 | Params: |
|---|
| | 170 | data = what is to be encoded |
|---|
| | 171 | |
|---|
| | 172 | Example: |
|---|
| | 173 | --- |
|---|
| | 174 | char[] myEncodedString = encode(cast(ubyte[])"Hello, how are you today?"); |
|---|
| | 175 | Stdout(myEncodedString).newline; // SGVsbG8sIGhvdyBhcmUgeW91IHRvZGF5Pw== |
|---|
| | 176 | --- |
|---|
| | 177 | |
|---|
| | 178 | |
|---|
| | 179 | *******************************************************************************/ |
|---|
| | 180 | |
|---|
| | 181 | |
|---|
| | 182 | char[] encode(ubyte[] data) |
|---|
| | 183 | in |
|---|
| | 184 | { |
|---|
| | 185 | assert(data); |
|---|
| | 186 | } |
|---|
| | 187 | body |
|---|
| | 188 | { |
|---|
| | 189 | auto rtn = new char[allocateEncodeSize(data)]; |
|---|
| | 190 | return encode(data, rtn); |
|---|
| 162 | 191 | } |
|---|
| 163 | 192 | |
|---|
| … | … | |
| 300 | 329 | char[] result = encode(cast(ubyte[])"Hello, how are you today?", encoded); |
|---|
| 301 | 330 | if (result == "SGVsbG8sIGhvdyBhcmUgeW91IHRvZGF5Pw==") |
|---|
| 302 | | return Test.Status.Success; |
|---|
| | 331 | { |
|---|
| | 332 | char[] result2 = encode(cast(ubyte[])"Hello, how are you today?"); |
|---|
| | 333 | if (result == "SGVsbG8sIGhvdyBhcmUgeW91IHRvZGF5Pw==") |
|---|
| | 334 | return Test.Status.Success; |
|---|
| | 335 | } |
|---|
| | 336 | |
|---|
| 303 | 337 | return Test.Status.Failure; |
|---|
| 304 | 338 | } |
|---|
| r3723 |
r3742 |
|
| 16 | 16 | *******************************************************************************/ |
|---|
| 17 | 17 | |
|---|
| 18 | | module tango.math.Kiss; |
|---|
| | 18 | module tango.math.random.Kiss; |
|---|
| 19 | 19 | |
|---|
| 20 | 20 | |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic