Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mViz
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
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
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
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Rico Possienka
mViz
Commits
1e2997cd
Commit
1e2997cd
authored
5 years ago
by
Rico Possienka
Browse files
Options
Downloads
Patches
Plain Diff
add list extension to calc median
parent
7178bd69
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
vis3d/unity/Assets/_app/Scripts/EnumerableEx.cs
+138
-0
138 additions, 0 deletions
vis3d/unity/Assets/_app/Scripts/EnumerableEx.cs
vis3d/unity/Assets/_app/Scripts/EnumerableEx.cs.meta
+11
-0
11 additions, 0 deletions
vis3d/unity/Assets/_app/Scripts/EnumerableEx.cs.meta
with
149 additions
and
0 deletions
vis3d/unity/Assets/_app/Scripts/EnumerableEx.cs
0 → 100644
+
138
−
0
View file @
1e2997cd
using
System
;
using
System.Collections
;
using
System.Collections.Generic
;
using
UnityEngine
;
public
static
class
EnumerableEx
{
static
int
[]
indices
=
new
int
[
1024
];
//https://github.com/jianminchen/AlgorithmsPractice/blob/master/quickSortAlgorithm.cs
//left and right are inclusive
private
static
void
Quick_Sort_indices
<
T
>(
IEnumerable
<
T
>
that
,
int
[]
indices
,
int
left
,
int
right
,
Func
<
T
,
T
,
int
>
comparer
)
{
if
(
left
>
right
||
left
<
0
||
right
<
0
)
return
;
int
index
=
partition
(
that
,
indices
,
left
,
right
,
comparer
);
if
(
index
!=
-
1
)
{
Quick_Sort_indices
(
that
,
indices
,
left
,
index
-
1
,
comparer
);
Quick_Sort_indices
(
that
,
indices
,
index
+
1
,
right
,
comparer
);
}
}
private
static
int
partition
<
T
>(
IEnumerable
<
T
>
that
,
int
[]
indices
,
int
left
,
int
right
,
Func
<
T
,
T
,
int
>
comparer
)
{
if
(
left
>
right
)
return
-
1
;
int
end
=
left
;
T
pivot
=
GetElement
(
that
,
indices
,
right
);
// choose last one to pivot, easy to code
for
(
int
i
=
left
;
i
<
right
;
i
++)
{
if
(
comparer
(
GetElement
(
that
,
indices
,
i
),
pivot
)
<
0
)
{
swap
(
indices
,
i
,
end
);
end
++;
}
}
swap
(
indices
,
end
,
right
);
return
end
;
}
private
static
void
swap
(
int
[]
A
,
int
left
,
int
right
)
{
int
tmp
=
A
[
left
];
A
[
left
]
=
A
[
right
];
A
[
right
]
=
tmp
;
}
private
static
T
GetElement
<
T
>(
IEnumerable
<
T
>
enumerable
,
int
[]
indices
,
int
index
)
{
var
array
=
enumerable
as
T
[];
if
(
array
!=
null
)
{
return
array
[
indices
[
index
]];
}
var
list
=
enumerable
as
List
<
T
>;
if
(
list
!=
null
)
{
return
list
[
indices
[
index
]];
}
int
i
=
0
;
foreach
(
var
item
in
enumerable
)
{
if
(
i
==
index
)
return
item
;
++
i
;
}
throw
new
ArgumentOutOfRangeException
(
"index"
);
}
private
static
int
SortIndices
<
T
>(
IEnumerable
<
T
>
that
,
Func
<
T
,
T
,
int
>
comparer
)
{
int
n
=
Count
(
that
);
if
(
indices
.
Length
<
n
)
{
indices
=
new
int
[
indices
.
Length
+
n
];
}
for
(
int
i
=
0
;
i
!=
n
;
++
i
)
{
indices
[
i
]
=
i
;
}
Quick_Sort_indices
<
T
>(
that
,
indices
,
0
,
n
-
1
,
comparer
);
return
n
;
}
public
static
int
MedianIndex
<
T
>(
this
IEnumerable
<
T
>
that
,
Func
<
T
,
T
,
int
>
comparer
)
{
int
n
=
SortIndices
(
that
,
comparer
);
return
indices
[
n
/
2
];
}
public
static
int
MinIndex
<
T
>(
this
IEnumerable
<
T
>
that
,
Func
<
T
,
T
,
int
>
comparer
)
{
SortIndices
(
that
,
comparer
);
return
indices
[
0
];
}
public
static
int
MaxIndex
<
T
>(
this
IEnumerable
<
T
>
that
,
Func
<
T
,
T
,
int
>
comparer
)
{
int
n
=
SortIndices
(
that
,
comparer
);
return
indices
[
n
-
1
];
}
private
static
int
Count
<
T
>(
IEnumerable
<
T
>
enumerable
)
{
var
array
=
enumerable
as
T
[];
if
(
array
!=
null
)
{
return
array
.
Length
;
}
var
list
=
enumerable
as
List
<
T
>;
if
(
list
!=
null
)
{
return
list
.
Count
;
}
int
n
=
0
;
foreach
(
var
item
in
enumerable
)
{
++
n
;
}
return
n
;
}
}
This diff is collapsed.
Click to expand it.
vis3d/unity/Assets/_app/Scripts/EnumerableEx.cs.meta
0 → 100644
+
11
−
0
View file @
1e2997cd
fileFormatVersion: 2
guid: 2c37a18095b8a6b4bbacd9b6f5cc622c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
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