Skip temporary allocations

This commit is contained in:
2019-10-31 13:54:58 +01:00
parent cdb3746a34
commit 82050d6b75
9 changed files with 37 additions and 59 deletions

View File

@@ -120,13 +120,9 @@ impl Core {
}
// Check if the given space_id is referenced in the current core.
pub fn is_empty<S>(&self, space_id: S) -> bool
where
S: Into<String>,
{
let id = space_id.into();
pub fn is_empty(&self, space_id: &str) -> bool {
for s in &self.space_db {
if s.name() == &id {
if s.name() == space_id {
return s.is_empty();
}
}

View File

@@ -94,15 +94,10 @@ impl DataBase {
}
// Check if the given space_id is referenced in the DB.
fn is_empty<S>(&self, id: S) -> bool
where
S: Into<String>,
{
let id = id.into();
fn is_empty(&self, id: &str) -> bool {
for s in self.cores.keys() {
let core: &Core = self.cores.find(s)[0];
if !core.is_empty(id.clone()) {
if !core.is_empty(id) {
return false;
}
}
@@ -110,22 +105,19 @@ impl DataBase {
true
}
fn check_exactly_one<'t, T, S>(list: &[&'t T], name: S, value: S) -> Result<&'t T, String>
where
S: Into<String>,
{
fn check_exactly_one<'t, T>(list: &[&'t T], name: &str, value: &str) -> Result<&'t T, String> {
if list.len() > 1 {
Err(format!(
"Multiple {} registered under `{}`: {}",
name.into(),
value.into(),
name,
value,
list.len()
))
} else if list.is_empty() {
Err(format!(
"No {} registered under `{}`: {}",
name.into(),
value.into(),
name,
value,
list.len()
))
} else {
@@ -133,17 +125,6 @@ impl DataBase {
}
}
pub fn space_id<S>(&self, name: S) -> Result<String, String>
where
S: Into<String>,
{
let name = name.into();
let r = self.reference_spaces.find(&name);
let s: &Space = Self::check_exactly_one(&r, "spaces", &name)?;
Ok(s.name().clone())
}
// Lookup a space within the reference spaces registered
pub fn space_keys(&self) -> &Vec<String> {
self.reference_spaces.keys()
@@ -151,13 +132,12 @@ impl DataBase {
// Lookup a space within the reference spaces registered
pub fn space(&self, name: &str) -> Result<&Space, String> {
let name = name.into();
if &name == space::Space::universe().name() {
if name == space::Space::universe().name() {
Ok(space::Space::universe())
} else {
let r = self.reference_spaces.find(&name);
let r = self.reference_spaces.find(&name.to_string());
Self::check_exactly_one(&r, "spaces", &name)
Self::check_exactly_one(&r, "spaces", name)
}
}
@@ -168,10 +148,9 @@ impl DataBase {
// Lookup a dataset within the datasets registered
pub fn core(&self, name: &str) -> Result<&Core, String> {
let name = name.into();
let r = self.cores.find(&name);
let r = self.cores.find(&name.to_string());
Self::check_exactly_one(&r, "cores", &name)
Self::check_exactly_one(&r, "cores", name)
}
}

View File

@@ -9,9 +9,9 @@ pub enum NumberSet {
R,
}
impl From<String> for NumberSet {
fn from(set: String) -> Self {
match set.as_str() {
impl From<&str> for NumberSet {
fn from(set: &str) -> Self {
match set {
"N" => NumberSet::N,
"Z" => NumberSet::Z,
"Q" => NumberSet::Q,
@@ -21,8 +21,8 @@ impl From<String> for NumberSet {
}
}
impl From<NumberSet> for String {
fn from(set: NumberSet) -> String {
impl From<&NumberSet> for String {
fn from(set: &NumberSet) -> String {
let s = match set {
NumberSet::N => "N",
NumberSet::Z => "R",

View File

@@ -25,13 +25,13 @@ impl CoordinateSystem {
}
}
pub fn axes(&self) -> Vec<Axis> {
pub fn axes(&self) -> &Vec<Axis> {
match self {
CoordinateSystem::Universe { .. } => {
//FIXME: Generate a CoordinateSystem on the fly or store it as part of the Universe Space?
unimplemented!()
}
CoordinateSystem::AffineSystem { axes, .. } => axes.clone(),
CoordinateSystem::AffineSystem { axes, .. } => axes,
}
}

View File

@@ -59,7 +59,7 @@ impl Space {
self.system.origin()
}
pub fn axes(&self) -> Vec<Axis> {
pub fn axes(&self) -> &Vec<Axis> {
self.system.axes()
}

View File

@@ -384,14 +384,15 @@ impl From<Vec<u64>> for Position {
impl From<Position> for Vec<f64> {
fn from(position: Position) -> Self {
let point: Vec<&Coordinate> = (&position).into();
point.into_iter().map(|c| c.into()).collect()
(&position).into()
}
}
impl From<&Position> for Vec<f64> {
fn from(coordinates: &Position) -> Self {
coordinates.clone().into()
fn from(position: &Position) -> Self {
let point: Vec<&Coordinate> = position.into();
point.into_iter().map(|c| c.into()).collect()
}
}

View File

@@ -167,7 +167,7 @@ impl Shape {
// Initialise the current value
let mut current = lower.clone();
// Add the first Position to the results, as nxt will return the following one.
// Add the first Position to the results, as next will return the following one.
results.push(current.clone());
while next(lower.dimensions(), &lower, higher, &mut current) {
results.push(current.clone())

View File

@@ -60,8 +60,9 @@ impl SpaceDB {
// Limit temporary values lifetimes
{
// Sort by values, smaller to bigger.
// Sort by values, smaller to bigger. We clone in order leave as-is scales.
let mut exps = scales.clone();
// FIXME: This should be done using all the values, somehow
exps.sort_unstable_by_key(|v| v[0]);
let mut previous = 0u32;

View File

@@ -54,7 +54,7 @@ pub struct Properties {
impl From<&space::Graduation> for Graduation {
fn from(g: &space::Graduation) -> Self {
Graduation {
set: g.set.clone().into(),
set: (&g.set).into(),
minimum: g.minimum,
maximum: g.maximum,
steps: g.steps,
@@ -69,7 +69,7 @@ impl From<Axis> for space::Axis {
space::Axis::new(
&axis.measurement_unit,
axis.unit_vector,
g.set.into(),
g.set.as_str().into(),
g.minimum,
g.maximum,
g.steps,
@@ -108,7 +108,7 @@ impl From<&space::Space> for Space {
Space {
name: space.name().clone(),
origin: space.origin().clone().into(),
origin: space.origin().into(),
axes,
}
}
@@ -196,7 +196,8 @@ pub fn build_index(
space_set_objects.push(SpaceSetObject::new(
&point.reference_space,
point.vertices[0].clone().into(),
// Use a reference to prevent an allocation
(&point.vertices[0]).into(),
value.into(),
))
}
@@ -205,7 +206,7 @@ pub fn build_index(
properties.append(&mut properties_hm.drain().map(|(_, v)| v).collect::<Vec<_>>());
}
properties.sort_unstable_by_key(|p| p.id().clone());
properties.sort_unstable_by(|a, b| a.id().cmp(b.id()));
space_set_objects.iter_mut().for_each(|object| {
let id = properties_ref[object.value().u64() as usize];