I have several lists with objects in them. I also have a Big list that contains all elements of some of the smallers lists I at that point deem "active". What I want to achieve is that objects can be moved between lists (which does not change the Big list), and that new elements can be added or removed (which DOES affect the Big list as well).
The background is that I am creating a game/simulation, and the active entities in the "World" should be the sum of all entities in "Loaded Chunks". Whenever I unload a Chunk, I want the entities to disappear from the World as well.
The application of a game (for which I bet there are millions of solutions to this problem) aside, I find the idea of synchronized (sub)lists very interesting to think about.
I have thought about two ways to achieve this:
- Each element has an identifying property which I use to tell what element needs to be deleted form the Big list.
- The lists don't contain the references to the objects, but references to references to the objects. Or something like a datacontainer. That way I can simply edit the content of the datacontainer, and the objects will vanish from all lists that have that datacontainer in it. Upside is flexibility, it could support more than one Big list.
Does anyone else know of a nice way to handle this? Which is best in terms of performance? Which is easier to implement? Which is easiest to maintain? Which is best?
question from:
https://stackoverflow.com/questions/65859967/is-there-a-clever-way-of-removing-an-instance-from-all-lists-with-a-reference-to 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…