Skip to content

Commit

Permalink
Merge #534
Browse files Browse the repository at this point in the history
534: Add Storage::count and Storage::is_empty r=Xaeroxe a=torkleyy

Fixes #517



Co-authored-by: Thomas Schaller <[email protected]>
  • Loading branch information
bors[bot] and torkleyy committed Jan 4, 2019
2 parents eb8510c + 65b64f2 commit a1c564f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ travis-ci = { repository = "slide-rs/specs" }
crossbeam = "0.4.1"
derivative = "1"
fnv = "1.0"
hibitset = { version = "0.5.2", default-features = false }
hibitset = { version = "0.5.3", default-features = false }
log = "0.4"
mopa = "0.2"
shred = { version = "0.7.0", default-features = false }
Expand Down
16 changes: 14 additions & 2 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ pub struct Storage<'e, T, D> {
}

impl<'e, T, D> Storage<'e, T, D> {
/// Create a new `Storage`
/// Creates a new `Storage` from a fetched allocator and a immutable or mutable `MaskedStorage`,
/// named `data`.
pub fn new(entities: Fetch<'e, EntitiesRes>, data: D) -> Storage<'e, T, D> {
Storage {
data,
Expand All @@ -204,7 +205,7 @@ where
&self.data.inner
}

/// Returns the `EntitesRes` resource fetched by this storage.
/// Returns the `EntitiesRes` resource fetched by this storage.
/// **This does not have anything to do with the components inside.**
/// You only want to use this when implementing additional methods
/// for `Storage` via an extension trait.
Expand All @@ -221,6 +222,17 @@ where
}
}

/// Computes the number of elements this `Storage` contains by counting the bits in the bit set.
/// This operation will never be performed in constant time.
pub fn count(&self) -> usize {
self.mask().iter().count()
}

/// Checks whether this `Storage` is empty. This operation is very cheap.
pub fn is_empty(&self) -> bool {
self.mask().is_empty()
}

/// Returns true if the storage has a component for this entity, and that entity is alive.
pub fn contains(&self, e: Entity) -> bool {
self.data.mask.contains(e.id()) && self.entities.is_alive(e)
Expand Down

0 comments on commit a1c564f

Please sign in to comment.