Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No_std with alloc. Compiles, tests pass #92

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@ autobenches = false
[lib]
bench = false

[features]
default = [ "std" ]
Stoeoef marked this conversation as resolved.
Show resolved Hide resolved
std = []

[dependencies]
smallvec = "1"
robust = "1.1.0"
num-traits = "0.2"
hashbrown = "0.14.2"

[dependencies.serde]
package = "serde"
optional = true
version = "1"
features = ["derive"]
default-features = false
features = [ "derive", "alloc" ]

[workspace]
members = ["delaunay_compare"]
Expand Down
6 changes: 3 additions & 3 deletions benches/benchmark_utilities.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::{self, Display, Formatter};
use core::fmt::{self, Display, Formatter};

use criterion::{measurement::WallTime, BenchmarkGroup, BenchmarkId, Throughput};
use rand::{distributions::uniform::SampleUniform, Rng, SeedableRng};
Expand Down Expand Up @@ -29,7 +29,7 @@ where
{
let range = rand::distributions::Uniform::new_inclusive(-range, range);
let mut rng = rand::rngs::StdRng::from_seed(seed);
std::iter::from_fn(move || Some(Point2::new(rng.sample(range), rng.sample(range))))
core::iter::from_fn(move || Some(Point2::new(rng.sample(range), rng.sample(range))))
}

pub fn uniform_f64() -> impl Iterator<Item = Point2<f64>> {
Expand All @@ -54,7 +54,7 @@ where

Some(Point2::new(last_x, last_y))
};
std::iter::from_fn(step_fn)
core::iter::from_fn(step_fn)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down
4 changes: 2 additions & 2 deletions delaunay_compare/benches/bench_utilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ where
{
let range = rand::distributions::Uniform::new_inclusive(-range, range);
let mut rng = StdRng::from_seed(seed);
std::iter::from_fn(move || Some([rng.sample(range), rng.sample(range)]))
core::iter::from_fn(move || Some([rng.sample(range), rng.sample(range)]))
}

pub fn uniform_f64() -> impl Iterator<Item = [f64; 2]> {
Expand All @@ -41,5 +41,5 @@ where

Some([last_x, last_y])
};
std::iter::from_fn(step_fn)
core::iter::from_fn(step_fn)
}
2 changes: 1 addition & 1 deletion delaunay_compare/src/spade_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type SpadePoint = spade::Point2<f64>;
#[derive(Default)]
pub struct SpadeCrateWithHintGenerator<HintGeneratorType> {
vertices: Vec<SpadePoint>,
_hint_generator_type: std::marker::PhantomData<HintGeneratorType>,
_hint_generator_type: core::marker::PhantomData<HintGeneratorType>,
}

pub type SpadeCrate = SpadeCrateWithHintGenerator<spade::LastUsedVertexHintGenerator>;
Expand Down
2 changes: 1 addition & 1 deletion examples/svg_renderer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod quicksketch;
mod scenario;
mod scenario_list;

type Result = std::result::Result<(), Box<dyn std::error::Error>>;
type Result = core::result::Result<(), Box<dyn std::error::Error>>;

/// Used for rendering SVGs for documentation. These are inlined (via #[doc = include_str!(...)])
/// into the doc comment of a few items. That makes sure they will be visible even for offline users.
Expand Down
4 changes: 2 additions & 2 deletions examples/svg_renderer/quicksketch/color.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt::Display;
use core::fmt::Display;

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Copy)]
pub struct SketchColor {
Expand All @@ -8,7 +8,7 @@ pub struct SketchColor {
}

impl Display for SketchColor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "rgb({} {} {})", self.red, self.green, self.blue)
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/svg_renderer/quicksketch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl Style {
stroke_dash_array,
])
.flatten()
.chain(std::iter::once(fill))
.chain(core::iter::once(fill))
.collect::<Vec<_>>()
.join("; ")
}
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/bulk_load_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub struct FuzzPoint {
y: f64,
}

impl std::fmt::Debug for FuzzPoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Debug for FuzzPoint {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!("Point2::new({:?}, {:?})", self.x, self.y))
}
}
4 changes: 2 additions & 2 deletions fuzz/fuzz_targets/bulk_load_int_fuzz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub struct IntFuzzPoint {
y: i32,
}

impl std::fmt::Debug for IntFuzzPoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Debug for IntFuzzPoint {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!("Point2::new({:?}.0, {:?}.0)", self.x, self.y))
}
}
11 changes: 8 additions & 3 deletions src/cdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::{
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use alloc::{vec, vec::Vec};

/// Undirected edge type of a [ConstrainedDelaunayTriangulation] (CDT).
///
/// CDTs need to store if an undirected edge is a constrained edge. To do so, CDTs don't use
Expand Down Expand Up @@ -362,7 +364,7 @@ where
VertexOutDirection::EdgeIntersection(edge) => edge,
};

let mut border_loop = std::collections::VecDeque::new();
let mut border_loop = alloc::collections::VecDeque::new();

border_loop.push_back(first_edge.rev().next().fix());
border_loop.push_front(first_edge.rev().prev().fix());
Expand Down Expand Up @@ -552,6 +554,8 @@ mod test {
type Cdt = ConstrainedDelaunayTriangulation<Point2<f64>>;
type Delaunay = DelaunayTriangulation<Point2<f64>>;

use alloc::{vec, vec::Vec};

#[test]
fn test_add_single_simple_constraint() -> Result<(), InsertionError> {
let mut cdt = Cdt::new();
Expand Down Expand Up @@ -657,7 +661,7 @@ mod test {
fn test_add_border_constraint() -> Result<(), InsertionError> {
let points = random_points_with_seed(1000, SEED);
let mut cdt = Cdt::new();
let mut max_y = -::std::f64::MAX;
let mut max_y = -::core::f64::MAX;
for point in points {
max_y = max_y.max(point.y);
cdt.insert(point)?;
Expand Down Expand Up @@ -695,7 +699,8 @@ mod test {
for p in delaunay_points {
d.insert(p)?;
}
let mut used_vertices = ::std::collections::HashSet::new();
let mut used_vertices = ::hashbrown::HashSet::new();

let mut inserted_constraints = Vec::new();
for v in d.vertices() {
// Insert only edges that do not touch at the end points if
Expand Down
8 changes: 6 additions & 2 deletions src/delaunay_core/bulk_load.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use std::cmp::{Ordering, Reverse};
use core::cmp::{Ordering, Reverse};

use crate::{HasPosition, InsertionError, Point2, Triangulation, TriangulationExt};

use super::{dcel_operations, FixedDirectedEdgeHandle, FixedUndirectedEdgeHandle};

use alloc::vec::Vec;

/// An `f64` wrapper implementing `Ord` and `Eq`.
///
/// This is only used as part of bulk loading.
Expand Down Expand Up @@ -469,7 +471,7 @@ impl Hull {

const INVALID: usize = usize::MAX;
self.buckets
.extend(std::iter::repeat(INVALID).take(target_size));
.extend(core::iter::repeat(INVALID).take(target_size));

let (first_index, current_node) = self
.data
Expand Down Expand Up @@ -753,6 +755,8 @@ mod test {

use super::Hull;

use alloc::vec::Vec;

#[test]
fn test_bulk_load_with_small_number_of_vertices() -> Result<(), InsertionError> {
for size in 0..10 {
Expand Down
2 changes: 2 additions & 0 deletions src/delaunay_core/bulk_load_fuzz_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{DelaunayTriangulation, Point2, Triangulation, TriangulationExt};

use alloc::{vec, vec::Vec};

fn fuzz_test(vertices: Vec<Point2<f64>>) {
let mut clone = vertices.clone();
clone.sort_by(|l, r| l.partial_cmp(r).unwrap());
Expand Down
2 changes: 2 additions & 0 deletions src/delaunay_core/dcel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use super::handles::*;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use alloc::vec::Vec;

#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy, Hash)]
pub struct EdgeData<DE, UE> {
directed_data: [DE; 2],
Expand Down
4 changes: 4 additions & 0 deletions src/delaunay_core/dcel_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use super::handles::*;

use smallvec::SmallVec;

use alloc::{vec, vec::Vec};

pub const OUTER_FACE_HANDLE: FixedFaceHandle<PossiblyOuterTag> = new_fixed_face_handle(0);

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -1308,6 +1310,8 @@ mod test {

use super::{Dcel, FixedDirectedEdgeHandle, FixedFaceHandle, FixedVertexHandle};

use alloc::{vec, vec::Vec};

fn default_triangle() -> Dcel<usize, (), ()> {
use super::{EdgeEntry, FaceEntry, HalfEdgeEntry, VertexEntry};

Expand Down
6 changes: 3 additions & 3 deletions src/delaunay_core/handles/handle_defs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::convert::TryInto;
use core::convert::TryInto;

use super::super::Dcel;
use super::public_handles::{InnerOuterMarker, PossiblyOuterTag};
Expand Down Expand Up @@ -27,8 +27,8 @@ pub struct FixedHandleImpl<Type, InnerOuter: InnerOuterMarker> {
inner_outer: InnerOuter,
}

impl<Type, InnerOuter: InnerOuterMarker> std::fmt::Debug for FixedHandleImpl<Type, InnerOuter> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl<Type, InnerOuter: InnerOuterMarker> core::fmt::Debug for FixedHandleImpl<Type, InnerOuter> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("FixedHandle")
.field("index", &self.index)
.finish()
Expand Down
24 changes: 12 additions & 12 deletions src/delaunay_core/handles/handle_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ use super::iterators::NextBackFn;
use super::public_handles::*;

use crate::{HasPosition, LineSideInfo, Point2};
use core::cmp::Ordering;
use core::fmt::Debug;
use core::hash::{Hash, Hasher};
use num_traits::{Float, One};
use std::cmp::Ordering;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};

// Debug implementations
impl<'a, V, DE, UE, F> std::fmt::Debug for VertexHandle<'a, V, DE, UE, F> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
impl<'a, V, DE, UE, F> core::fmt::Debug for VertexHandle<'a, V, DE, UE, F> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "VertexHandle({:?})", self.handle.index())
}
}

impl<'a, V, DE, UE, F> Debug for DirectedEdgeHandle<'a, V, DE, UE, F> {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(
f,
"DirectedEdgeHandle - id: {:?} ({:?} -> {:?})",
Expand All @@ -30,8 +30,8 @@ impl<'a, V, DE, UE, F> Debug for DirectedEdgeHandle<'a, V, DE, UE, F> {
}
}

impl<'a, V, DE, UE, F> std::fmt::Debug for UndirectedEdgeHandle<'a, V, DE, UE, F> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> ::std::fmt::Result {
impl<'a, V, DE, UE, F> core::fmt::Debug for UndirectedEdgeHandle<'a, V, DE, UE, F> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> ::core::fmt::Result {
let [v0, v1] = self.vertices();
write!(
f,
Expand All @@ -43,8 +43,8 @@ impl<'a, V, DE, UE, F> std::fmt::Debug for UndirectedEdgeHandle<'a, V, DE, UE, F
}
}

impl<'a, V, DE, UE, F> std::fmt::Debug for FaceHandle<'a, PossiblyOuterTag, V, DE, UE, F> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> ::std::fmt::Result {
impl<'a, V, DE, UE, F> core::fmt::Debug for FaceHandle<'a, PossiblyOuterTag, V, DE, UE, F> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> ::core::fmt::Result {
if let Some(inner) = self.as_inner() {
inner.fmt(f)
} else {
Expand All @@ -53,8 +53,8 @@ impl<'a, V, DE, UE, F> std::fmt::Debug for FaceHandle<'a, PossiblyOuterTag, V, D
}
}

impl<'a, V, DE, UE, F> std::fmt::Debug for FaceHandle<'a, InnerTag, V, DE, UE, F> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> ::std::fmt::Result {
impl<'a, V, DE, UE, F> core::fmt::Debug for FaceHandle<'a, InnerTag, V, DE, UE, F> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> ::core::fmt::Result {
let [v0, v1, v2] = self.vertices();
write!(
f,
Expand Down
2 changes: 1 addition & 1 deletion src/delaunay_core/handles/iterators/circular_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::marker::PhantomData;
use core::marker::PhantomData;

use super::super::DirectedEdgeHandle;

Expand Down
8 changes: 4 additions & 4 deletions src/delaunay_core/handles/iterators/fixed_iterators.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::marker::PhantomData;
use core::marker::PhantomData;

use super::super::handle_defs::{DelaunayElementType, DynamicHandleImpl, FixedHandleImpl};
use super::super::InnerOuterMarker;

use crate::delaunay_core::Dcel;

pub struct FixedHandleIterator<Type, InnerOuter> {
range: std::ops::Range<usize>,
range: core::ops::Range<usize>,
ty: PhantomData<Type>,
inner_outer: PhantomData<InnerOuter>,
}
Expand Down Expand Up @@ -59,7 +59,7 @@ impl<Type: Default, InnerOuter: InnerOuterMarker> DoubleEndedIterator
pub struct DynamicHandleIterator<'a, V, DE, UE, F, Type, InnerOuter> {
fixed_iterator: FixedHandleIterator<Type, InnerOuter>,
dcel: &'a Dcel<V, DE, UE, F>,
inner_outer: std::marker::PhantomData<InnerOuter>,
inner_outer: core::marker::PhantomData<InnerOuter>,
}

impl<'a, V, DE, UE, F, Type, InnerOuter> DynamicHandleIterator<'a, V, DE, UE, F, Type, InnerOuter>
Expand All @@ -71,7 +71,7 @@ where
DynamicHandleIterator {
fixed_iterator: FixedHandleIterator::new(Type::num_elements(dcel)),
dcel,
inner_outer: std::marker::PhantomData,
inner_outer: core::marker::PhantomData,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/delaunay_core/handles/iterators/hull_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ impl<'a, V, DE, UE, F> DoubleEndedIterator for HullIterator<'a, V, DE, UE, F> {

#[cfg(test)]
mod test {

use alloc::vec::Vec;

use crate::test_utilities::{random_points_with_seed, SEED};
use crate::{DelaunayTriangulation, InsertionError, Point2, Triangulation};

Expand Down
2 changes: 1 addition & 1 deletion src/delaunay_core/handles/public_handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub const OUTER_FACE: FixedFaceHandle<PossiblyOuterTag> = dcel_operations::OUTER
///
/// There should be no need to implement this.
pub trait InnerOuterMarker:
Clone + Copy + PartialEq + Eq + PartialOrd + Ord + std::fmt::Debug + Default + std::hash::Hash
Clone + Copy + PartialEq + Eq + PartialOrd + Ord + core::fmt::Debug + Default + core::hash::Hash
{
fn debug_string() -> &'static str;
}
Expand Down
6 changes: 5 additions & 1 deletion src/delaunay_core/hint_generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::atomic::{AtomicUsize, Ordering};
use core::sync::atomic::{AtomicUsize, Ordering};

use crate::{
DelaunayTriangulation, HasPosition, Point2, SpadeNum, Triangulation, TriangulationExt,
Expand All @@ -9,6 +9,8 @@ use super::FixedVertexHandle;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use alloc::vec::Vec;

/// A structure used to speed up common operations on delaunay triangulations like insertion and geometry queries by providing
/// hints on where to start searching for elements.
///
Expand Down Expand Up @@ -288,6 +290,8 @@ mod test {
Triangulation, TriangulationExt,
};

use alloc::vec::Vec;

const BRANCH_FACTOR: u32 = 3;

type HierarchyTriangulation = DelaunayTriangulation<
Expand Down
Loading
Loading