Skip to content

Commit

Permalink
Rename OklabCounts::iter to pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivordir committed May 19, 2023
1 parent 6e3213c commit a529e75
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions okolors/src/kmeans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn kmeans_plus_plus<D: ColorDifference>(

/// Initializes the center sums and counts based off the initial centroids
fn compute_initial_sums(oklab: &OklabCounts, centers: &mut CenterData, assignment: &[u8]) {
for ((color, n), &center) in oklab.iter().zip(assignment) {
for ((color, n), &center) in oklab.pairs().zip(assignment) {
let i = usize::from(center);
let nf = f64::from(n);
let sum = &mut centers.sum[i];
Expand Down Expand Up @@ -211,7 +211,7 @@ fn update_assignments<D: ColorDifference>(
points: &mut PointData,
) {
let k = centers.centroid.len();
for ((color, n), center) in oklab.iter().zip(&mut points.assignment) {
for ((color, n), center) in oklab.pairs().zip(&mut points.assignment) {
let ci = usize::from(*center);
let dist = D::squared_distance(color, centers.centroid[ci]);

Expand Down Expand Up @@ -396,7 +396,7 @@ fn kmeans<D: ColorDifference>(
}

let variance = oklab
.iter()
.pairs()
.zip(&points.assignment)
.map(|((color, n), &center)| {
f64::from(n) * f64::from(D::squared_distance(color, centers.centroid[usize::from(center)]))
Expand Down Expand Up @@ -630,7 +630,7 @@ mod tests {

let mut expected_sum = Oklab { l: 0.0, a: 0.0, b: 0.0 };
let mut expected_count = 0;
for (color, count) in data.iter() {
for (color, count) in data.pairs() {
expected_count += count;
let n = f64::from(count);
expected_sum.l += n * f64::from(color.l);
Expand Down Expand Up @@ -661,7 +661,7 @@ mod tests {

update_assignments::<EuclideanDistance>(&data, &mut state.centers, &state.distances, &mut state.points);

for ((color, count), &center) in data.iter().zip(&state.points.assignment) {
for ((color, count), &center) in data.pairs().zip(&state.points.assignment) {
let center = usize::from(center);
let n = f64::from(count);
let sum = &mut state.centers.sum[center];
Expand Down
2 changes: 1 addition & 1 deletion okolors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl OklabCounts {
}

/// Returns an iterator over each `(Oklab, count: u32)` pair
pub fn iter(&self) -> impl Iterator<Item = (Oklab, u32)> + '_ {
pub fn pairs(&self) -> impl Iterator<Item = (Oklab, u32)> + '_ {
self.colors.iter().copied().zip(self.counts.iter().copied())
}

Expand Down

0 comments on commit a529e75

Please sign in to comment.