I have a collection of objects, with certain properties (let say 3 - zone, type, owner) only having a small predetermined possible set of values (like enum).
This is just a simple (javascript) array of gaming cards (think "magic the gathering"), that could be in different zones (deck, hand, grave, play), and they could be owned by different players at different stages of their lifecycle. Also, there is a type (spell, minion, weapon, enchantment) which is unchangeable, but still very important discriminator.
In order to process game, I need to often query my store to get "all spells in hand which cost less than current mana of current player", or "all enemy minions which had not attacked this turn".
Right now I simply use Array.filter
and just filter the all universe with small predicate functions, but I have a feeling this could be made more efficient, if I can at least cut the size of whole collection in the beginning of query processing.
To speed up queries like where type = N
, I considered using a pre-made number of Sets (e.g.: "cardsByType = MINIONS | SPELLS | WEAPONS" or cardsByZone = HAND | DECK | GRAVE).
Also, during program life cycle - some of those properties could be updated - so I was updating those Sets together with object update.
Unfortunately in practice, I need the collection to be ordered, as insertions and removals need to happen in certain positions.
This means that Sets only help me with queries like count where type=n
. Also, during program life cycle - some of those properties could be updated - so I was updating those Sets together with each object's (card) update.
Also, maintaining the consistency between object itself, and two collection (old and new) is very error prone.
Am I missing some known obvious data-structure that could be helpful with such task ? OR no general-purpose data-structure like that exists ?