honeycomb_core/cmap/dim2/embed.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
//! Attribute operations implementation
//!
//! This module contains code used to implement operations on the embedded data associated to the
//! map. This includes operations regarding vertices as well as (in the future) user-defined
//! generic attributes
// ------ IMPORT
use crate::prelude::{AttributeBind, AttributeUpdate, CMap2, Vertex2, VertexIdType};
use crate::{
attributes::{AttributeStorage, UnknownAttributeStorage},
geometry::CoordsFloat,
};
// ------ CONTENT
/// **Built-in vertex-related methods**
impl<T: CoordsFloat> CMap2<T> {
/// Return the current number of vertices.
#[must_use = "returned value is not used, consider removing this method call"]
pub fn n_vertices(&self) -> usize {
self.vertices.n_attributes()
}
/// Fetch vertex value associated to a given identifier.
///
/// # Arguments
///
/// - `vertex_id: VertexIdentifier` -- Identifier of the given vertex.
///
/// # Return
///
/// This method return a `Option` taking the following values:
/// - `Some(v: Vertex2)` if there is a vertex associated to this ID.
/// - `None` -- otherwise
///
/// # Panics
///
/// The method may panic if:
/// - the index lands out of bounds
/// - the index cannot be converted to `usize`
#[must_use = "returned value is not used, consider removing this method call"]
pub fn vertex(&self, vertex_id: VertexIdType) -> Option<Vertex2<T>> {
self.vertices.get(vertex_id)
}
/// Insert a vertex in the combinatorial map.
///
/// This method can be interpreted as giving a value to the vertex of a specific ID. Vertices
/// implicitly exist through topology, but their spatial representation is not automatically
/// created at first.
///
/// # Arguments
///
/// - `vertex_id: VertexIdentifier` -- Vertex identifier to attribute a value to.
/// - `vertex: impl Into<Vertex2>` -- Value used to create a [Vertex2] value.
///
/// # Panics
///
/// The method may panic if:
/// - **there is already a vertex associated to the specified index**
/// - the index lands out of bounds
/// - the index cannot be converted to `usize`
pub fn insert_vertex(&self, vertex_id: VertexIdType, vertex: impl Into<Vertex2<T>>) {
self.vertices.insert(vertex_id, vertex.into());
}
#[allow(clippy::must_use_candidate)]
/// Remove a vertex from the combinatorial map.
///
/// # Arguments
///
/// - `vertex_id: VertexIdentifier` -- Identifier of the vertex to remove.
///
/// # Return
///
/// This method return a `Option` taking the following values:
/// - `Some(v: Vertex2)` -- The vertex was successfully removed & its value was returned
/// - `None` -- The vertex was not found in the internal storage
///
/// # Panics
///
/// The method may panic if:
/// - the index lands out of bounds
/// - the index cannot be converted to `usize`
pub fn remove_vertex(&self, vertex_id: VertexIdType) -> Option<Vertex2<T>> {
self.vertices.remove(vertex_id)
}
/// Try to overwrite the given vertex with a new value.
///
/// # Arguments
///
/// - `vertex_id: VertexIdentifier` -- Identifier of the vertex to replace.
/// - `vertex: impl<Into<Vertex2>>` -- New value for the vertex.
///
/// # Return
///
/// This method return an `Option` taking the following values:
/// - `Some(v: Vertex2)` -- The vertex was successfully overwritten & its previous value was
/// returned
/// - `None` -- The vertex was set, but no value were overwritten
///
/// # Panics
///
/// The method may panic if:
/// - the index lands out of bounds
/// - the index cannot be converted to `usize`
pub fn replace_vertex(
&self,
vertex_id: VertexIdType,
vertex: impl Into<Vertex2<T>>,
) -> Option<Vertex2<T>> {
self.vertices.replace(vertex_id, vertex.into())
}
}
/// **Generic attribute-related methods**
impl<T: CoordsFloat> CMap2<T> {
/// Setter
///
/// Set the value of an attribute for a given index. This operation is not affected by
/// the initial state of the edited entry.
///
/// # Arguments
///
/// - `index: A::IdentifierType` -- Cell index.
/// - `val: A` -- Attribute value.
///
/// ## Generic
///
/// - `A: AttributeBind + AttributeUpdate` -- Attribute kind to edit.
///
/// # Panics
///
/// The method:
/// - should panic if the index lands out of bounds
/// - may panic if the index cannot be converted to `usize`
pub fn set_attribute<A: AttributeBind + AttributeUpdate>(&self, id: A::IdentifierType, val: A) {
self.attributes.set_attribute::<A>(id, val);
}
/// Setter
///
/// Insert an attribute value at a given undefined index. See the panics section information
/// on behavior if the value is already defined.
///
/// # Arguments
///
/// - `index: A::IdentifierType` -- Cell index.
/// - `val: A` -- Attribute value.
///
/// ## Generic
///
/// - `A: AttributeBind + AttributeUpdate` -- Attribute kind to edit.
///
/// # Panics
///
/// The method:
/// - **should panic if there is already a value associated to the specified index**
/// - should panic if the index lands out of bounds
/// - may panic if the index cannot be converted to `usize`
pub fn insert_attribute<A: AttributeBind + AttributeUpdate>(
&self,
id: A::IdentifierType,
val: A,
) {
self.attributes.insert_attribute::<A>(id, val);
}
/// Getter
///
/// # Arguments
///
/// - `index: A::IdentifierType` -- Cell index.
///
/// ## Generic
///
/// - `A: AttributeBind + AttributeUpdate` -- Attribute kind to edit.
///
/// # Return
///
/// The method should return:
/// - `Some(val: A)` if there is an attribute associated with the specified index,
/// - `None` if there is not.
///
/// # Panics
///
/// The method:
/// - should panic if the index lands out of bounds
/// - may panic if the index cannot be converted to `usize`
pub fn get_attribute<A: AttributeBind + AttributeUpdate>(
&self,
id: A::IdentifierType,
) -> Option<A> {
self.attributes.get_attribute::<A>(id)
}
/// Setter
///
/// Replace the value of the attribute for a given index.
///
/// # Arguments
///
/// - `index: A::IdentifierType` -- Cell index.
/// - `val: A` -- Attribute value.
///
/// ## Generic
///
/// - `A: AttributeBind + AttributeUpdate` -- Attribute kind to edit.
///
/// # Return
///
/// The method should return:
/// - `Some(val_old: A)` if there was an attribute associated with the specified index,
/// - `None` if there is not.
///
/// In both cases, the new value should be set to the one specified as argument.
///
/// # Panics
///
/// The method:
/// - should panic if the index lands out of bounds
/// - may panic if the index cannot be converted to `usize`
pub fn replace_attribute<A: AttributeBind + AttributeUpdate>(
&self,
id: A::IdentifierType,
val: A,
) -> Option<A> {
self.attributes.replace_attribute::<A>(id, val)
}
/// Remove an attribute value from the storage and return it
///
/// # Arguments
///
/// - `index: A::IdentifierType` -- Cell index.
///
/// ## Generic
///
/// - `A: AttributeBind + AttributeUpdate` -- Attribute kind to edit.
///
/// # Return
///
/// The method should return:
/// - `Some(val: A)` if there was an attribute associated with the specified index,
/// - `None` if there is not.
///
/// # Panics
///
/// The method:
/// - may panic if the index lands out of bounds
/// - may panic if the index cannot be converted to `usize`
pub fn remove_attribute<A: AttributeBind + AttributeUpdate>(
&self,
id: A::IdentifierType,
) -> Option<A> {
self.attributes.remove_attribute::<A>(id)
}
// --- big guns
/// Remove an entire attribute storage from the map.
///
/// This method is useful when implementing routines that uses attributes to run; Those can then be removed
/// before the final result is returned.
///
/// # Generic
///
/// - `A: AttributeBind + AttributeUpdate` -- Attribute stored by the fetched storage.
pub fn remove_attribute_storage<A: AttributeBind + AttributeUpdate>(&mut self) {
self.attributes.remove_storage::<A>();
}
}