mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Updated indentation to C3 standard.
This commit is contained in:
@@ -8,32 +8,32 @@ module std::sort;
|
||||
**/
|
||||
macro usz binarysearch(list, x, cmp = null) @builtin
|
||||
{
|
||||
usz i;
|
||||
usz len = @len_from_list(list);
|
||||
for (usz j = len; i < j;)
|
||||
{
|
||||
usz half = i + (j - i) / 2;
|
||||
$if $checks(!cmp):
|
||||
switch
|
||||
{
|
||||
case greater(list[half], x): j = half;
|
||||
case less(list[half], x): i = half + 1;
|
||||
default: return half;
|
||||
}
|
||||
$else
|
||||
$switch
|
||||
$case $checks(cmp(list[0], list[0])):
|
||||
int res = cmp(list[half], x);
|
||||
$case $checks(cmp(&list[0], &list[0])):
|
||||
int res = cmp(&list[half], &x);
|
||||
$endswitch
|
||||
switch
|
||||
{
|
||||
case res > 0: j = half;
|
||||
case res < 0: i = half + 1;
|
||||
default: return half;
|
||||
}
|
||||
$endif
|
||||
}
|
||||
return i;
|
||||
usz i;
|
||||
usz len = @len_from_list(list);
|
||||
for (usz j = len; i < j;)
|
||||
{
|
||||
usz half = i + (j - i) / 2;
|
||||
$if $checks(!cmp):
|
||||
switch
|
||||
{
|
||||
case greater(list[half], x): j = half;
|
||||
case less(list[half], x): i = half + 1;
|
||||
default: return half;
|
||||
}
|
||||
$else
|
||||
$switch
|
||||
$case $checks(cmp(list[0], list[0])):
|
||||
int res = cmp(list[half], x);
|
||||
$case $checks(cmp(&list[0], &list[0])):
|
||||
int res = cmp(&list[half], &x);
|
||||
$endswitch
|
||||
switch
|
||||
{
|
||||
case res > 0: j = half;
|
||||
case res < 0: i = half + 1;
|
||||
default: return half;
|
||||
}
|
||||
$endif
|
||||
}
|
||||
return i;
|
||||
}
|
||||
@@ -8,10 +8,10 @@ import std::sort::qs;
|
||||
**/
|
||||
macro quicksort(list, cmp = null) @builtin
|
||||
{
|
||||
var $Type = $typeof(list);
|
||||
var $CmpType = $typeof(cmp);
|
||||
usz len = sort::@len_from_list(list);
|
||||
qs::qsort(<$Type, $CmpType>)(list, 0, (isz)len - 1, cmp);
|
||||
var $Type = $typeof(list);
|
||||
var $CmpType = $typeof(cmp);
|
||||
usz len = sort::@len_from_list(list);
|
||||
qs::qsort(<$Type, $CmpType>)(list, 0, (isz)len - 1, cmp);
|
||||
}
|
||||
|
||||
module std::sort::qs(<Type, Comparer>);
|
||||
@@ -20,49 +20,49 @@ def ElementType = $typeof(Type{}[0]);
|
||||
|
||||
fn void qsort(Type list, isz low, isz high, Comparer cmp)
|
||||
{
|
||||
if (low >= 0 && high >= 0 && low < high)
|
||||
{
|
||||
isz p = partition(list, low, high, cmp);
|
||||
qsort(list, low, p, cmp);
|
||||
qsort(list, p + 1, high, cmp);
|
||||
}
|
||||
if (low >= 0 && high >= 0 && low < high)
|
||||
{
|
||||
isz p = partition(list, low, high, cmp);
|
||||
qsort(list, low, p, cmp);
|
||||
qsort(list, p + 1, high, cmp);
|
||||
}
|
||||
}
|
||||
|
||||
fn isz partition(Type list, isz low, isz high, Comparer cmp) @inline @local
|
||||
{
|
||||
ElementType pivot = list[low + (high - low) / 2];
|
||||
isz i = low - 1;
|
||||
isz j = high + 1;
|
||||
bool ok;
|
||||
while (true)
|
||||
{
|
||||
do {
|
||||
i++;
|
||||
$switch
|
||||
$case $checks(cmp(list[0], list[0])):
|
||||
ok = cmp(list[i], pivot) < 0;
|
||||
$case $checks(cmp(&list[0], &list[0])):
|
||||
ok = cmp(&list[i], &pivot) < 0;
|
||||
$default:
|
||||
ok = less(list[i], pivot);
|
||||
$endswitch
|
||||
} while (ok);
|
||||
do {
|
||||
j--;
|
||||
$switch
|
||||
$case $checks(cmp(list[0], list[0])):
|
||||
ok = cmp(list[j], pivot) > 0;
|
||||
$case $checks(cmp(&list[0], &list[0])):
|
||||
ok = cmp(&list[j], &pivot) > 0;
|
||||
$default:
|
||||
ok = greater(list[j], pivot);
|
||||
$endswitch
|
||||
} while (ok);
|
||||
if (i >= j) return j;
|
||||
$if $checks(list.swap(0, 0)):
|
||||
list.swap(i, j);
|
||||
$else
|
||||
@swap(list[i], list[j]);
|
||||
$endif
|
||||
}
|
||||
ElementType pivot = list[low + (high - low) / 2];
|
||||
isz i = low - 1;
|
||||
isz j = high + 1;
|
||||
bool ok;
|
||||
while (true)
|
||||
{
|
||||
do {
|
||||
i++;
|
||||
$switch
|
||||
$case $checks(cmp(list[0], list[0])):
|
||||
ok = cmp(list[i], pivot) < 0;
|
||||
$case $checks(cmp(&list[0], &list[0])):
|
||||
ok = cmp(&list[i], &pivot) < 0;
|
||||
$default:
|
||||
ok = less(list[i], pivot);
|
||||
$endswitch
|
||||
} while (ok);
|
||||
do {
|
||||
j--;
|
||||
$switch
|
||||
$case $checks(cmp(list[0], list[0])):
|
||||
ok = cmp(list[j], pivot) > 0;
|
||||
$case $checks(cmp(&list[0], &list[0])):
|
||||
ok = cmp(&list[j], &pivot) > 0;
|
||||
$default:
|
||||
ok = greater(list[j], pivot);
|
||||
$endswitch
|
||||
} while (ok);
|
||||
if (i >= j) return j;
|
||||
$if $checks(list.swap(0, 0)):
|
||||
list.swap(i, j);
|
||||
$else
|
||||
@swap(list[i], list[j]);
|
||||
$endif
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user