// Header file: mystringlist. h
//////////////////////////////////////// //////////////////////
// 1. Description: Object List class-index string + Object Pointer
// 2. Main functions:
//
// You can obtain the index based on the index string's Active Object Pointer value.
// Flexible addition and Deletion
//
//
//////////////////////////////////////// //////////////////////
# Ifndef define_cstringlist _
# Define define_cstringlist _
Struct listitem
{
Char * key; // index string
Int Len; // number of bytes
Lpvoid OBJ; // object pointer
Int keyindex; // index in the index group
Int objindex; // index in the object group
Int orgindex; // index in the original group
Listitem ()
{
Key = NULL;
Len = 0;
OBJ = NULL;
Keyindex =-1;
Orgindex =-1;
Objindex =-1;
}
};
Typedef listitem * plistitem;
Class cmystringlist: Public cobject
{
PRIVATE:
Cobarray lstkey;
Cobarray lstorg;
Cobarray lstobj;
Void insert_keylst (listitem * P, int lowindex, int highindex );
Void insert_objlst (listitem * P, int lowindex, int highindex );
Int getkeyindex (char * P, int lowindex, int highindex );
Int getobjindex (lpvoid OBJ, int lowindex, int highindex );
Public:
~ Cmystringlist ();
Void add (cstring & Key, lpvoid OBJ );
Void add (lpvoid OBJ );
Void add (cstring & Key );
Void insertat (INT index, cstring & Key, lpvoid OBJ );
Void insertat (INT index, lpvoid OBJ );
Void insertat (INT index, cstring & Key );
Int indexof (cstring & Key );
Int indexof (lpvoid OBJ );
Void Delete (cstring & Key );
Void Delete (lpvoid OBJ );
Void Delete (INT index );
Int getsize ();
Lpvoid elementat_obj (INT index );
Lpvoid elementat_key (INT index );
Void removeall ();
};
# Endif
// CPP file: mystringlist. cpp
//////////////////////////////////////// /////////
//
// VC ++ implements the Object List Index string: Object Pointer
//
//////////////////////////////////////// /////////
# Include "stdafx. H"
# Include "mystringlist. H"
Void cstringtopchar (cstring & STR, char ** P, Int & Len)
{
Len = Str. getlength () + 1;
* P = new char [Len];
: Zeromemory (* P, Len );
Strcpy (* P, STR );
};
Void cmystringlist: insert_keylst (listitem * P, int lowindex, int highindex)
{
Plistitem pcomp;
If (lstkey. getsize () = 0)
{
Lstkey. Add (cobject *) P );
P-> keyindex = 0;
Return;
}
If (lowindex = highindex)
{
Pcomp = (plistitem) lstkey. elementat (lowindex );
If (strcmp (p-> key, pcomp-> key) <0)
{
Lstkey. insertat (lowindex, (cobject *) P );
P-> keyindex = lowindex;
}
Else
{
Int iinsert = lowindex + 1;
If (iinsert> = lstkey. getsize ())
Lstkey. Add (cobject *) P );
Else
Lstkey. insertat (iinsert, (cobject *) P );
P-> keyindex = iinsert;
}
Return;
}
// Binary comparison
Int imiddle = (lowindex + highindex)/2;
Imiddle = (imiddle <0 )? 0: imiddle;
Pcomp = (plistitem) lstkey. elementat (imiddle );
If (strcmp (p-> key, pcomp-> key) <0)
Insert_keylst (p, lowindex, (iMiddle-1) <0? 0 :( iMiddle-1 )));
Else
Insert_keylst (p, imiddle + 1, highindex );
}
Void cmystringlist: insert_objlst (listitem * P, int lowindex, int highindex)
{
Plistitem pcomp;
If (lstobj. getsize () = 0)
{
Lstobj. Add (cobject *) P );
P-> objindex = 0;
Return;
}
If (lowindex = highindex)
{
Pcomp = (plistitem) lstobj. elementat (lowindex );
If (INT) P-> OBJ <(INT) pcomp-> OBJ)
{
Lstobj. insertat (lowindex, (cobject *) P );
P-> objindex = lowindex;
}
Else
{
Int iinsert = lowindex + 1;
If (iinsert> = lstobj. getsize ())
Lstobj. Add (cobject *) P );
Else
Lstobj. insertat (iinsert, (cobject *) P );
P-> objindex = iinsert;
}
Return;
}
// Binary comparison
Int imiddle = (lowindex + highindex)/2;
Imiddle = (imiddle <0 )? 0: imiddle;
Pcomp = (plistitem) lstobj. elementat (imiddle );
If (INT) P-> OBJ <(INT) pcomp-> OBJ)
Insert_objlst (p, lowindex, (iMiddle-1) <0? 0 :( iMiddle-1 )));
Else
Insert_objlst (p, imiddle + 1, highindex );
}
Cmystringlist ::~ Cmystringlist ()
{
Removeall ();
}
Void cmystringlist: insertat (INT index, lpvoid OBJ)
{
Cstring c = "";
Insertat (index, C, OBJ );
}
Void cmystringlist: insertat (INT index, cstring & Key)
{
Insertat (index, key, null );
}
Void cmystringlist: insertat (INT index, cstring & Key, lpvoid OBJ)
{
Cstring S;
Plistitem P = new listitem ();
Cstringtopchar (Key, & P-> key, p-> Len );
P-> OBJ = OBJ;
P-> orgindex = index;
Lstorg. insertat (index, (cobject *) P );
Insert_keylst (p, 0, lstkey. getsize ()-1 );
Insert_objlst (p, 0, lstobj. getsize ()-1 );
// Adjust the index numbers of each list
Int Ikey = p-> keyindex;
Int iobj = p-> objindex;
For (Int J = Ikey + 1; j <lstkey. getsize (); j ++)
{
P = (plistitem) lstkey. elementat (j );
P-> keyindex = p-> keyindex + 1;
}
For (int K = iobj + 1; k <lstobj. getsize (); k ++)
{
P = (plistitem) lstobj. elementat (k );
P-> objindex = p-> objindex + 1;
}
For (INT I = index + 1; I <lstorg. getsize (); I ++)
{
P = (plistitem) lstorg. elementat (I );
P-> orgindex = p-> orgindex + 1;
}
}
Void cmystringlist: add (cstring & Key, lpvoid OBJ)
{
Cstring S;
Plistitem P = new listitem ();
Cstringtopchar (Key, & P-> key, p-> Len );
P-> OBJ = OBJ;
P-> orgindex = lstorg. getsize ();
Lstorg. Add (cobject *) P );
Insert_keylst (p, 0, lstkey. getsize ()-1 );
Insert_objlst (p, 0, lstobj. getsize ()-1 );
// Adjust the index numbers of each list
Int Ikey = p-> keyindex;
Int iobj = p-> objindex;
For (Int J = Ikey + 1; j <lstkey. getsize (); j ++)
{
P = (plistitem) lstkey. elementat (j );
P-> keyindex = p-> keyindex + 1;
}
For (int K = iobj + 1; k <lstobj. getsize (); k ++)
{
P = (plistitem) lstobj. elementat (k );
P-> objindex = p-> objindex + 1;
}
}
Void cmystringlist: add (lpvoid OBJ)
{
Cstring c = "";
Add (C, OBJ );
}
Void cmystringlist: add (cstring & Key)
{
Add (Key, null );
}
Int cmystringlist: getobjindex (lpvoid OBJ, int lowindex, int highindex)
{
Plistitem pcomp;
If (lowindex = highindex)
{
Pcomp = (plistitem) lstobj. elementat (lowindex );
If (INT (OBJ) = (INT) pcomp-> OBJ)
Return pcomp-> orgindex;
Else
Return-1;
}
// Binary comparison
Int imiddle = (lowindex + highindex)/2;
Imiddle = (imiddle <0 )? 0: imiddle;
Pcomp = (plistitem) lstobj. elementat (imiddle );
If (INT (OBJ) = (INT) pcomp-> OBJ) return pcomp-> orgindex;
If (INT (OBJ) <(INT) pcomp-> OBJ)
Return getobjindex (OBJ, lowindex, (iMiddle-1) <0? 0 :( iMiddle-1 )));
Else
Return getobjindex (OBJ, imiddle + 1, highindex );
}
Int cmystringlist: getkeyindex (char * P, int lowindex, int highindex)
{
Plistitem pcomp;
If (lowindex = highindex)
{
Pcomp = (plistitem) lstkey. elementat (lowindex );
If (strcmp (p, pcomp-> key) = 0)
Return pcomp-> orgindex;
Else
Return-1;
}
// Binary comparison
Int imiddle = (lowindex + highindex)/2;
Imiddle = (imiddle <0 )? 0: imiddle;
Pcomp = (plistitem) lstkey. elementat (imiddle );
If (strcmp (p, pcomp-> key) = 0) return pcomp-> orgindex;
If (strcmp (p, pcomp-> key) <0)
Return getkeyindex (p, lowindex, (iMiddle-1) <0? 0 :( iMiddle-1 )));
Else
Return getkeyindex (p, imiddle + 1, highindex );
}
Int cmystringlist: indexof (cstring & Key)
{
If (lstkey. getsize () = 0) Return-1;
Char * P;
Int Len;
Cstringtopchar (Key, & P, Len );
Int Index = getkeyindex (p, 0, lstkey. getsize ()-1 );
Delete [Len] P;
Return Index;
}
Int cmystringlist: indexof (lpvoid OBJ)
{
If (lstobj. getsize () = 0) Return-1;
Return getobjindex (OBJ, 0, lstkey. getsize ()-1 );
}
Void cmystringlist: delete (cstring & Key)
{
Int I = indexof (key );
If (I! =-1) Delete (I );
}
Void cmystringlist: delete (lpvoid OBJ)
{
Int I = indexof (OBJ );
If (I! =-1) Delete (I );
}
Void cmystringlist: delete (INT index)
{
If (index <0) | (index> lstorg. getsize ()-1) return;
Int Ikey;
Int iobj;
Plistitem P = (plistitem) lstorg. elementat (INDEX );
// Adjust the index numbers of each list
Ikey = p-> keyindex;
Iobj = p-> objindex;
For (Int J = Ikey + 1; j <lstkey. getsize (); j ++)
{
P = (plistitem) lstkey. elementat (j );
P-> keyindex = p-> keyindex-1;
}
For (int K = iobj + 1; k <lstobj. getsize (); k ++)
{
P = (plistitem) lstobj. elementat (k );
P-> objindex = p-> objindex-1;
}
For (INT I = index + 1; I <lstorg. getsize (); I ++)
{
P = (plistitem) lstorg. elementat (I );
P-> orgindex = p-> orgindex-1;
}
// Delete a project
P = (plistitem) lstorg. elementat (INDEX );
Lstorg. removeat (p-> orgindex );
Lstkey. removeat (p-> keyindex );
Lstobj. removeat (p-> objindex );
Delete [p-> Len] p-> key;
Delete P;
}
Int cmystringlist: getsize ()
{
Return lstorg. getsize ();
}
Lpvoid cmystringlist: elementat_obj (INT index)
{
Listitem * P;
P = (listitem *) lstorg. elementat (INDEX );
Return p-> OBJ;
}
Lpvoid cmystringlist: elementat_key (INT index)
{
Listitem * P;
P = (listitem *) lstorg. elementat (INDEX );
Return p-> key;
}
Void cmystringlist: removeall ()
{
Listitem * P;
For (INT I = 0; I <lstorg. getsize (); I ++)
{
P = (listitem *) lstorg. elementat (I );
Delete [p-> Len] p-> key;
Delete P;
}
Lstorg. removeall ();
Lstobj. removeall ();
Lstkey. removeall ();
}