1+ //! A map where each key-value pair is assigned a contiguous range of IDs.
12use crate :: { EntityId , EntityRange , EntityVec , EntityMap } ;
23use crate :: map:: Entry ;
34use crate :: id:: { EntityTag , EntityIdU32 } ;
45
6+ /// Indices occupied by a given bundle.
57#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
6- pub enum EntityBundleIndex < I : EntityId > {
8+ pub enum EntityBundleIndices < I : EntityId > {
9+ /// Singular index occupied by a unit-shaped bundle.
710 Single ( I ) ,
11+ /// Range of indices occupied by an array-shaped bundle.
812 Array ( EntityRange < I > ) ,
913}
1014
15+ /// An index within a particular bundle.
1116#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
1217pub enum EntityBundleItemIndex {
18+ /// The bundle is unit-shaped, thus we shall meow no further of indices within it.
1319 Single ,
14- Array { index : usize , total : usize } ,
20+ /// The bundle is array-shaped.
21+ Array {
22+ /// The index within the array.
23+ index : usize ,
24+ /// The total size of the array.
25+ total : usize ,
26+ } ,
1527}
1628
1729struct BundleTag ;
1830impl EntityTag for BundleTag { }
1931type BundleId = EntityIdU32 < BundleTag > ;
2032
21- /// A map where each `(K, V)` pair is assigned a contiguous range of IDs.
33+ /// A map where each key-value pair is assigned a contiguous range of IDs.
34+ ///
35+ /// An `EntityBundleMap` is a collection of *bundles*. Each bundle consists of a key, a value,
36+ /// and a *range* of IDs assigned to it. The amount of IDs assigned to a bundle is determined by
37+ /// its *shape*: an array of a specified size, or just a single unit.
38+ ///
39+ /// Note that we distinguish between a *single unit* and *an array of size one*. This is
40+ /// intentional, as this datastructure is used to implement data models that make this distinction,
41+ /// much like Rust makes a distinction between `u32` and `[u32; 1]`.
2242#[ derive( Clone , Debug , PartialEq , Eq ) ]
2343pub struct EntityBundleMap < I : EntityId , T > {
2444 ids : EntityVec < I , BundleId > ,
25- bundles : EntityMap < BundleId , String , ( EntityBundleIndex < I > , T ) > ,
45+ bundles : EntityMap < BundleId , String , ( EntityBundleIndices < I > , T ) > ,
2646}
2747
2848impl < I : EntityId , T > EntityBundleMap < I , T > {
@@ -33,6 +53,7 @@ impl<I: EntityId, T> EntityBundleMap<I, T> {
3353 }
3454 }
3555
56+ /// Returns the number of allocated IDs, i.e. the total size of all the bundles.
3657 pub fn len ( & self ) -> usize {
3758 self . ids . len ( )
3859 }
@@ -45,12 +66,13 @@ impl<I: EntityId, T> EntityBundleMap<I, T> {
4566 self . ids . ids ( )
4667 }
4768
48- pub fn get ( & self , key : & str ) -> Option < ( EntityBundleIndex < I > , & T ) > {
69+ /// Retrieve a bundle by its key.
70+ pub fn get ( & self , key : & str ) -> Option < ( EntityBundleIndices < I > , & T ) > {
4971 let ( _, ( idx, val) ) = self . bundles . get ( key) ?;
5072 Some ( ( * idx, val) )
5173 }
5274
53- pub fn get_mut ( & mut self , key : & str ) -> Option < ( EntityBundleIndex < I > , & mut T ) > {
75+ pub fn get_mut ( & mut self , key : & str ) -> Option < ( EntityBundleIndices < I > , & mut T ) > {
5476 let ( _, ( idx, val) ) = self . bundles . get_mut ( key) ?;
5577 Some ( ( * idx, val) )
5678 }
@@ -59,16 +81,18 @@ impl<I: EntityId, T> EntityBundleMap<I, T> {
5981 self . bundles . contains_key ( key)
6082 }
6183
84+ /// Given an ID, returns the key of the bundle which owns that ID, as well as the particular
85+ /// position within the bundle that corresponds to the ID.
6286 pub fn key ( & self , id : I ) -> ( & str , EntityBundleItemIndex ) {
6387 let idx = self . ids [ id] ;
6488 let key = self . bundles . key ( idx) ;
6589 let ( bidx, _) = self . bundles [ idx] ;
6690 match bidx {
67- EntityBundleIndex :: Single ( sid) => {
91+ EntityBundleIndices :: Single ( sid) => {
6892 assert_eq ! ( id, sid) ;
6993 ( key, EntityBundleItemIndex :: Single )
7094 }
71- EntityBundleIndex :: Array ( range) => (
95+ EntityBundleIndices :: Array ( range) => (
7296 key,
7397 EntityBundleItemIndex :: Array {
7498 index : range. index_of ( id) . unwrap ( ) ,
@@ -78,17 +102,19 @@ impl<I: EntityId, T> EntityBundleMap<I, T> {
78102 }
79103 }
80104
105+ /// Insert a unit-shaped bundle.
81106 pub fn insert ( & mut self , name : String , value : T ) -> Option < I > {
82107 match self . bundles . entry ( name) {
83108 Entry :: Occupied ( _) => None ,
84109 Entry :: Vacant ( e) => {
85110 let id = self . ids . push ( e. index ( ) ) ;
86- e. insert ( ( EntityBundleIndex :: Single ( id) , value) ) ;
111+ e. insert ( ( EntityBundleIndices :: Single ( id) , value) ) ;
87112 Some ( id)
88113 }
89114 }
90115 }
91116
117+ /// Insert an array-shaped bundle.
92118 pub fn insert_array ( & mut self , name : String , num : usize , value : T ) -> Option < EntityRange < I > > {
93119 match self . bundles . entry ( name) {
94120 Entry :: Occupied ( _) => None ,
@@ -98,7 +124,7 @@ impl<I: EntityId, T> EntityBundleMap<I, T> {
98124 for _ in 0 ..num {
99125 self . ids . push ( e. index ( ) ) ;
100126 }
101- e. insert ( ( EntityBundleIndex :: Array ( range) , value) ) ;
127+ e. insert ( ( EntityBundleIndices :: Array ( range) , value) ) ;
102128 Some ( range)
103129 }
104130 }
@@ -111,19 +137,19 @@ impl<I: EntityId, T> EntityBundleMap<I, T> {
111137 } )
112138 }
113139
114- pub fn bundles ( & self ) -> impl Iterator < Item = ( EntityBundleIndex < I > , & str , & T ) > {
140+ pub fn bundles ( & self ) -> impl Iterator < Item = ( EntityBundleIndices < I > , & str , & T ) > {
115141 self . bundles
116142 . iter ( )
117143 . map ( |( _, k, ( i, v) ) | ( * i, k. as_str ( ) , v) )
118144 }
119145
120- pub fn bundles_mut ( & mut self ) -> impl Iterator < Item = ( EntityBundleIndex < I > , & str , & mut T ) > {
146+ pub fn bundles_mut ( & mut self ) -> impl Iterator < Item = ( EntityBundleIndices < I > , & str , & mut T ) > {
121147 self . bundles
122148 . iter_mut ( )
123149 . map ( |( _, k, ( i, v) ) | ( * i, k. as_str ( ) , v) )
124150 }
125151
126- pub fn into_bundles ( self ) -> impl Iterator < Item = ( EntityBundleIndex < I > , String , T ) > {
152+ pub fn into_bundles ( self ) -> impl Iterator < Item = ( EntityBundleIndices < I > , String , T ) > {
127153 self . bundles . into_iter ( ) . map ( |( _, k, ( i, v) ) | ( i, k, v) )
128154 }
129155}
0 commit comments