Skip to content

Commit 3d61a3b

Browse files
committed
fix a bug with sorting
1 parent b318f76 commit 3d61a3b

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/algorithm/invert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ fn invert_temp_pattern<'a>(
13591359
if let Some((input, start_instr @ Instr::CopyToTemp { span, .. }, inner, end_instr, count)) =
13601360
try_copy_temp_wrap(input)
13611361
{
1362-
// Pseudo-inverse
1362+
// On-inverse
13631363
for mid in 0..inner.len() {
13641364
let (before, after) = inner.split_at(mid);
13651365
let Ok(before_sig) = instrs_signature(before) else {

src/algorithm/monadic.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -778,27 +778,29 @@ impl<T: ArrayValue> Array<T> {
778778
return;
779779
}
780780
let chunk_len: usize = self.shape[depth..].iter().product();
781-
if chunk_len == 0 {
781+
let subrow_len: usize = self.shape[depth + 1..].iter().product();
782+
if chunk_len == 0 || subrow_len == 0 {
782783
return;
783784
}
784785
let is_list = self.rank() == depth + 1;
785786
let mut new_chunk = Vec::with_capacity(chunk_len);
787+
let mut indices = Vec::with_capacity(chunk_len / subrow_len);
786788
for chunk in self.data.as_mut_slice().chunks_exact_mut(chunk_len) {
787789
if is_list {
788790
chunk.par_sort_by(T::array_cmp);
789791
} else {
790-
let mut indices: Vec<usize> = (0..chunk.len() / chunk_len).collect();
792+
indices.extend(0..chunk.len() / subrow_len);
791793
indices.par_sort_by(|&a, &b| {
792-
chunk[a * chunk_len..(a + 1) * chunk_len]
794+
chunk[a * subrow_len..(a + 1) * subrow_len]
793795
.iter()
794-
.zip(&chunk[b * chunk_len..(b + 1) * chunk_len])
796+
.zip(&chunk[b * subrow_len..(b + 1) * subrow_len])
795797
.map(|(a, b)| a.array_cmp(b))
796798
.find(|x| x != &Ordering::Equal)
797799
.unwrap_or(Ordering::Equal)
798800
});
799801
new_chunk.clear();
800-
for i in indices {
801-
new_chunk.extend_from_slice(&chunk[i * chunk_len..(i + 1) * chunk_len]);
802+
for i in indices.drain(..) {
803+
new_chunk.extend_from_slice(&chunk[i * subrow_len..(i + 1) * subrow_len]);
802804
}
803805
chunk.clone_from_slice(&new_chunk);
804806
}
@@ -810,27 +812,29 @@ impl<T: ArrayValue> Array<T> {
810812
return;
811813
}
812814
let chunk_len: usize = self.shape[depth..].iter().product();
813-
if chunk_len == 0 {
815+
let subrow_len: usize = self.shape[depth + 1..].iter().product();
816+
if chunk_len == 0 || subrow_len == 0 {
814817
return;
815818
}
816819
let is_list = self.rank() == depth + 1;
817820
let mut new_chunk = Vec::with_capacity(chunk_len);
821+
let mut indices = Vec::with_capacity(chunk_len / subrow_len);
818822
for chunk in self.data.as_mut_slice().chunks_exact_mut(chunk_len) {
819823
if is_list {
820824
chunk.par_sort_by(|a, b| b.array_cmp(a));
821825
} else {
822-
let mut indices: Vec<usize> = (0..chunk.len() / chunk_len).collect();
826+
indices.extend(0..chunk.len() / subrow_len);
823827
indices.par_sort_by(|&a, &b| {
824-
chunk[a * chunk_len..(a + 1) * chunk_len]
828+
chunk[a * subrow_len..(a + 1) * subrow_len]
825829
.iter()
826-
.zip(&chunk[b * chunk_len..(b + 1) * chunk_len])
830+
.zip(&chunk[b * subrow_len..(b + 1) * subrow_len])
827831
.map(|(a, b)| b.array_cmp(a))
828832
.find(|x| x != &Ordering::Equal)
829833
.unwrap_or(Ordering::Equal)
830834
});
831835
new_chunk.clear();
832-
for i in indices {
833-
new_chunk.extend_from_slice(&chunk[i * chunk_len..(i + 1) * chunk_len]);
836+
for i in indices.drain(..) {
837+
new_chunk.extend_from_slice(&chunk[i * subrow_len..(i + 1) * subrow_len]);
834838
}
835839
chunk.clone_from_slice(&new_chunk);
836840
}

tests/optimized.ua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@
3131
⍤⟜≍: ⟜(≡⊂≡°⊂)↯3_3_3⇡27
3232
⍤⟜≍: ⟜(≡≡⊂≡≡°⊂)↯3_3_3⇡27
3333

34-
# Rows sort
34+
# Sort
35+
⍤⟜≍: ⇌ ⟜(⊏⍏.) ↯3_3⇌⇡9
36+
⍤⟜≍: ⇌ ⟜(⊏⍖.) ↯3_3⇌9
3537
⍤⟜≍: ⇌↯3_3⇡9 ≡(⊏⍏.) ↯3_3⇌⇡9
38+
⍤⟜≍: ≡⇌ ⟜≡(⊏⍖.) ↯3_3⇡9
3639

3740
# Adjacent
3841
⍤⟜≍: [3 5 7] ≡/+◫2 [1 2 3 4]

0 commit comments

Comments
 (0)