FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
string-sorting/external/lcp-quicksort.cpp at master · rantala/string-sorting · GitHub
rantala
/
string-sorting
Public
Notifications
You must be signed in to change notification settings
Fork
15
Star
58
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
string-sorting
/
external
/
lcp-quicksort.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
62 lines (50 loc) · 1.75 KB
Breadcrumbs
string-sorting
/
external
/
lcp-quicksort.cpp
Copy path
File metadata and controls
62 lines (50 loc) · 1.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#
include
<
stdlib.h
>
#
include
<
algorithm
>
#
include
"
routine.h
"
typedef
int
Lcp;
inline
int
lcpstrcmp
(
unsigned
char
const
*
const
p,
unsigned
char
const
*
const
q, Lcp &i) {
for
( ; !(q[i] - p[i]) && p[i]; i++ )
;
return
q[i]-p[i];
}
inline
void
exch
(
unsigned
char
*strings[], Lcp lcps[],
int
I,
int
J) {
std::swap
(strings[I],strings[J]);
std::swap
(lcps[I],lcps[J]);
}
void
strsort
(
unsigned
char
* strings[], Lcp lcps[],
int
lo,
int
hi );
template
<
bool
ascending>
void
lcpsort
(
unsigned
char
* strings[], Lcp lcps[],
int
lo,
int
hi ) {
if
( hi <= lo )
return
;
int
lt = lo, gt = hi;
Lcp pivot = lcps[lo];
for
(
int
i = lo +
1
; i <= gt; ) {
if
( ascending ? lcps[i] > pivot : lcps[i] < pivot )
exch
( strings, lcps, i, gt--);
else
if
( ascending ? lcps[i] < pivot : lcps[i] > pivot )
exch
( strings, lcps, lt++, i++);
else
i++;
}
strsort
( strings, lcps, lt, gt );
lcpsort<ascending>( strings, lcps, lo, lt-
1
);
lcpsort<ascending>( strings, lcps, gt+
1
, hi );
};
void
strsort
(
unsigned
char
* strings[], Lcp lcps[],
int
lo,
int
hi )
{
if
( hi <= lo )
return
;
int
lt = lo, gt = hi;
unsigned
char
* pivotStr = strings[lo];
for
(
int
i = lo +
1
; i <= gt; )
{
int
cmpr =
lcpstrcmp
( pivotStr, strings[i], lcps[i] );
if
(cmpr <
0
)
exch
( strings, lcps, lt++, i++);
else
if
(cmpr >
0
)
exch
( strings, lcps, i, gt--);
else
i++;
}
lcpsort<
true
> ( strings, lcps, lo, lt-
1
);
lcpsort<
false
>( strings, lcps, gt+
1
, hi );
};
extern
"
C
"
void
lcpquicksort
(
unsigned
char
* strings[],
size_t
n ) {
Lcp *lcps = (Lcp *)
calloc
( n,
sizeof
(Lcp));
strsort
( strings, lcps,
0
, n-
1
);
free
(lcps);
}
ROUTINE_REGISTER_SINGLECORE
( lcpquicksort,
"
LCP Quicksort by Kendall Willets
"
)
Back
|
FazBrowse Home
|
New Git URL