Skip to content

Commit a32d2bb

Browse files
authored
refactor!: update float genericity code (#68)
* refactor: replace manual CoordsFloat impl by a generic one * refactor: remove FloatType from examples & doc * refactor: remove pub reexport of FloatType * refactor: add _ to single_precision feat name also, add FloatType def in benches' lib.rs * refactor: update benches import * ci: update feature name * doc: remove mention of single_precrision in core doc * chore: update changelog
1 parent 6ff37b3 commit a32d2bb

File tree

26 files changed

+93
-57
lines changed

26 files changed

+93
-57
lines changed

.github/workflows/rust-test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
strategy:
2525
matrix:
2626
features:
27-
- single_precision
27+
- _single_precision
2828
- utils
2929
runs-on: ubuntu-22.04
3030
steps:
@@ -39,7 +39,7 @@ jobs:
3939
strategy:
4040
matrix:
4141
features:
42-
- single_precision
42+
- _single_precision
4343
- utils
4444
runs-on: ubuntu-22.04
4545
steps:

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
<sup>core definitions and tools for combinatorial map implementation</sup>
1616

17+
- replace `CoordsFloat` implementation blocks to automatize implementation
18+
for all types that fit the traits requirement (#68)
19+
- remove `FloatType` from the public API (#68)
1720
- change some main methods return type to have better overall consistency (#67):
1821
- `Vector2::normal_dir` now returns a result instead of potentially panicking
1922
- `CMap2::vertex` now returns a result instead of panicking if no associated vertex is found

honeycomb-benches/Cargo.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ description = "Core structure benchmarks"
99
authors.workspace = true
1010
publish = false
1111

12+
[features]
13+
_single_precision = []
14+
15+
# deps
16+
17+
[dependencies]
18+
cfg-if.workspace = true
19+
1220
[dev-dependencies]
1321
honeycomb-core = { workspace = true, features = ["utils"] }
1422
criterion = { workspace = true, features = ["html_reports"] }
1523
iai-callgrind.workspace = true
1624
rand = { workspace = true, features = ["small_rng"] }
1725

18-
# Iai-callgrind benchmarks
26+
# benches
27+
28+
## Iai-callgrind benchmarks
1929

2030
[[bench]]
2131
name = "prof-cmap2-editing"
@@ -33,7 +43,7 @@ path = "benches/core/cmap2/sewing.rs"
3343
harness = false
3444

3545

36-
# Criterion benchmarks
46+
## Criterion benchmarks
3747

3848
[[bench]]
3949
name = "squaremap-init"

honeycomb-benches/benches/core/cmap2/editing.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
1515
// ------ IMPORTS
1616

17-
use honeycomb_core::{utils::GridBuilder, CMap2, DartIdentifier, FloatType, Vertex2};
17+
use honeycomb_benches::FloatType;
18+
use honeycomb_core::{utils::GridBuilder, CMap2, DartIdentifier, Vertex2};
1819
use iai_callgrind::{
1920
library_benchmark, library_benchmark_group, main, FlamegraphConfig, LibraryBenchmarkConfig,
2021
};

honeycomb-benches/benches/core/cmap2/reading.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
1515
// ------ IMPORTS
1616

17-
use honeycomb_core::{utils::GridBuilder, CMap2, DartIdentifier, FloatType};
17+
use honeycomb_benches::FloatType;
18+
use honeycomb_core::{utils::GridBuilder, CMap2, DartIdentifier};
1819
use iai_callgrind::{
1920
library_benchmark, library_benchmark_group, main, FlamegraphConfig, LibraryBenchmarkConfig,
2021
};

honeycomb-benches/benches/core/cmap2/sewing.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
1616
// ------ IMPORTS
1717

18-
use honeycomb_core::{utils::GridBuilder, CMap2, FloatType};
18+
use honeycomb_benches::FloatType;
19+
use honeycomb_core::{utils::GridBuilder, CMap2};
1920
use iai_callgrind::{
2021
library_benchmark, library_benchmark_group, main, FlamegraphConfig, LibraryBenchmarkConfig,
2122
};

honeycomb-benches/benches/splitsquaremap/init.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use criterion::{
99
black_box, criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion,
1010
PlotConfiguration, Throughput,
1111
};
12-
use honeycomb_core::{utils::GridBuilder, CMap2, FloatType};
12+
use honeycomb_benches::FloatType;
13+
use honeycomb_core::{utils::GridBuilder, CMap2};
1314

1415
// ------ CONTENT
1516

honeycomb-benches/benches/splitsquaremap/shift.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ use rand::{
2222
rngs::SmallRng,
2323
};
2424

25+
use honeycomb_benches::FloatType;
2526
use honeycomb_core::{
26-
utils::GridBuilder, CMap2, DartIdentifier, FloatType, Vector2, VertexIdentifier, NULL_DART_ID,
27+
utils::GridBuilder, CMap2, DartIdentifier, Vector2, VertexIdentifier, NULL_DART_ID,
2728
};
2829

2930
// ------ CONTENT

honeycomb-benches/benches/squaremap/init.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use criterion::{
99
black_box, criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion,
1010
PlotConfiguration, Throughput,
1111
};
12-
use honeycomb_core::{utils::GridBuilder, CMap2, FloatType};
12+
use honeycomb_benches::FloatType;
13+
use honeycomb_core::{utils::GridBuilder, CMap2};
1314

1415
// ------ CONTENT
1516

honeycomb-benches/benches/squaremap/shift.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ use rand::{
2222
rngs::SmallRng,
2323
};
2424

25+
use honeycomb_benches::FloatType;
2526
use honeycomb_core::{
26-
utils::GridBuilder, CMap2, DartIdentifier, FloatType, Vector2, VertexIdentifier, NULL_DART_ID,
27+
utils::GridBuilder, CMap2, DartIdentifier, Vector2, VertexIdentifier, NULL_DART_ID,
2728
};
2829

2930
// ------ CONTENT

honeycomb-benches/benches/squaremap/split.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
// ------ IMPORTS
2020

2121
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
22-
use honeycomb_core::{utils::GridBuilder, CMap2, DartIdentifier, FloatType};
22+
use honeycomb_benches::FloatType;
23+
use honeycomb_core::{utils::GridBuilder, CMap2, DartIdentifier};
2324
use rand::{
2425
distributions::{Bernoulli, Distribution},
2526
rngs::SmallRng,

honeycomb-benches/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@
1919
//! - `prof-cmap2-editing` - `CMap2` editing methods benchmarks
2020
//! - `prof-cmap2-reading` - `CMap2` reading methods benchmarks
2121
//! - `prof-cmap2-sewing-unsewing` - `CMap2` (un)sewing methods benchmarks
22+
23+
cfg_if::cfg_if! {
24+
if #[cfg(feature = "_single_precision")] {
25+
/// Floating-point type alias.
26+
///
27+
/// This is mostly used to run tests using both `f64` and `f32`.
28+
pub type FloatType = f32;
29+
} else {
30+
/// Floating-point type alias.
31+
///
32+
/// This is mostly used to run tests using both `f64` and `f32`.
33+
pub type FloatType = f64;
34+
}
35+
}

honeycomb-core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ publish = true
1414

1515
[features]
1616
utils = []
17-
single_precision = []
17+
_single_precision = []
18+
19+
# deps
1820

1921
[dependencies]
2022
cfg-if.workspace = true

honeycomb-core/src/cells/orbits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl<'a, T: CoordsFloat> Iterator for Orbit2<'a, T> {
219219

220220
#[cfg(test)]
221221
mod tests {
222-
use crate::{CMap2, DartIdentifier, FloatType, Orbit2, OrbitPolicy};
222+
use crate::{common::FloatType, CMap2, DartIdentifier, Orbit2, OrbitPolicy};
223223

224224
fn simple_map() -> CMap2<FloatType> {
225225
let mut map: CMap2<FloatType> = CMap2::new(6);

honeycomb-core/src/cmap2/structure.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ use std::collections::BTreeSet;
6565
/// # use honeycomb_core::CMapError;
6666
/// # fn main() -> Result<(), CMapError> {
6767
///
68-
/// use honeycomb_core::{CMap2, FloatType, Orbit2, OrbitPolicy, Vertex2};
68+
/// use honeycomb_core::{CMap2, Orbit2, OrbitPolicy, Vertex2};
6969
///
7070
/// // build a triangle
71-
/// let mut map: CMap2<FloatType> = CMap2::new(3); // three darts
71+
/// let mut map: CMap2<f64> = CMap2::new(3); // three darts
7272
/// map.one_link(1, 2); // beta1(1) = 2 & beta0(2) = 1
7373
/// map.one_link(2, 3); // beta1(2) = 3 & beta0(3) = 2
7474
/// map.one_link(3, 1); // beta1(3) = 1 & beta0(1) = 3

honeycomb-core/src/cmap2/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ------ IMPORTS
22

3-
use crate::{CMap2, FloatType, Orbit2, OrbitPolicy, Vertex2};
3+
use crate::{common::FloatType, CMap2, Orbit2, OrbitPolicy, Vertex2};
44

55
// ------ CONTENT
66

honeycomb-core/src/common.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,21 @@ pub enum CMapError {
2828
UndefinedVertex,
2929
}
3030

31-
// --- decimal types
31+
// --- generic decimal trait
3232

33+
/// Common trait implemented by types used for coordinate representation.
34+
pub trait CoordsFloat:
35+
num::Float + Default + AddAssign + SubAssign + MulAssign + DivAssign
36+
{
37+
}
38+
39+
impl<T: num::Float + Default + AddAssign + SubAssign + MulAssign + DivAssign> CoordsFloat for T {}
40+
41+
// --- test utility
42+
43+
#[cfg(test)]
3344
cfg_if::cfg_if! {
34-
if #[cfg(feature = "single_precision")] {
45+
if #[cfg(feature = "_single_precision")] {
3546
/// Floating-point type alias.
3647
///
3748
/// This is mostly used to run tests using both `f64` and `f32`.
@@ -43,12 +54,3 @@ cfg_if::cfg_if! {
4354
pub type FloatType = f64;
4455
}
4556
}
46-
47-
/// Common trait implemented by types used for coordinate representation.
48-
pub trait CoordsFloat:
49-
num::Float + Default + AddAssign + SubAssign + MulAssign + DivAssign
50-
{
51-
}
52-
53-
impl CoordsFloat for f32 {}
54-
impl CoordsFloat for f64 {}

honeycomb-core/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//! Optional features can be enabled when compiling this crate:
1515
//!
1616
//! - `utils` -- provides additionnal implementations for map generation, benchmarking & debugging
17-
//! - `single_precision` -- uses `f32` instead of `f64` for coordinates representation in tests
1817
1918
// ------ CUSTOM LINTS
2019

@@ -40,6 +39,8 @@ pub mod utils;
4039

4140
// ------ RE-EXPORTS
4241

42+
// --- PUBLIC API
43+
4344
pub use attributes::{
4445
collections::{AttrCompactVec, AttrSparseVec},
4546
traits::{AttributeBind, AttributeUpdate},
@@ -53,7 +54,7 @@ pub use cells::{
5354
orbits::{Orbit2, OrbitPolicy},
5455
};
5556
pub use cmap2::CMap2;
56-
pub use common::{CMapError, CoordsFloat, DartIdentifier, FloatType, NULL_DART_ID};
57+
pub use common::{CMapError, CoordsFloat, DartIdentifier, NULL_DART_ID};
5758
pub use spatial_repr::{
5859
coords::{Coords2, CoordsError},
5960
vector::Vector2,

honeycomb-core/src/spatial_repr/coords.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
77
// ------ IMPORTS
88

9-
#[cfg(doc)]
10-
use crate::FloatType;
119
use crate::{CoordsFloat, Vector2, Vertex2};
1210

1311
use std::iter::Sum;
@@ -26,10 +24,7 @@ pub enum CoordsError {
2624
InvalidUnitDir,
2725
}
2826

29-
/// 2-dimensional coordinates structure
30-
///
31-
/// The floating type used for coordinate representation is determined by the user. For tests, it
32-
/// can be controled using features and the [`FloatType`] alias.
27+
/// Bare-bone 2-dimensional coordinates representation
3328
///
3429
/// # Generics
3530
///
@@ -220,7 +215,7 @@ impl<T: CoordsFloat> IndexMut<usize> for Coords2<T> {
220215
#[cfg(test)]
221216
mod tests {
222217
use super::*;
223-
use crate::FloatType;
218+
use crate::common::FloatType;
224219

225220
fn almost_equal(lhs: &Coords2<FloatType>, rhs: &Coords2<FloatType>) -> bool {
226221
const EPS: FloatType = 10.0e-12;

honeycomb-core/src/spatial_repr/vector.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ use crate::{Coords2, CoordsError, CoordsFloat};
2323
/// ```
2424
/// # use honeycomb_core::CoordsError;
2525
/// # fn main() -> Result<(), CoordsError> {
26-
/// use honeycomb_core::{Vector2, FloatType};
26+
/// use honeycomb_core::Vector2;
2727
///
2828
/// let unit_x = Vector2::unit_x();
2929
/// let unit_y = Vector2::unit_y();
3030
///
3131
/// assert_eq!(unit_x.dot(&unit_y), 0.0);
3232
/// assert_eq!(unit_x.normal_dir().unwrap(), unit_y);
3333
///
34-
/// let two: FloatType = 2.0;
35-
/// let x_plus_y: Vector2<FloatType> = unit_x + unit_y;
34+
/// let two: f64 = 2.0;
35+
/// let x_plus_y: Vector2<f64> = unit_x + unit_y;
3636
///
3737
/// assert_eq!(x_plus_y.norm(), two.sqrt());
3838
/// assert_eq!(x_plus_y.unit_dir()?, Vector2::from((1.0 / two.sqrt(), 1.0 / two.sqrt())));
@@ -291,7 +291,7 @@ impl<T: CoordsFloat> std::ops::Neg for Vector2<T> {
291291
#[cfg(test)]
292292
mod tests {
293293
use super::*;
294-
use crate::FloatType;
294+
use crate::common::FloatType;
295295

296296
macro_rules! almost_equal {
297297
($f1: expr, $f2: expr) => {

honeycomb-core/src/spatial_repr/vertex.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ use crate::{
2525
/// ```
2626
/// # use honeycomb_core::CoordsError;
2727
/// # fn main() -> Result<(), CoordsError> {
28-
/// use honeycomb_core::{Vector2, Vertex2, FloatType};
28+
/// use honeycomb_core::{Vector2, Vertex2};
2929
///
3030
/// let v1 = Vertex2::from((1.0, 0.0));
3131
/// let v2 = Vertex2::from((1.0, 1.0));
3232
///
3333
/// assert_eq!(v1.x(), 1.0);
3434
/// assert_eq!(v1.y(), 0.0);
3535
///
36-
/// let two: FloatType = 2.0;
36+
/// let two: f64 = 2.0;
3737
/// // vectorAB = vertexB - vertexA
38-
/// let v2_minus_v1: Vector2<FloatType> = v2 - v1;
38+
/// let v2_minus_v1: Vector2<f64> = v2 - v1;
3939
///
4040
/// assert_eq!(v2_minus_v1.norm(), 1.0);
4141
/// assert_eq!(v2_minus_v1.unit_dir()?, Vector2::unit_y());
@@ -255,7 +255,7 @@ impl<T: CoordsFloat> AttributeBind for Vertex2<T> {
255255
#[cfg(test)]
256256
mod tests {
257257
use super::*;
258-
use crate::FloatType;
258+
use crate::common::FloatType;
259259

260260
#[test]
261261
fn add_vertex_vector() {

honeycomb-core/src/utils/generation.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ impl<T: CoordsFloat> GridBuilder<T> {
512512
#[cfg(test)]
513513
mod tests {
514514
use super::*;
515+
use crate::common::FloatType;
515516

516517
#[test]
517518
fn build_nc_lpc_l() {
@@ -603,7 +604,7 @@ mod tests {
603604

604605
#[test]
605606
fn square_cmap2_correctness() {
606-
let cmap: CMap2<f64> = GridBuilder::unit_squares(2).build2().unwrap();
607+
let cmap: CMap2<FloatType> = GridBuilder::unit_squares(2).build2().unwrap();
607608

608609
// hardcoded because using a generic loop & dim would just mean
609610
// reusing the same pattern as the one used during construction
@@ -704,7 +705,7 @@ mod tests {
704705
#[allow(clippy::too_many_lines)]
705706
#[test]
706707
fn splitsquare_cmap2_correctness() {
707-
let cmap: CMap2<f64> = GridBuilder::split_unit_squares(2).build2().unwrap();
708+
let cmap: CMap2<FloatType> = GridBuilder::split_unit_squares(2).build2().unwrap();
708709

709710
// hardcoded because using a generic loop & dim would just mean
710711
// reusing the same pattern as the one used during construction

honeycomb-examples/examples/memory_usage/compute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use honeycomb_core::{utils::GridBuilder, CMap2, DartIdentifier, FloatType};
1+
use honeycomb_core::{utils::GridBuilder, CMap2, DartIdentifier};
22

33
pub fn main() {
44
// create a 3x3 grid & remove the central square
5-
let mut cmap: CMap2<FloatType> = GridBuilder::unit_squares(3).build2().unwrap();
5+
let mut cmap: CMap2<f64> = GridBuilder::unit_squares(3).build2().unwrap();
66
// darts making up the central square
77
let (d1, d2, d3, d4): (
88
DartIdentifier,

0 commit comments

Comments
 (0)