Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Molstar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michal Malý
Molstar
Commits
66072249
Commit
66072249
authored
7 years ago
by
David Sehnal
Browse files
Options
Downloads
Patches
Plain Diff
specialized array sort is back (because ordered sets need it)
parent
d4e1950c
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/structure/collections/sort.ts
+61
-1
61 additions, 1 deletion
src/structure/collections/sort.ts
with
61 additions
and
1 deletion
src/structure/collections/sort.ts
+
61
−
1
View file @
66072249
...
...
@@ -83,12 +83,72 @@ function quickSort(ctx: Ctx, low: number, high: number) {
}
}
function
partitionArrayAsc
(
data
:
number
[],
parts
:
number
[],
l
:
number
,
r
:
number
)
{
let
equals
=
l
+
1
,
tail
=
r
;
// move the median to the 1st spot
arraySwap
(
data
,
l
,
medianPivotIndex
(
data
,
arrayLess
,
l
,
r
));
const
pivot
=
data
[
l
];
while
(
data
[
tail
]
>
pivot
)
{
--
tail
;
}
for
(
let
i
=
l
+
1
;
i
<=
tail
;
i
++
)
{
const
v
=
data
[
i
];
if
(
v
>
pivot
)
{
arraySwap
(
data
,
i
,
tail
);
--
tail
;
while
(
data
[
tail
]
>
pivot
)
{
--
tail
;
}
i
--
;
}
else
if
(
v
===
pivot
)
{
arraySwap
(
data
,
i
,
equals
);
++
equals
;
}
}
// move all medians to the correct spots
for
(
let
i
=
l
;
i
<
equals
;
i
++
)
{
arraySwap
(
data
,
i
,
l
+
tail
-
i
);
}
parts
[
0
]
=
tail
-
equals
+
l
+
1
;
parts
[
1
]
=
tail
;
}
function
insertionSortArrayAsc
(
data
:
number
[],
start
:
number
,
end
:
number
)
{
for
(
let
i
=
start
+
1
;
i
<=
end
;
i
++
)
{
const
key
=
data
[
i
];
let
j
=
i
-
1
;
while
(
j
>=
start
&&
data
[
j
]
>
key
)
{
data
[
j
+
1
]
=
data
[
j
];
j
=
j
-
1
;
}
data
[
j
+
1
]
=
key
;
}
}
function
quickSortArrayAsc
(
data
:
number
[],
parts
:
number
[],
low
:
number
,
high
:
number
)
{
while
(
low
<
high
)
{
if
(
high
-
low
<
16
)
{
insertionSortArrayAsc
(
data
,
low
,
high
);
return
;
}
partitionArrayAsc
(
data
,
parts
,
low
,
high
);
const
li
=
parts
[
0
],
ri
=
parts
[
1
];
if
(
li
-
low
<
high
-
ri
)
{
quickSortArrayAsc
(
data
,
parts
,
low
,
li
-
1
);
low
=
ri
+
1
;
}
else
{
quickSortArrayAsc
(
data
,
parts
,
ri
+
1
,
high
);
high
=
li
-
1
;
}
}
}
export
function
sortArray
(
data
:
ArrayLike
<
number
>
,
cmp
:
Comparer
<
ArrayLike
<
number
>>
=
arrayLess
):
ArrayLike
<
number
>
{
return
sortArrayRange
(
data
,
0
,
data
.
length
,
cmp
);
}
export
function
sortArrayRange
(
data
:
ArrayLike
<
number
>
,
start
:
number
,
end
:
number
,
cmp
:
Comparer
<
ArrayLike
<
number
>>
=
arrayLess
):
ArrayLike
<
number
>
{
quickSort
({
data
,
cmp
,
swap
:
arraySwap
,
parts
:
[
0
,
0
]
},
start
,
end
-
1
);
if
(
cmp
===
arrayLess
)
quickSortArrayAsc
(
data
as
any
,
[
0
,
0
],
start
,
end
-
1
);
else
quickSort
({
data
,
cmp
,
swap
:
arraySwap
,
parts
:
[
0
,
0
]
},
start
,
end
-
1
);
return
data
;
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment