|
45 | 45 |
|
46 | 46 | namespace Gecode { namespace Kernel { |
47 | 47 |
|
| 48 | + /// Occurence information for a view |
| 49 | + template<class View> |
| 50 | + class ViewOcc { |
| 51 | + public: |
| 52 | + /// The view |
| 53 | + View x; |
| 54 | + /// The original index in the array |
| 55 | + int i; |
| 56 | + /// Sorting order |
| 57 | + bool operator <(const ViewOcc& y) const; |
| 58 | + }; |
| 59 | + |
| 60 | + template<class View> |
| 61 | + forceinline bool |
| 62 | + ViewOcc<View>::operator <(const ViewOcc& y) const { |
| 63 | + return x < y.x; |
| 64 | + } |
| 65 | + |
48 | 66 | /// Check whether \a p has duplicates among its \a n elements (changes \a p) |
49 | 67 | GECODE_KERNEL_EXPORT |
50 | 68 | bool duplicates(void** p, int n); |
@@ -1399,12 +1417,32 @@ namespace Gecode { |
1399 | 1417 | ViewArray<View>::unique(void) { |
1400 | 1418 | if (n < 2) |
1401 | 1419 | return; |
1402 | | - Support::quicksort<View>(x,n); |
1403 | | - int j = 0; |
1404 | | - for (int i=1; i<n; i++) |
1405 | | - if (x[j] != x[i]) |
1406 | | - x[++j] = x[i]; |
1407 | | - n = j+1; |
| 1420 | + Region r; |
| 1421 | + Kernel::ViewOcc<View>* o = r.alloc<Kernel::ViewOcc<View>>(n); |
| 1422 | + for (int i=0; i<n; i++) { |
| 1423 | + o[i].x = x[i]; o[i].i = i; |
| 1424 | + } |
| 1425 | + Support::quicksort<Kernel::ViewOcc<View>>(o,n); |
| 1426 | + // Assign bucket numbers |
| 1427 | + int* bkt = r.alloc<int>(n); |
| 1428 | + int b = 0; |
| 1429 | + bkt[o[0].i] = b; |
| 1430 | + for (int i=1; i<n; i++) { |
| 1431 | + if (o[i-1].x != o[i].x) |
| 1432 | + b++; |
| 1433 | + bkt[o[i].i] = b; |
| 1434 | + } |
| 1435 | + // Eliminate duplicate elements |
| 1436 | + Support::BitSet<Region> seen(r,static_cast<unsigned int>(b+1)); |
| 1437 | + int j=0; |
| 1438 | + for (int i=0; i<n; i++) |
| 1439 | + if (!seen.get(bkt[i])) { |
| 1440 | + x[j++]=x[i]; seen.set(bkt[i]); |
| 1441 | + } else { |
| 1442 | + x[j]=x[i]; |
| 1443 | + } |
| 1444 | + assert(j == b+1); |
| 1445 | + n = j; |
1408 | 1446 | } |
1409 | 1447 |
|
1410 | 1448 | template<class View> |
|
0 commit comments