Generic List and n-criteria sorting
-
I need to add nodes to a list regardless of its structure...I don't know how to do that since when the pointer is declared, it points to a certain data type, which needs to be generic... Moreover I need a way to sort that given list on many criteria, where maximum n is the number of fields in the structure, and those may be numeric or alphanumeric. (what I did before was if (crit1a = crit1b)&&(crit2a>crit2b) {switch...}, a more elaborate version of that, but my problem is mainly the generic issue) ............I'm so helpless..... :) Thx in advance!!!
-
I need to add nodes to a list regardless of its structure...I don't know how to do that since when the pointer is declared, it points to a certain data type, which needs to be generic... Moreover I need a way to sort that given list on many criteria, where maximum n is the number of fields in the structure, and those may be numeric or alphanumeric. (what I did before was if (crit1a = crit1b)&&(crit2a>crit2b) {switch...}, a more elaborate version of that, but my problem is mainly the generic issue) ............I'm so helpless..... :) Thx in advance!!!
Mariano Lopez-Gappa wrote:
since when the pointer is declared, it points to a certain data type, which needs to be generic...
Just make it a void pointer. When it comes time to dereference those items in the list, however, you'll need to know what type each points to. Consider:
void main( void )
{
int Int = 5;
double Double = 12.34;
void *pVoid;pVoid = ∬ printf("%d\\n", \*((int \*) pVoid)); pVoid = &Double; printf("%f\\n", \*((double \*) pVoid));
}
"Take only what you need and leave the land as you found it." - Native American Proverb
-
I need to add nodes to a list regardless of its structure...I don't know how to do that since when the pointer is declared, it points to a certain data type, which needs to be generic... Moreover I need a way to sort that given list on many criteria, where maximum n is the number of fields in the structure, and those may be numeric or alphanumeric. (what I did before was if (crit1a = crit1b)&&(crit2a>crit2b) {switch...}, a more elaborate version of that, but my problem is mainly the generic issue) ............I'm so helpless..... :) Thx in advance!!!
Hi, I think that each "certain data type" must have a common base class, which could be as simple as class CBase { ... Constructor/Destructor... UINT m_type; // The class type. UINT GetType () { return m_type; }; }; Suppose you had various other classes CA, CB, CC, CD derived from CBase Then the comparison routine for the sort could switch in the following way int Compare( const void *e1, const void *e2 ) { // handle element e1 switch ( ((CBase *)e1)->GetType() ) { case TYPEA: CA *A = (CA *)e1; --- set some criteria based on CA info --- break; case ... etc. // handle element e2 switch ( ((CBase *)e2)->GetType() ) { case TYPEA: CA *A = (CA *)e1; --- set some criteria based on CA info --- break; case ... etc. // Now make the comparison based on information generated in the // last 2 steps. --- comparison code --- } Id be interested to hear if anybody can think of a more general/elegant way to do it! Cheers, Dave
-
Mariano Lopez-Gappa wrote:
since when the pointer is declared, it points to a certain data type, which needs to be generic...
Just make it a void pointer. When it comes time to dereference those items in the list, however, you'll need to know what type each points to. Consider:
void main( void )
{
int Int = 5;
double Double = 12.34;
void *pVoid;pVoid = ∬ printf("%d\\n", \*((int \*) pVoid)); pVoid = &Double; printf("%f\\n", \*((double \*) pVoid));
}
"Take only what you need and leave the land as you found it." - Native American Proverb
thx for the help. it's been really useful. i'm a step closer to the solution but theres still an issue: the actual matter about the generic list is that, in this program, the user gets to decide what "fields" of a database get on the list. so for each user, or even for the same user changing the configuration, I get a different kind of list. so I gotta work on some kind of generic way to use those list, otherwise the number of field combinations are virtually infinite! another thing...I haven't been able to figure out a way to apply the n-criteria sorting, the numeric-alphanumeric thing is easy, but first of all I don't know WHICH or HOW MANY fields I have, and also it gets confusing how to apply the sorting in a generic way... this problem looks pretty complex...hope someone gets it right! thx in advance!! :D
-
thx for the help. it's been really useful. i'm a step closer to the solution but theres still an issue: the actual matter about the generic list is that, in this program, the user gets to decide what "fields" of a database get on the list. so for each user, or even for the same user changing the configuration, I get a different kind of list. so I gotta work on some kind of generic way to use those list, otherwise the number of field combinations are virtually infinite! another thing...I haven't been able to figure out a way to apply the n-criteria sorting, the numeric-alphanumeric thing is easy, but first of all I don't know WHICH or HOW MANY fields I have, and also it gets confusing how to apply the sorting in a generic way... this problem looks pretty complex...hope someone gets it right! thx in advance!! :D
Mariano Lopez-Gappa wrote:
the user gets to decide what "fields" of a database get on the list.
This is probably a silly question but why not use the database to do the sorting for you? I do this all the time. I create a query based on what fields the user wants. Too slow or possibly you are making your own database?? John
-
Mariano Lopez-Gappa wrote:
the user gets to decide what "fields" of a database get on the list.
This is probably a silly question but why not use the database to do the sorting for you? I do this all the time. I create a query based on what fields the user wants. Too slow or possibly you are making your own database?? John
I'm not going to use MFC, nor any database as I need my program to work on a 486 dx2. Also the purpose of the list is to display multisorted information on a grid. Thx anyway! :D keep talking ppl plz!