Compress EntityHandle
This commit is contained in:
1 parent
d8e3a5b6db
commit
2c1163fc3b
3 files changed
+120
-111
No files matched your search
+28
-6
@@ -182,8 +182,7 @@ impl CardStatistics {
|
||||
#[derive(Copy)]
|
||||
pub struct EntityHandle<E> {
|
||||
pub(crate) id: Id,
|
||||
pub(crate) real: bool,
|
||||
pub(crate) index: u32,
|
||||
index: i32,
|
||||
pub(crate) _phantom: PhantomData<E>,
|
||||
|
||||
}
|
||||
@@ -192,17 +191,40 @@ impl<T> Clone for EntityHandle<T> {
|
||||
fn clone(&self) -> Self {
|
||||
EntityHandle{
|
||||
id: self.id,
|
||||
real: self.real,
|
||||
index: self.index,
|
||||
_phantom: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T> EntityHandle<T> {
|
||||
pub fn new(id: Id, index: usize, real: bool) -> Self {
|
||||
Self {
|
||||
id,
|
||||
index : index as i32 | (!real as i32) << 31,
|
||||
_phantom: Default::default()
|
||||
}
|
||||
}
|
||||
pub fn index(&self) -> usize {
|
||||
(self.index << 1 >> 1) as usize
|
||||
}
|
||||
|
||||
pub fn set_index(&mut self, index: usize) {
|
||||
self.index = self.index >> 31 << 31 | index as i32;
|
||||
}
|
||||
|
||||
pub fn real(&self) -> bool {
|
||||
(self.index >> 31) == 0
|
||||
}
|
||||
|
||||
pub fn set_real(&mut self, value: bool) {
|
||||
self.index = self.index << 1 >> 1 | (!value as i32) << 31;
|
||||
}
|
||||
}
|
||||
impl<T> Eq for EntityHandle<T> {}
|
||||
|
||||
impl<T> PartialEq<Self> for EntityHandle<T> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.real == other.real && self.id == other.id
|
||||
self.real() == other.real() && self.id == other.id
|
||||
}
|
||||
}
|
||||
impl<T> PartialOrd<Self> for EntityHandle<T> {
|
||||
@@ -213,11 +235,11 @@ impl<T> PartialOrd<Self> for EntityHandle<T> {
|
||||
|
||||
impl<T> Ord for EntityHandle<T> {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
if self.real && !other.real {
|
||||
if self.real() && !other.real() {
|
||||
return Ordering::Less
|
||||
}
|
||||
|
||||
if !self.real && other.real{
|
||||
if !self.real() && other.real(){
|
||||
return Ordering::Greater
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user