Skip to content

Commit 313b9f3

Browse files
committed
feat(tools): Improved OrderedHashMap apis
1 parent 76b4aea commit 313b9f3

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

libraries/graphing/src/fdp.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ pub use edge::*;
1515
pub use repulsive::*;
1616

1717
use crate::{Graph, SharedNode};
18-
use alloc::collections::BTreeMap;
1918
use alloc::rc::Rc;
2019
use core::cell::RefCell;
2120
use core::fmt::{Debug, Formatter};
2221
use irox_geometry::{Point, Vector};
2322
use irox_tools::identifier::{Identifier, SharedIdentifier};
23+
use irox_tools::map::OrderedHashMap;
2424
use irox_units::units::angle::Angle;
2525

2626
const INITIAL_RADIUS: f64 = 1.0;
@@ -123,14 +123,30 @@ pub struct SimulationWorkingEdge {
123123
pub trait InitialNodePlacer {
124124
fn place_node(&mut self, node: &mut SharedNode, working: &mut SimulationWorkingNode);
125125
}
126-
#[derive(Debug, Default, Clone)]
126+
#[derive(Debug, Clone)]
127127
pub struct DefaultNodePlacement {
128+
initial_radius: f64,
128129
last_idx: usize,
129130
}
131+
impl DefaultNodePlacement {
132+
#[must_use]
133+
pub fn with_initial_radius(mut self, radius: f64) -> Self {
134+
self.initial_radius = radius;
135+
self
136+
}
137+
}
138+
impl Default for DefaultNodePlacement {
139+
fn default() -> Self {
140+
Self {
141+
last_idx: 0,
142+
initial_radius: INITIAL_RADIUS,
143+
}
144+
}
145+
}
130146
impl InitialNodePlacer for DefaultNodePlacement {
131147
fn place_node(&mut self, _node: &mut SharedNode, working: &mut SimulationWorkingNode) {
132148
let idx = self.last_idx;
133-
let init_radius = INITIAL_RADIUS * (0.5 + idx as f64).sqrt();
149+
let init_radius = self.initial_radius * (0.5 + idx as f64).sqrt();
134150
let init_angle = INITIAL_ANGLE * (idx as f64);
135151
let current_position = Vector {
136152
vx: init_radius * init_angle.cos(),
@@ -145,8 +161,8 @@ pub struct Simulation {
145161
pub graph: Shared<Graph>,
146162
pub forces: Vec<Force>,
147163

148-
pub working_nodes: BTreeMap<SharedIdentifier, SimulationWorkingNode>,
149-
pub working_edges: BTreeMap<SharedIdentifier, SimulationWorkingEdge>,
164+
pub working_nodes: OrderedHashMap<SharedIdentifier, SimulationWorkingNode>,
165+
pub working_edges: OrderedHashMap<SharedIdentifier, SimulationWorkingEdge>,
150166

151167
pub params: SimulationParams,
152168
pub placement: Box<dyn InitialNodePlacer>,

libraries/tools/src/map.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,24 @@ use alloc::collections::vec_deque::Iter;
88
use alloc::collections::VecDeque;
99
use core::borrow::Borrow;
1010
use core::hash::Hash;
11+
use std::collections::hash_map::{Entry, ValuesMut};
1112
use std::collections::HashMap;
1213

1314
///
1415
/// Tracks the insertion order of key/value pairs. Backed by a [`HashMap`] for storage
1516
/// and a [`VecDeque`] for key insertion order.
16-
#[derive(Default)]
1717
pub struct OrderedHashMap<K, V> {
1818
map: HashMap<K, V>,
1919
key_order: VecDeque<K>,
2020
}
21+
impl<K, V> Default for OrderedHashMap<K, V> {
22+
fn default() -> Self {
23+
Self {
24+
map: Default::default(),
25+
key_order: Default::default()
26+
}
27+
}
28+
}
2129

2230
impl<K, V> OrderedHashMap<K, V> {
2331
pub fn new() -> OrderedHashMap<K, V> {
@@ -69,6 +77,18 @@ impl<K: Eq + Hash + Clone, V> OrderedHashMap<K, V> {
6977
pub fn contains(&self, k: &K) -> bool {
7078
self.map.contains_key(k)
7179
}
80+
81+
pub fn entry(&mut self, k: K) -> Entry<'_, K, V>{
82+
self.map.entry(k)
83+
}
84+
85+
pub fn get_mut(&mut self, k: &K) -> Option<&mut V>{
86+
self.map.get_mut(k)
87+
}
88+
89+
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
90+
self.map.values_mut()
91+
}
7292
}
7393
impl<'a, K: Eq + Hash, V> OrderedHashMap<K, V> {
7494
pub fn iter(&'a self) -> OrderedMapIter<'a, K, V> {
@@ -84,6 +104,14 @@ impl<'a, K: Eq + Hash, V> OrderedHashMap<K, V> {
84104
}
85105
}
86106
}
107+
impl<'a, K: Eq + Hash, V> IntoIterator for &'a OrderedHashMap<K, V> {
108+
type Item = (&'a K, &'a V);
109+
type IntoIter = OrderedMapIter<'a, K, V>;
110+
111+
fn into_iter(self) -> Self::IntoIter {
112+
self.iter()
113+
}
114+
}
87115

88116
pub struct OrderedMapIter<'a, K, V> {
89117
key_iter: Iter<'a, K>,

0 commit comments

Comments
 (0)