Skip to content

Commit

Permalink
Add a method for accessing the width of a Table (bevyengine#6249)
Browse files Browse the repository at this point in the history
# Objective

There is currently no good way of getting the width (# of components) of a table outside of `bevy_ecs`.

# Solution

Added the methods `Table::{component_count, component_capacity}`
For consistency and clarity, renamed `Table::{len, capacity}` to `entity_count` and `entity_capacity`.

## Changelog

- Added the methods `Table::component_count` and `Table::component_capacity`
- Renamed `Table::len` and `Table::capacity` to `entity_count` and `entity_capacity`

## Migration Guide

Any use of `Table::len` should now be `Table::entity_count`. Any use of `Table::capacity` should now be `Table::entity_capacity`.
  • Loading branch information
JoJoJet authored and james7132 committed Oct 19, 2022
1 parent 68f774d commit b0caf70
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/query/iter.rs
Expand Up @@ -587,7 +587,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> QueryIterationCursor<'w, 's,
// `fetch_state`/`filter_state` are the states that `fetch/filter` were initialized with
Q::set_table(&mut self.fetch, &query_state.fetch_state, table);
F::set_table(&mut self.filter, &query_state.filter_state, table);
self.current_len = table.len();
self.current_len = table.entity_count();
self.current_index = 0;
continue;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/query/state.rs
Expand Up @@ -925,7 +925,7 @@ impl<Q: WorldQuery, F: ReadOnlyWorldQuery> QueryState<Q, F> {
Q::set_table(&mut fetch, &self.fetch_state, table);
F::set_table(&mut filter, &self.filter_state, table);

for table_index in 0..table.len() {
for table_index in 0..table.entity_count() {
if !F::table_filter_fetch(&mut filter, table_index) {
continue;
}
Expand Down Expand Up @@ -988,9 +988,9 @@ impl<Q: WorldQuery, F: ReadOnlyWorldQuery> QueryState<Q, F> {
}

let mut offset = 0;
while offset < table.len() {
while offset < table.entity_count() {
let func = func.clone();
let len = batch_size.min(table.len() - offset);
let len = batch_size.min(table.entity_count() - offset);
let task = async move {
let mut fetch = Q::init_fetch(
world,
Expand Down
26 changes: 18 additions & 8 deletions crates/bevy_ecs/src/storage/table.rs
Expand Up @@ -262,7 +262,7 @@ impl Table {
row: usize,
new_table: &mut Table,
) -> TableMoveResult {
debug_assert!(row < self.len());
debug_assert!(row < self.entity_count());
let is_last = row == self.entities.len() - 1;
let new_row = new_table.allocate(self.entities.swap_remove(row));
for (component_id, column) in self.columns.iter_mut() {
Expand Down Expand Up @@ -294,7 +294,7 @@ impl Table {
row: usize,
new_table: &mut Table,
) -> TableMoveResult {
debug_assert!(row < self.len());
debug_assert!(row < self.entity_count());
let is_last = row == self.entities.len() - 1;
let new_row = new_table.allocate(self.entities.swap_remove(row));
for (component_id, column) in self.columns.iter_mut() {
Expand Down Expand Up @@ -325,7 +325,7 @@ impl Table {
row: usize,
new_table: &mut Table,
) -> TableMoveResult {
debug_assert!(row < self.len());
debug_assert!(row < self.entity_count());
let is_last = row == self.entities.len() - 1;
let new_row = new_table.allocate(self.entities.swap_remove(row));
for (component_id, column) in self.columns.iter_mut() {
Expand Down Expand Up @@ -388,13 +388,23 @@ impl Table {
}

#[inline]
pub fn capacity(&self) -> usize {
pub fn entity_count(&self) -> usize {
self.entities.len()
}

#[inline]
pub fn component_count(&self) -> usize {
self.columns.len()
}

#[inline]
pub fn entity_capacity(&self) -> usize {
self.entities.capacity()
}

#[inline]
pub fn len(&self) -> usize {
self.entities.len()
pub fn component_capacity(&self) -> usize {
self.columns.capacity()
}

#[inline]
Expand Down Expand Up @@ -565,7 +575,7 @@ mod tests {
};
}

assert_eq!(table.capacity(), 256);
assert_eq!(table.len(), 200);
assert_eq!(table.entity_capacity(), 256);
assert_eq!(table.entity_count(), 200);
}
}

0 comments on commit b0caf70

Please sign in to comment.