Thursday, 15 July 2010

C# Looking for a flexible data structure -



C# Looking for a flexible data structure -

i mulling on little issue , appreciate peoples opinions. i'm making c# plugin unity c++ particle simulation library. i'd way represent particles in scheme end user can access info easily. @ moment each particle represented struct so..

public struct particle { public vector3 position; public color _color; public float age; public float weight; public vector3 velocity; public int userdata }

the issue have in cases not not bother retrieving of info available in struct simulation. instance might want retrieve position , colour info each frame improve performance. in case struct have lots of uninitialised fields + taking more memory needs (there 1000's of them btw) cant utilize class hieratchy combination of fields might used in different situations. 1 approach did think of this..

public struct particle { public dictionary<string,object> data; }

creating them looks this..

particle parttest = new particle{data = new dictionary<string,object>()}; parttest.data.add("position",(object)vector3.one); parttest.data.add("weight",(object)1.8745f);

and cast appropriate class when reading them. can have particle just, say, color, weight , position info contained in , nil else. ye think?

i can envision 2 approaches:

a) cannot utilize classes "as combination of fields might used". maybe composition (as opposed inheritance) job: particle have displaybehavior, movementbehavior, etc. , behaviors hold actual properties. fit problem?

b) otherwise, if enumeration of properties of particle can slow (a few microseconds) while retrieval of values of 1 or 2 specific properties must fast, proper solution have property stores: each property have 1 construction holds values of particles. store (particle-id, property-value) information, either in flat array or dictionary. solution used wpf illustration , cache-efficient , memory-savvy.

meanwhile store nil in particle structure, point may consider removing type , store particle identifiers (possibly particleid enum create code more explicit). or maintain type , add together properties manipulate global property stores.

ps: 1 note structs: understand them correctly? in cases using big struct bad thought because involves many memory copies. case when taxation not paid when struct can accessed address, illustration when manipulate through index in array (particles[index].position = ...).

c# data-structures

No comments:

Post a Comment