Jump to content

Category talk:Wren-sort: Difference between revisions

→‎Source code: Made several methods 'chaining' friendly.
(→‎Source code: Added SortedList class.)
(→‎Source code: Made several methods 'chaining' friendly.)
Line 77:
Sort contains various sorting methods which may be useful in different scenarios.
As it would be too expensive to check that all elements of a large list are of the
same type or compatible types, errors are left to emerge naturally. In place sorting
methods return the original list after sorting.
*/
class Sort {
Line 87 ⟶ 88:
isList_(a)
var c = a.count
if (c < 2 || s < 0 || s >= c || e <= s || e >= c) return a
if (!e) e = c - 1
if (cmp == true) cmp = Cmp.defaultDesc(a[0])
if (!cmp) cmp = Cmp.default(a[0])
quick_(a, s, e, cmp)
return a
}
 
Line 157 ⟶ 159:
isList_(a)
var count = a.count
if (count < 2) return a
if (cmp == true) cmp = Cmp.defaultDesc(a[0])
if (!cmp) cmp = Cmp.default(a[0])
Line 173 ⟶ 175:
siftDown_(a, 0, end, cmp)
}
return a
}
 
Line 196 ⟶ 199:
isList_(a)
var c = a.count
if (c < 2) return a
if (cmp == true) cmp = Cmp.defaultDesc(a[0])
if (!cmp) cmp = Cmp.default(a[0])
Line 208 ⟶ 211:
a[j+1] = v
}
return a
}
 
Line 214 ⟶ 218:
isList_(a)
var c = a.count
if (c < 2) return a
if (cmp == true) cmp = Cmp.defaultDesc(a[0])
if (!cmp) cmp = Cmp.default(a[0])
Line 229 ⟶ 233:
}
}
return a
}
 
Line 235 ⟶ 240:
isList_(a)
var n = a.count
if (n < 2) return a
if (cmp == true) cmp = Cmp.defaultDesc(a[0])
if (!cmp) cmp = Cmp.default(a[0])
Line 252 ⟶ 257:
}
}
return a
}
 
Line 293 ⟶ 299:
static isSortedDesc(a) { isSorted(a, true) }
 
// Reverses a list in place and returns it.
static reverse(a) {
var c = a.count
if (c < 2) return a
var i = 0
var j = a.count - 1
Line 306 ⟶ 312:
j = j - 1
}
return a
}
}
Line 611 ⟶ 618:
removeAt(i) { _lst.removeAt(i) }
 
// Removes all duplicates from 'this' and returns the result.
prune() {
var c = _lst.count
if (c < 2) return this
for (i in c-1..1) {
if (_lst[i-1] == _lst[i]) _lst.removeAt(i)
}
return this
}
 
Line 650 ⟶ 658:
asList { _lst }
 
// Resort 'this' using a (different) comparison function and returns the result.
resort(cmp) {
SortedList.isCmp_(cmp)
_cmp = cmp
SortedList.sortAll_(_lst, _cmp)
return this
}
 
// Resort 'this' using the current comparison function. Notand usuallyreturns neededthe thoughresult. see 'asList'.
// Not usually needed though see 'asList'.
resort() {
SortedList.sortAll_(_lst, _cmp)
return this
}
 
9,482

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.