-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathsort.rs
253 lines (227 loc) · 8.07 KB
/
sort.rs
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
static USAGE: &str = r#"
Sorts CSV data in alphabetical (with case-insensitive option), numerical,
reverse, unique or random (with optional seed) order.
The sort is done in lexicographical order.
https://en.wikipedia.org/wiki/Lexicographic_order
Note that this requires reading all of the CSV data into memory. If
you need to sort a large file that may not fit into memory, use the
extsort command instead.
For examples, see https://github.com/jqnatividad/qsv/blob/master/tests/test_sort.rs.
Usage:
qsv sort [options] [<input>]
qsv sort --help
sort options:
-s, --select <arg> Select a subset of columns to sort.
See 'qsv select --help' for the format details.
-N, --numeric Compare according to string numerical value
-R, --reverse Reverse order
--random Random order
--seed <number> Random number generator seed to use if --random is set
-i, --ignore-case Compare strings disregarding case
-u, --unique When set, identical consecutive lines will be dropped
to keep only one line per sorted value.
-j, --jobs <arg> The number of jobs to run in parallel.
When not set, the number of jobs is set to the
number of CPUs detected.
Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
-n, --no-headers When set, the first row will not be interpreted
as headers. Namely, it will be sorted with the rest
of the rows. Otherwise, the first row will always
appear as the header row in the output.
-d, --delimiter <arg> The field delimiter for reading CSV data.
Must be a single character. (default: ,)
--no-memcheck Do not check if there is enough memory to load the
entire CSV into memory.
"#;
use std::cmp;
use rand::{rngs::StdRng, seq::SliceRandom, SeedableRng};
use rayon::slice::ParallelSliceMut;
use serde::Deserialize;
use simdutf8::basic::from_utf8;
use self::Number::{Float, Int};
use crate::{
cmd::dedup::iter_cmp_ignore_case,
config::{Config, Delimiter},
select::SelectColumns,
util, CliResult,
};
#[derive(Deserialize)]
struct Args {
arg_input: Option<String>,
flag_select: SelectColumns,
flag_numeric: bool,
flag_reverse: bool,
flag_random: bool,
flag_seed: Option<u64>,
flag_ignore_case: bool,
flag_jobs: Option<usize>,
flag_output: Option<String>,
flag_no_headers: bool,
flag_delimiter: Option<Delimiter>,
flag_unique: bool,
flag_no_memcheck: bool,
}
pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let numeric = args.flag_numeric;
let reverse = args.flag_reverse;
let random = args.flag_random;
let rconfig = Config::new(&args.arg_input)
.delimiter(args.flag_delimiter)
.no_headers(args.flag_no_headers)
.select(args.flag_select);
// we're loading the entire file into memory, we need to check avail mem
if let Some(path) = rconfig.path.clone() {
util::mem_file_check(&path, false, args.flag_no_memcheck)?;
}
let mut rdr = rconfig.reader()?;
let headers = rdr.byte_headers()?.clone();
let sel = rconfig.selection(&headers)?;
// set RAYON_NUM_THREADS
util::njobs(args.flag_jobs);
// Seeding RNG
let seed = args.flag_seed;
let ignore_case = args.flag_ignore_case;
let mut all = rdr.byte_records().collect::<Result<Vec<_>, _>>()?;
match (numeric, reverse, random) {
(_, _, true) => {
// we don't need cryptographically strong RNGs for this
// add DevSkim lint ignores to suppress warning
if let Some(val) = seed {
let mut rng = StdRng::seed_from_u64(val); //DevSkim: ignore DS148264
SliceRandom::shuffle(&mut *all, &mut rng); //DevSkim: ignore DS148264
} else {
let mut rng = ::rand::thread_rng();
SliceRandom::shuffle(&mut *all, &mut rng); //DevSkim: ignore DS148264
}
}
(false, false, false) => all.par_sort_by(|r1, r2| {
let a = sel.select(r1);
let b = sel.select(r2);
if ignore_case {
iter_cmp_ignore_case(a, b)
} else {
iter_cmp(a, b)
}
}),
(true, false, false) => all.par_sort_by(|r1, r2| {
let a = sel.select(r1);
let b = sel.select(r2);
iter_cmp_num(a, b)
}),
(false, true, false) => all.par_sort_by(|r1, r2| {
let a = sel.select(r1);
let b = sel.select(r2);
if ignore_case {
iter_cmp_ignore_case(b, a)
} else {
iter_cmp(b, a)
}
}),
(true, true, false) => all.par_sort_by(|r1, r2| {
let a = sel.select(r1);
let b = sel.select(r2);
iter_cmp_num(b, a)
}),
}
let mut wtr = Config::new(&args.flag_output).writer()?;
let mut prev: Option<csv::ByteRecord> = None;
rconfig.write_headers(&mut rdr, &mut wtr)?;
for r in all {
if args.flag_unique {
match prev {
Some(other_r) => match iter_cmp(sel.select(&r), sel.select(&other_r)) {
cmp::Ordering::Equal => (),
_ => {
wtr.write_byte_record(&r)?;
}
},
None => {
wtr.write_byte_record(&r)?;
}
}
prev = Some(r);
} else {
wtr.write_byte_record(&r)?;
}
}
Ok(wtr.flush()?)
}
/// Order `a` and `b` lexicographically using `Ord`
#[inline]
pub fn iter_cmp<A, L, R>(mut a: L, mut b: R) -> cmp::Ordering
where
A: Ord,
L: Iterator<Item = A>,
R: Iterator<Item = A>,
{
loop {
match (a.next(), b.next()) {
(None, None) => return cmp::Ordering::Equal,
(None, _) => return cmp::Ordering::Less,
(_, None) => return cmp::Ordering::Greater,
(Some(x), Some(y)) => match x.cmp(&y) {
cmp::Ordering::Equal => (),
non_eq => return non_eq,
},
}
}
}
/// Try parsing `a` and `b` as numbers when ordering
#[inline]
pub fn iter_cmp_num<'a, L, R>(mut a: L, mut b: R) -> cmp::Ordering
where
L: Iterator<Item = &'a [u8]>,
R: Iterator<Item = &'a [u8]>,
{
loop {
match (next_num(&mut a), next_num(&mut b)) {
(None, None) => return cmp::Ordering::Equal,
(None, _) => return cmp::Ordering::Less,
(_, None) => return cmp::Ordering::Greater,
(Some(x), Some(y)) => match compare_num(x, y) {
cmp::Ordering::Equal => (),
non_eq => return non_eq,
},
}
}
}
#[derive(Clone, Copy, PartialEq)]
enum Number {
Int(i64),
Float(f64),
}
#[inline]
fn compare_num(n1: Number, n2: Number) -> cmp::Ordering {
match (n1, n2) {
(Int(i1), Int(i2)) => i1.cmp(&i2),
#[allow(clippy::cast_precision_loss)]
(Int(i1), Float(f2)) => compare_float(i1 as f64, f2),
#[allow(clippy::cast_precision_loss)]
(Float(f1), Int(i2)) => compare_float(f1, i2 as f64),
(Float(f1), Float(f2)) => compare_float(f1, f2),
}
}
#[inline]
fn compare_float(f1: f64, f2: f64) -> cmp::Ordering {
f1.partial_cmp(&f2).unwrap_or(cmp::Ordering::Equal)
}
#[inline]
fn next_num<'a, X>(xs: &mut X) -> Option<Number>
where
X: Iterator<Item = &'a [u8]>,
{
xs.next()
.map(|bytes| from_utf8(bytes).unwrap())
.and_then(|s| {
if let Ok(i) = s.parse::<i64>() {
Some(Number::Int(i))
} else if let Ok(f) = s.parse::<f64>() {
Some(Number::Float(f))
} else {
None
}
})
}