use crate::array_store::ArrayStore;
use crate::entity_table::EntityMeta;
use crate::object::{EntityId, PsObject};
pub struct DualArrayStore {
pub global: ArrayStore,
pub local: ArrayStore,
}
impl DualArrayStore {
pub fn new() -> Self {
Self {
global: ArrayStore::new(),
local: ArrayStore::new(),
}
}
#[inline]
fn store(&self, entity: EntityId) -> &ArrayStore {
if entity.is_global() {
&self.global
} else {
&self.local
}
}
#[inline]
fn store_mut(&mut self, entity: EntityId) -> &mut ArrayStore {
if entity.is_global() {
&mut self.global
} else {
&mut self.local
}
}
pub fn allocate(&mut self, len: usize) -> EntityId {
self.local.allocate(len)
}
pub fn allocate_from(&mut self, items: &[PsObject]) -> EntityId {
self.local.allocate_from(items)
}
pub fn allocate_from_with(
&mut self,
items: &[PsObject],
save_level: u16,
global: bool,
created_after_save: u32,
) -> EntityId {
if global {
self.global
.allocate_from_with(items, save_level, global, created_after_save)
} else {
self.local
.allocate_from_with(items, save_level, global, created_after_save)
}
}
pub fn allocate_with(
&mut self,
len: usize,
save_level: u16,
global: bool,
created_after_save: u32,
) -> EntityId {
if global {
self.global
.allocate_with(len, save_level, global, created_after_save)
} else {
self.local
.allocate_with(len, save_level, global, created_after_save)
}
}
pub fn get(&self, entity: EntityId, start: u32, len: u32) -> &[PsObject] {
self.store(entity).get(entity, start, len)
}
pub fn get_mut(&mut self, entity: EntityId, start: u32, len: u32) -> &mut [PsObject] {
self.store_mut(entity).get_mut(entity, start, len)
}
#[inline]
pub fn get_element(&self, entity: EntityId, index: u32) -> PsObject {
self.store(entity).get_element(entity, index)
}
pub fn set_element(&mut self, entity: EntityId, index: u32, obj: PsObject) {
self.store_mut(entity).set_element(entity, index, obj);
}
pub fn cow_copy(&mut self, entity: EntityId) -> EntityId {
debug_assert!(!entity.is_global(), "COW copy on global entity");
self.local.cow_copy(entity)
}
pub fn swap_offsets(&mut self, a: EntityId, b: EntityId) {
debug_assert!(
!a.is_global() && !b.is_global(),
"swap_offsets on global entity"
);
self.local.swap_offsets(a, b);
}
pub fn entity_meta(&self, entity: EntityId) -> &EntityMeta {
self.store(entity).entities.get(entity)
}
pub fn entity_meta_mut(&mut self, entity: EntityId) -> &mut EntityMeta {
self.store_mut(entity).entities.get_mut(entity)
}
pub fn entity_count(&self) -> usize {
self.local.entities.len() + self.global.entities.len()
}
pub fn reset_local(&mut self) {
self.local = ArrayStore::new();
}
}
impl Default for DualArrayStore {
fn default() -> Self {
Self::new()
}
}