18 {
19 _source = source;
20 int count = source.Count;
21 int tmp;
22 Hashtable cache = new Hashtable();
23
24 _remapTable = new int[count];
25 _impTable = new int[count];
26
27
28 int impIndex = 0;
29 for (int i = 0; i < count; i++)
30 {
31 T t = source[i];
32 if (cache.ContainsKey(t))
33 {
34 _remapTable[i] = (int) cache[t];
35 }
36 else
37 {
38 _impTable[impIndex] = i;
39 cache[t] = _remapTable[i] = impIndex++;
40 }
41 }
42
43 int impCount = impIndex;
44
45 if (comp != null)
46 {
47
48 int[] sorted = new int[impCount];
49 impIndex = 0;
50 for (int i = 0; i < impCount; i++)
51 {
52
53 int ind = _impTable[i];
54 T t = source[ind];
55
56 sorted[impIndex] = i;
57
58
59 for (int y = 0; y < impIndex; y++)
60 {
61 tmp = sorted[y];
62 if (comp(t, source[_impTable[tmp]]) < 0)
63 {
64 sorted[y] = i;
65
66
67 for (int z = y; z++ < impIndex;)
68 {
69 ind = sorted[z];
70 sorted[z] = tmp;
71 tmp = ind;
72 }
73
74 break;
75 }
76 }
77
78 impIndex++;
79 }
80
81
82 for (int i = 0; i < impCount; i++)
83 {
84 tmp = sorted[i];
85 sorted[i] = _impTable[tmp];
86 _impTable[tmp] = i;
87 }
88
89
90 for (int i = 0; i < count; i++)
91 {
92 _remapTable[i] = _impTable[_remapTable[i]];
93 }
94
95
96 _impTable = sorted;
97 }
98 else
99 {
100 Array.Resize(ref _impTable, impCount);
101 }
102 }