Refactor graph and misc
This commit is contained in:
parent
5f5fe0c149
commit
8f163269bd
22 changed files with 126 additions and 45 deletions
|
|
@ -3,6 +3,7 @@ pub mod beltoptions;
|
|||
pub mod block;
|
||||
pub mod color;
|
||||
pub mod direction;
|
||||
pub mod misc;
|
||||
pub mod pathfield;
|
||||
pub mod position;
|
||||
pub mod quaterdirection;
|
||||
|
|
|
|||
52
factorio-core/src/misc/arena.rs
Normal file
52
factorio-core/src/misc/arena.rs
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#[derive(Debug)]
|
||||
pub struct Arena<T> {
|
||||
data: Vec<Option<T>>,
|
||||
free: Vec<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct ArenaKey(usize);
|
||||
|
||||
impl Default for ArenaKey {
|
||||
fn default() -> Self {
|
||||
ArenaKey(usize::MAX)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Arena<T> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
data: Vec::new(),
|
||||
free: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, item: T) -> ArenaKey {
|
||||
if let Some(i) = self.free.pop() {
|
||||
self.data[i] = Some(item);
|
||||
ArenaKey(i)
|
||||
} else {
|
||||
self.data.push(Some(item));
|
||||
ArenaKey(self.data.len() - 1)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, key: &ArenaKey) -> &T {
|
||||
self.data.get(key.0).unwrap().as_ref().unwrap()
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, key: &ArenaKey) -> &mut T {
|
||||
self.data.get_mut(key.0).unwrap().as_mut().unwrap()
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, key: ArenaKey) -> T {
|
||||
self.free.push(key.0);
|
||||
self.data[key.0].take().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Default for Arena<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
88
factorio-core/src/misc/map.rs
Normal file
88
factorio-core/src/misc/map.rs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Map<T> {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
data: Vec<T>,
|
||||
}
|
||||
|
||||
impl<T> Map<T>
|
||||
where
|
||||
T: Default,
|
||||
{
|
||||
pub fn new(width: usize, height: usize) -> Self {
|
||||
let mut data = Vec::new();
|
||||
for _ in 0..(width * height) {
|
||||
data.push(T::default());
|
||||
}
|
||||
|
||||
Self {
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Map<T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
pub fn new_with(width: usize, height: usize, value: T) -> Self {
|
||||
let mut data = Vec::new();
|
||||
for _ in 0..(width * height) {
|
||||
data.push(value.clone());
|
||||
}
|
||||
|
||||
Self {
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Map<T> {
|
||||
fn index(&self, x: usize, y: usize) -> usize {
|
||||
x + y * self.width
|
||||
}
|
||||
|
||||
pub fn get(&self, x: usize, y: usize) -> &T {
|
||||
assert!(
|
||||
x < self.width,
|
||||
"assertion failed: x < self.width; x: {}, self.width: {}",
|
||||
x,
|
||||
self.width
|
||||
);
|
||||
assert!(
|
||||
y < self.height,
|
||||
"assertion failed: y < self.height; y: {}, self.height: {}",
|
||||
y,
|
||||
self.height
|
||||
);
|
||||
let i = self.index(x, y);
|
||||
self.data.get(i).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, x: usize, y: usize) -> &mut T {
|
||||
assert!(
|
||||
x < self.width,
|
||||
"assertion failed: x < self.width; x: {}, self.width: {}",
|
||||
x,
|
||||
self.width
|
||||
);
|
||||
assert!(
|
||||
y < self.height,
|
||||
"assertion failed: y < self.height; y: {}, self.height: {}",
|
||||
y,
|
||||
self.height
|
||||
);
|
||||
let i = self.index(x, y);
|
||||
self.data.get_mut(i).unwrap()
|
||||
}
|
||||
|
||||
pub fn set(&mut self, x: usize, y: usize, item: T) {
|
||||
*self.get_mut(x, y) = item;
|
||||
}
|
||||
}
|
||||
5
factorio-core/src/misc/mod.rs
Normal file
5
factorio-core/src/misc/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
pub mod arena;
|
||||
pub mod map;
|
||||
|
||||
pub use arena::*;
|
||||
pub use map::*;
|
||||
|
|
@ -91,3 +91,14 @@ impl From<(PositionType, PositionType)> for Position {
|
|||
Self::new(value.0, value.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Div<PositionType> for Position {
|
||||
type Output = Position;
|
||||
|
||||
fn div(self, rhs: PositionType) -> Self::Output {
|
||||
Self {
|
||||
x: self.x / rhs,
|
||||
y: self.y / rhs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,6 +85,14 @@ impl Color {
|
|||
Self::new(255, 255, 255)
|
||||
}
|
||||
|
||||
pub fn cyan() -> Self {
|
||||
Self::new(0x0f, 0xf0, 0xfc)
|
||||
}
|
||||
|
||||
pub fn gray(l: u8) -> Self {
|
||||
Self::new(l, l, l)
|
||||
}
|
||||
|
||||
pub fn index(i: usize) -> Self {
|
||||
let c = [
|
||||
Color::new(0xe6, 0x00, 0x49),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue