Skip to content

Commit

Permalink
use hashing for classify and deduplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Dec 22, 2023
1 parent 80033b7 commit c0ef682
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/algorithm/monadic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::{
cmp::Ordering,
collections::{BTreeMap, BTreeSet, HashMap},
collections::{HashMap, HashSet},
f64::consts::{PI, TAU},
iter::repeat,
mem::size_of,
Expand Down Expand Up @@ -636,11 +636,11 @@ impl<T: ArrayValue> Array<T> {
if self.rank() == 0 {
return Err(env.error("Cannot classify a rank-0 array"));
}
let mut classes = BTreeMap::new();
let mut classes = HashMap::new();
let mut classified = Vec::with_capacity(self.row_count());
for row in self.rows() {
for row in self.row_slices() {
let new_class = classes.len();
let class = *classes.entry(row).or_insert(new_class);
let class = *classes.entry(ArrayCmpSlice(row)).or_insert(new_class);
classified.push(class);
}
Ok(classified)
Expand All @@ -651,7 +651,7 @@ impl<T: ArrayValue> Array<T> {
return;
}
let mut deduped = CowSlice::new();
let mut seen = BTreeSet::new();
let mut seen = HashSet::new();
let mut new_len = 0;
for row in self.row_slices() {
if seen.insert(ArrayCmpSlice(row)) {
Expand Down
11 changes: 11 additions & 0 deletions tests/units.ua
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ ParseOrZero ← ⍣⋕⋅⋅0
⍤⊃⋅∘≍ 1 ⊗ 5 [1 5 5]
⍤⊃⋅∘≍ [1] ⊗ [5] [1 5 5]

# Classify
⍤⊃⋅∘≍ [0 1 2 3 4] ⊛ [2 9 4 8 3]
⍤⊃⋅∘≍ [0 1 2 3 4] ⊛ ↯5_2⇡10
⍤⊃⋅∘≍ [0 1 0 1 0] ⊛ ↯5_2⇡4

# Deduplicate
⍤⊃⋅∘≍ [1 2 3 4 5] ⊝ [1 2 3 4 5]
⍤⊃⋅∘≍ [1 2 3 4 5] ⊝ [1 2 3 4 5 5 5 5 5 5]
⍤⊃⋅∘≍ [4 8 2 9 1 3] ⊝ [4 8 2 9 1 8 3 9 4 9 2 8]
⍤⊃⋅∘≍ [1_2 3_4 5_6] ⊝ [1_2 3_4 5_6 3_4]

# Bits
⍤⊃⋅∘≍ ⍜⋯∘ .5
⍤⊃⋅∘≍ ⍜⋯∘ .15
Expand Down

0 comments on commit c0ef682

Please sign in to comment.