PathCache

Struct PathCache 

Source
pub struct PathCache<K>
where K: PartialEq + Eq + Hash,
{ /* private fields */ }
Expand description

A simple cache for path data.

Implementations§

Source§

impl<K> PathCache<K>
where K: PartialEq + Eq + Hash + Clone,

Source

pub fn new() -> Self

Initializes a new, empty terrain cache.

Source

pub fn get_cached_path(&self, path_key: &K) -> Option<&[Position]>

Returns the cached path, if it exists. Returns None if the path isn’t cached.

§Examples
use screeps::{Direction, Position, RoomCoordinate};
use screeps_pathfinding::utils::cache::PathCache;

// Create a new path cache that uses String as the key type
let mut cache: PathCache<String> = PathCache::new();

// Build a path
let start = Position::new(
    RoomCoordinate::try_from(24).unwrap(),
    RoomCoordinate::try_from(18).unwrap(),
    "E5N6".parse().unwrap(),
);
let goal = start.checked_add_direction(Direction::Right).unwrap();
let path = vec!(start, goal);

let existing_key = "existing_key".to_string();

// Actually store the path in the cache
assert_eq!(cache.is_path_cached(&existing_key), false);
cache.update_cached_path(&existing_key, path.into_iter());
assert_eq!(cache.is_path_cached(&existing_key), true);

// Pull the cached copy of the path
let path_opt = cache.get_cached_path(&existing_key);
assert!(path_opt.is_some());

let path_slice = path_opt.unwrap();
assert!(path_slice.len() == 2);
assert!(path_slice[0] == start);
assert!(path_slice[1] == goal);

// Attempt to pull a non-existent path entry
let nonexisting_key = "nonexisting_key".to_string();
let path_opt = cache.get_cached_path(&nonexisting_key);
assert!(path_opt.is_none());
assert_eq!(cache.is_path_cached(&nonexisting_key), false);
Source

pub fn is_path_cached(&self, path_key: &K) -> bool

Returns whether a path is cached with the provided path key.

§Examples
use screeps::{Direction, Position, RoomCoordinate};
use screeps_pathfinding::utils::cache::PathCache;

// Create a new path cache that uses String as the key type
let mut cache: PathCache<String> = PathCache::new();

// A path doesn't exist yet in the cache for this key
let key = "some_key".to_string();
assert_eq!(cache.is_path_cached(&key), false);

// Build a path
let start = Position::new(
    RoomCoordinate::try_from(24).unwrap(),
    RoomCoordinate::try_from(18).unwrap(),
    "E5N6".parse().unwrap(),
);
let goal = start.checked_add_direction(Direction::Right).unwrap();
let path = vec!(start, goal);

// Store the path in the cache
cache.update_cached_path(&key, path.into_iter());

assert_eq!(cache.is_path_cached(&key), true);
Source

pub fn get_path<G>( &mut self, path_key: &K, generator_fn: G, ) -> Option<&[Position]>
where G: FnOnce(&K) -> Option<Vec<Position>>,

Returns the path, generating and caching it if it’s not already cached.

Returns None only if the path is not cached and the generation function returns None.

§Examples
use screeps::{Direction, Position, RoomCoordinate};
use screeps_pathfinding::utils::cache::PathCache;

// Create a new path cache that uses String as the key type
let mut cache: PathCache<String> = PathCache::new();

// The path doesn't exist yet in the cache
let key = "some_key".to_string();
assert_eq!(cache.is_path_cached(&key), false);

let start = Position::new(
    RoomCoordinate::try_from(24).unwrap(),
    RoomCoordinate::try_from(18).unwrap(),
    "E5N6".parse().unwrap(),
);
let goal = start.checked_add_direction(Direction::Right).unwrap();

// Pull the path, generating it since it doesn't exist
let path_opt = cache.get_path(&key, |path_key| {
    // Build a path
    let path = vec!(start, goal);
    Some(path)
});
assert!(path_opt.is_some());

let path_slice = path_opt.unwrap();
assert!(path_slice.len() == 2);
assert!(path_slice[0] == start);
assert!(path_slice[1] == goal);

assert_eq!(cache.is_path_cached(&key), true);
Source

pub fn update_cached_path( &mut self, path_key: &K, path_iter: impl Iterator<Item = Position>, )

Updates the path cache for a specific key.

This allows for pre-loading the cache with any existing path data you might already have available.

§Examples
use screeps::{Direction, Position, RoomCoordinate};
use screeps_pathfinding::utils::cache::PathCache;

// Create a new path cache that uses String as the key type
let mut cache: PathCache<String> = PathCache::new();

// Build a path
let start = Position::new(
    RoomCoordinate::try_from(24).unwrap(),
    RoomCoordinate::try_from(18).unwrap(),
    "E5N6".parse().unwrap(),
);
let goal = start.checked_add_direction(Direction::Right).unwrap();
let path = vec!(start, goal);

let existing_key = "existing_key".to_string();

// Store the path in the cache
cache.update_cached_path(&existing_key, path.into_iter());

// Pull the cached copy of the path
let path_opt = cache.get_cached_path(&existing_key);
assert!(path_opt.is_some());

let path_slice = path_opt.unwrap();
assert!(path_slice.len() == 2);
assert!(path_slice[0] == start);
assert!(path_slice[1] == goal);
Source

pub fn remove_cached_path(&mut self, path_key: &K)

Removes the path cached for a specific key.

Trait Implementations§

Source§

impl<K> Clone for PathCache<K>
where K: PartialEq + Eq + Hash + Clone,

Source§

fn clone(&self) -> PathCache<K>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K> Debug for PathCache<K>
where K: PartialEq + Eq + Hash + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K> Default for PathCache<K>
where K: PartialEq + Eq + Hash + Clone,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, K> Deserialize<'de> for PathCache<K>
where K: PartialEq + Eq + Hash + Deserialize<'de>,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<K> Serialize for PathCache<K>
where K: PartialEq + Eq + Hash + Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<K> Freeze for PathCache<K>

§

impl<K> RefUnwindSafe for PathCache<K>
where K: RefUnwindSafe,

§

impl<K> Send for PathCache<K>
where K: Send,

§

impl<K> Sync for PathCache<K>
where K: Sync,

§

impl<K> Unpin for PathCache<K>
where K: Unpin,

§

impl<K> UnwindSafe for PathCache<K>
where K: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,