 |
Changeset 3358
- Timestamp:
- 03/15/08 14:49:29
(9 months ago)
- Author:
- kris
- Message:
changed Sorting mechanism to use a delegate instead of an interface
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r2809 |
r3358 |
|
| 618 | 618 | int mid = (lo + hi) / 2; |
|---|
| 619 | 619 | |
|---|
| 620 | | if (cmp.compare(s[lo], s[mid]) > 0) |
|---|
| | 620 | if (cmp(s[lo], s[mid]) > 0) |
|---|
| 621 | 621 | { |
|---|
| 622 | 622 | T tmp = s[lo]; |
|---|
| … | … | |
| 625 | 625 | } |
|---|
| 626 | 626 | |
|---|
| 627 | | if (cmp.compare(s[mid], s[hi]) > 0) |
|---|
| | 627 | if (cmp(s[mid], s[hi]) > 0) |
|---|
| 628 | 628 | { |
|---|
| 629 | 629 | T tmp = s[mid]; |
|---|
| … | … | |
| 631 | 631 | s[hi] = tmp; // swap |
|---|
| 632 | 632 | |
|---|
| 633 | | if (cmp.compare(s[lo], s[mid]) > 0) |
|---|
| | 633 | if (cmp(s[lo], s[mid]) > 0) |
|---|
| 634 | 634 | { |
|---|
| 635 | 635 | T tmp2 = s[lo]; |
|---|
| … | … | |
| 648 | 648 | for (;;) |
|---|
| 649 | 649 | { |
|---|
| 650 | | while (cmp.compare(s[right], partition) > 0) |
|---|
| | 650 | while (cmp(s[right], partition) > 0) |
|---|
| 651 | 651 | --right; |
|---|
| 652 | 652 | |
|---|
| 653 | | while (left < right && cmp.compare(s[left], partition) <= 0) |
|---|
| | 653 | while (left < right && cmp(s[left], partition) <= 0) |
|---|
| 654 | 654 | ++left; |
|---|
| 655 | 655 | |
|---|
| r2809 |
r3358 |
|
| 23 | 23 | private import tango.util.collection.impl.RBCell, |
|---|
| 24 | 24 | tango.util.collection.impl.BagCollection, |
|---|
| 25 | | tango.util.collection.impl.AbstractIterator, |
|---|
| 26 | | tango.util.collection.impl.DefaultComparator; |
|---|
| | 25 | tango.util.collection.impl.AbstractIterator; |
|---|
| 27 | 26 | |
|---|
| 28 | 27 | /** |
|---|
| … | … | |
| 102 | 101 | cmp_ = cmp; |
|---|
| 103 | 102 | else |
|---|
| 104 | | cmp_ = new DefaultComparator!(T); |
|---|
| 105 | | } |
|---|
| | 103 | cmp_ = &compare; |
|---|
| | 104 | } |
|---|
| | 105 | |
|---|
| | 106 | /** |
|---|
| | 107 | * The default comparator |
|---|
| | 108 | * |
|---|
| | 109 | * @param fst first argument |
|---|
| | 110 | * @param snd second argument |
|---|
| | 111 | * Returns: a negative number if fst is less than snd; a |
|---|
| | 112 | * positive number if fst is greater than snd; else 0 |
|---|
| | 113 | **/ |
|---|
| | 114 | |
|---|
| | 115 | private final int compare(T fst, T snd) |
|---|
| | 116 | { |
|---|
| | 117 | if (fst is snd) |
|---|
| | 118 | return 0; |
|---|
| | 119 | |
|---|
| | 120 | return typeid(T).compare (&fst, &snd); |
|---|
| | 121 | } |
|---|
| | 122 | |
|---|
| 106 | 123 | |
|---|
| 107 | 124 | /** |
|---|
| … | … | |
| 193 | 210 | cmp_ = cmp; |
|---|
| 194 | 211 | else |
|---|
| 195 | | cmp_ = new DefaultComparator!(T); |
|---|
| | 212 | cmp_ = &compare; |
|---|
| 196 | 213 | |
|---|
| 197 | 214 | if (count !is 0) |
|---|
| … | … | |
| 328 | 345 | for (;;) |
|---|
| 329 | 346 | { |
|---|
| 330 | | int diff = cmp_.compare(element, t.element()); |
|---|
| | 347 | int diff = cmp_(element, t.element()); |
|---|
| 331 | 348 | if (diff is 0 && checkOccurrence) |
|---|
| 332 | 349 | return ; |
|---|
| … | … | |
| 417 | 434 | T v = t.element(); |
|---|
| 418 | 435 | if (last !is T.init) |
|---|
| 419 | | assert(cmp_.compare(last, v) <= 0); |
|---|
| | 436 | assert(cmp_(last, v) <= 0); |
|---|
| 420 | 437 | last = v; |
|---|
| 421 | 438 | t = t.successor(); |
|---|
| r2809 |
r3358 |
|
| 509 | 509 | for (;;) |
|---|
| 510 | 510 | { |
|---|
| 511 | | int diff = cmp.compare(key, t.key()); |
|---|
| | 511 | int diff = cmp(key, t.key()); |
|---|
| 512 | 512 | if (diff is 0 && checkOccurrence) |
|---|
| 513 | 513 | { |
|---|
| … | … | |
| 568 | 568 | { |
|---|
| 569 | 569 | K v = t.key(); |
|---|
| 570 | | assert((last is K.init || cmp.compare(last, v) <= 0)); |
|---|
| | 570 | assert((last is K.init || cmp(last, v) <= 0)); |
|---|
| 571 | 571 | last = v; |
|---|
| 572 | 572 | t = cast(RBPairT)(t.successor()); |
|---|
| r2809 |
r3358 |
|
| 252 | 252 | } |
|---|
| 253 | 253 | |
|---|
| 254 | | int diff = cmp.compare(a.element(), b.element()); |
|---|
| | 254 | int diff = cmp (a.element(), b.element()); |
|---|
| 255 | 255 | if (diff <= 0) |
|---|
| 256 | 256 | { |
|---|
| r2809 |
r3358 |
|
| 270 | 270 | for (;;) |
|---|
| 271 | 271 | { |
|---|
| 272 | | int diff = cmp.compare(element, t.element()); |
|---|
| | 272 | int diff = cmp(element, t.element()); |
|---|
| 273 | 273 | if (diff is 0) |
|---|
| 274 | 274 | return t; |
|---|
| … | … | |
| 295 | 295 | while (t !is null) |
|---|
| 296 | 296 | { |
|---|
| 297 | | int diff = cmp.compare(element, t.element()); |
|---|
| | 297 | int diff = cmp(element, t.element()); |
|---|
| 298 | 298 | if (diff is 0) |
|---|
| 299 | 299 | { |
|---|
| r2809 |
r3358 |
|
| 153 | 153 | for (;;) |
|---|
| 154 | 154 | { |
|---|
| 155 | | int diff = cmp.compare(key, t.key_); |
|---|
| | 155 | int diff = cmp(key, t.key_); |
|---|
| 156 | 156 | if (diff is 0) |
|---|
| 157 | 157 | return t; |
|---|
| … | … | |
| 177 | 177 | for (;;) |
|---|
| 178 | 178 | { |
|---|
| 179 | | int diff = cmp.compare(key, t.key_); |
|---|
| | 179 | int diff = cmp(key, t.key_); |
|---|
| 180 | 180 | if (diff is 0 && t.element() == (element)) |
|---|
| 181 | 181 | return t; |
|---|
| … | … | |
| 202 | 202 | while (t !is null) |
|---|
| 203 | 203 | { |
|---|
| 204 | | int diff = cmp.compare(key, t.key_); |
|---|
| | 204 | int diff = cmp(key, t.key_); |
|---|
| 205 | 205 | // rely on insert to always go left on <= |
|---|
| 206 | 206 | if (diff is 0) |
|---|
| … | … | |
| 225 | 225 | while (t !is null) |
|---|
| 226 | 226 | { |
|---|
| 227 | | int diff = cmp.compare(key, t.key_); |
|---|
| | 227 | int diff = cmp(key, t.key_); |
|---|
| 228 | 228 | if (diff is 0) |
|---|
| 229 | 229 | { |
|---|
| r2809 |
r3358 |
|
| 28 | 28 | **/ |
|---|
| 29 | 29 | |
|---|
| | 30 | template Comparator(T) |
|---|
| | 31 | { |
|---|
| | 32 | alias int delegate(T, T) Comparator; |
|---|
| | 33 | } |
|---|
| | 34 | |
|---|
| | 35 | /+ |
|---|
| 30 | 36 | public interface Comparator(T) |
|---|
| 31 | 37 | { |
|---|
| … | … | |
| 38 | 44 | public int compare(T fst, T snd); |
|---|
| 39 | 45 | } |
|---|
| 40 | | |
|---|
| | 46 | +/ |
|---|
| r2809 |
r3358 |
|
| 33 | 33 | * obtained in succession from nextElement(), that |
|---|
| 34 | 34 | * <PRE> |
|---|
| 35 | | * comparator().compare(a, b) <= 0. |
|---|
| | 35 | * comparator(a, b) <= 0. |
|---|
| 36 | 36 | * </PRE> |
|---|
| 37 | 37 | * |
|---|
| r2809 |
r3358 |
|
| 30 | 30 | * obtained in succession from keys().nextElement(), that |
|---|
| 31 | 31 | * <PRE> |
|---|
| 32 | | * comparator().compare(a, b) <= 0. |
|---|
| | 32 | * comparator(a, b) <= 0. |
|---|
| 33 | 33 | * </PRE> |
|---|
| 34 | 34 | * |
|---|
| r2809 |
r3358 |
|
| 31 | 31 | * obtained in succession from elements().nextElement(), that |
|---|
| 32 | 32 | * <PRE> |
|---|
| 33 | | * comparator().compare(a, b) <= 0. |
|---|
| | 33 | * comparator(a, b) <= 0. |
|---|
| 34 | 34 | * </PRE> |
|---|
| 35 | 35 | * |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic