flog-rs 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Cargo.lock +486 -0
- data/Cargo.toml +22 -0
- data/DIVERGENCES.md +14 -0
- data/LICENSE +22 -0
- data/README.md +100 -0
- data/THIRD_PARTY_NOTICES.md +45 -0
- data/crates/flog-cli/Cargo.toml +23 -0
- data/crates/flog-cli/src/main.rs +224 -0
- data/crates/flog-core/Cargo.toml +18 -0
- data/crates/flog-core/src/attach_comments.rs +271 -0
- data/crates/flog-core/src/lib.rs +307 -0
- data/crates/flog-core/src/literal.rs +217 -0
- data/crates/flog-core/src/ruby_format.rs +131 -0
- data/crates/flog-core/src/ruby_sort.rs +192 -0
- data/crates/flog-core/src/scorer.rs +1851 -0
- data/crates/flog-core/src/scores.rs +96 -0
- data/crates/flog-core/src/syntax_error.rs +145 -0
- data/crates/flog-core/src/translation.rs +487 -0
- data/ext/flog_rs/Cargo.toml +18 -0
- data/ext/flog_rs/extconf.rb +6 -0
- data/ext/flog_rs/src/flog.rs +493 -0
- data/ext/flog_rs/src/lib.rs +39 -0
- data/lib/flog_rs/version.rb +8 -0
- data/lib/flog_rs.rb +16 -0
- metadata +86 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
//! The order Ruby's `Enumerable#sort_by` gives. `sort_by` is not stable, so
|
|
2
|
+
//! elements with equal keys come out in the order Ruby's algorithm leaves
|
|
3
|
+
//! them. When every key is a Float or an Integer, `sort_by` uses its own
|
|
4
|
+
//! introsort (`rb_uniform_intro_sort_2` in `enum.c`), the same in 3.3, 3.4
|
|
5
|
+
//! and 4.0. [`sort_by_numeric_keys`] ports it.
|
|
6
|
+
|
|
7
|
+
/// One `sort_by` entry: the key and the element's index.
|
|
8
|
+
type Entry = (f64, usize);
|
|
9
|
+
|
|
10
|
+
/// The indexes of `keys` in the order `sort_by` yields the elements, for
|
|
11
|
+
/// keys that are all Floats or Integers (given here as `f64`).
|
|
12
|
+
pub fn sort_by_numeric_keys(keys: &[f64]) -> Vec<usize> {
|
|
13
|
+
let mut entries: Vec<Entry> = keys.iter().copied().zip(0..).collect();
|
|
14
|
+
// `if (RARRAY_LEN(ary) > 2)`: the buffer holds two VALUEs per element.
|
|
15
|
+
if entries.len() > 1 {
|
|
16
|
+
uniform_intro_sort(&mut entries);
|
|
17
|
+
}
|
|
18
|
+
entries.into_iter().map(|(_, index)| index).collect()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/// `rb_uniform_is_less`.
|
|
22
|
+
fn uniform_is_less(a: f64, b: f64) -> bool {
|
|
23
|
+
a < b
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/// `rb_uniform_intro_sort_2`: returns at once when the keys are already in
|
|
27
|
+
/// order, then sorts with a depth limit of twice `floor(log2(n))`.
|
|
28
|
+
fn uniform_intro_sort(entries: &mut [Entry]) {
|
|
29
|
+
let n = entries.len();
|
|
30
|
+
let depth = (usize::BITS - 1 - n.leading_zeros()) as usize;
|
|
31
|
+
let sorted = entries.windows(2).all(|pair| !uniform_is_larger(pair[0].0, pair[1].0));
|
|
32
|
+
if sorted {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
uniform_quicksort_intro(entries, 0, n, depth << 1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/// `rb_uniform_is_larger`.
|
|
39
|
+
fn uniform_is_larger(a: f64, b: f64) -> bool {
|
|
40
|
+
a > b
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/// `rb_uniform_quicksort_intro_2` on `entries[begin..end]`. The two ranges
|
|
44
|
+
/// it recurses into can share one element (equal to the pivot), so this
|
|
45
|
+
/// works on index bounds as the C code does on pointers.
|
|
46
|
+
fn uniform_quicksort_intro(entries: &mut [Entry], begin: usize, end: usize, depth: usize) {
|
|
47
|
+
if end - begin <= 16 {
|
|
48
|
+
uniform_insertion_sort(&mut entries[begin..end]);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if depth == 0 {
|
|
52
|
+
uniform_heapsort(&mut entries[begin..end]);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let pivot = median_of_three(entries[begin].0, entries[begin + ((end - begin) >> 1)].0, entries[end - 1].0);
|
|
57
|
+
let mut i = begin as isize;
|
|
58
|
+
let mut j = end as isize - 1;
|
|
59
|
+
loop {
|
|
60
|
+
while uniform_is_less(entries[i as usize].0, pivot) {
|
|
61
|
+
i += 1;
|
|
62
|
+
}
|
|
63
|
+
while uniform_is_less(pivot, entries[j as usize].0) {
|
|
64
|
+
j -= 1;
|
|
65
|
+
}
|
|
66
|
+
if i <= j {
|
|
67
|
+
entries.swap(i as usize, j as usize);
|
|
68
|
+
i += 1;
|
|
69
|
+
j -= 1;
|
|
70
|
+
}
|
|
71
|
+
if i > j {
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
let j = (j + 1) as usize;
|
|
76
|
+
let i = i as usize;
|
|
77
|
+
if end - j > 1 {
|
|
78
|
+
uniform_quicksort_intro(entries, j, end, depth - 1);
|
|
79
|
+
}
|
|
80
|
+
if i - begin > 1 {
|
|
81
|
+
uniform_quicksort_intro(entries, begin, i, depth - 1);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/// `med3_val`.
|
|
86
|
+
fn median_of_three(a: f64, b: f64, c: f64) -> f64 {
|
|
87
|
+
let less = uniform_is_less;
|
|
88
|
+
if less(a, b) {
|
|
89
|
+
if less(b, c) {
|
|
90
|
+
b
|
|
91
|
+
} else if less(c, a) {
|
|
92
|
+
a
|
|
93
|
+
} else {
|
|
94
|
+
c
|
|
95
|
+
}
|
|
96
|
+
} else if less(c, b) {
|
|
97
|
+
b
|
|
98
|
+
} else if less(a, c) {
|
|
99
|
+
a
|
|
100
|
+
} else {
|
|
101
|
+
c
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/// `rb_uniform_insertionsort_2`: an element smaller than the first goes to
|
|
106
|
+
/// the front; any other moves left past every larger element.
|
|
107
|
+
fn uniform_insertion_sort(entries: &mut [Entry]) {
|
|
108
|
+
for index in 1..entries.len() {
|
|
109
|
+
let entry = entries[index];
|
|
110
|
+
let mut j = index;
|
|
111
|
+
if uniform_is_less(entry.0, entries[0].0) {
|
|
112
|
+
entries.copy_within(0..index, 1);
|
|
113
|
+
j = 0;
|
|
114
|
+
} else {
|
|
115
|
+
while uniform_is_less(entry.0, entries[j - 1].0) {
|
|
116
|
+
entries[j] = entries[j - 1];
|
|
117
|
+
j -= 1;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
entries[j] = entry;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/// `rb_uniform_heapsort_2`.
|
|
125
|
+
fn uniform_heapsort(entries: &mut [Entry]) {
|
|
126
|
+
let n = entries.len();
|
|
127
|
+
if n < 2 {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
for offset in (0..n >> 1).rev() {
|
|
131
|
+
uniform_heap_down(entries, offset, n - 1);
|
|
132
|
+
}
|
|
133
|
+
for last in (1..n).rev() {
|
|
134
|
+
entries.swap(0, last);
|
|
135
|
+
uniform_heap_down(entries, 0, last - 1);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/// `rb_uniform_heap_down_2`: `last` is the index of the heap's last entry.
|
|
140
|
+
fn uniform_heap_down(entries: &mut [Entry], mut offset: usize, last: usize) {
|
|
141
|
+
let entry = entries[offset];
|
|
142
|
+
loop {
|
|
143
|
+
let mut child = (offset << 1) + 1;
|
|
144
|
+
if child > last {
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
if child < last && uniform_is_less(entries[child].0, entries[child + 1].0) {
|
|
148
|
+
child += 1;
|
|
149
|
+
}
|
|
150
|
+
if !uniform_is_less(entry.0, entries[child].0) {
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
entries[offset] = entries[child];
|
|
154
|
+
offset = child;
|
|
155
|
+
}
|
|
156
|
+
entries[offset] = entry;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
#[cfg(test)]
|
|
160
|
+
mod tests {
|
|
161
|
+
use super::*;
|
|
162
|
+
|
|
163
|
+
/// `(1..40).map { |i| -(i % 3).to_f }`, sorted with `sort_by` on Ruby
|
|
164
|
+
/// 3.3.14 (x86_64 Linux): the 1-based positions.
|
|
165
|
+
const KEYS_40: fn() -> Vec<f64> = || (1..=40).map(|i| -f64::from(i % 3)).collect();
|
|
166
|
+
|
|
167
|
+
fn one_based(order: Vec<usize>) -> Vec<usize> {
|
|
168
|
+
order.into_iter().map(|index| index + 1).collect()
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
#[test]
|
|
172
|
+
fn ruby_3_3_order_for_ties() {
|
|
173
|
+
let expected = [
|
|
174
|
+
20, 23, 17, 26, 14, 29, 11, 32, 8, 35, 5, 38, 2, 31, 34, 28, 37, 25, 22, 40, 1, 4, 7, 10, 13, 16, 19, 15,
|
|
175
|
+
27, 12, 30, 24, 9, 33, 18, 6, 36, 3, 39, 21,
|
|
176
|
+
];
|
|
177
|
+
assert_eq!(one_based(sort_by_numeric_keys(&KEYS_40())), expected);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
#[test]
|
|
181
|
+
fn sorted_keys_keep_their_order() {
|
|
182
|
+
assert_eq!(sort_by_numeric_keys(&[1.0, 1.0, 2.0]), vec![0, 1, 2]);
|
|
183
|
+
assert_eq!(sort_by_numeric_keys(&[]), Vec::<usize>::new());
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#[test]
|
|
187
|
+
fn deep_inputs_use_heapsort_and_stay_sorted() {
|
|
188
|
+
let keys: Vec<f64> = (0..5000).map(|i| f64::from((i * 7919) % 101)).collect();
|
|
189
|
+
let order = sort_by_numeric_keys(&keys);
|
|
190
|
+
assert!(order.windows(2).all(|pair| keys[pair[0]] <= keys[pair[1]]));
|
|
191
|
+
}
|
|
192
|
+
}
|