Extended Array Template Like CArray

Environment: Generic. Tested on Windows 2000 and Windows Me

CExtendedArray<TYPE, NUM_SORTFLDS, FLDTYPE>

This template implements an abstract MFC style class and provides additional functionality of sorting the data internally over NUM_SORTFLDS simultaneously. Also, class provides ability to search for a given value over these fields quickly, (using binary search) implemented over the sorted fields. It depends on MFC template classes (afxtempl.h) to provide global functions like ConstructElement etc.... If you do not wish to use MFC at all, uncomment all of the functions from top of the file.

It is an abstract class and client must two CompareElem functions. Both of this functions compare two supplied elements and return negative, zero or positive number depending upon whether first element is "less than", "equal to" or "greater than" second element respectively. Only difference between two functions is that in one version, it provides references to two TYPE elements, whereas the second version, passes reference to type and FLDTYPE value.

TYPE

is a data type of object that the CExtArray template will store a pointer to I have done away with ARG_TYPE present in MFC class and all references to object are either converted to a pointer or reference Also, although you can use any field in TYPE, class really becomes useful, when TYPE is a pointer to structure or a class

NUM_SORTFLDS

is a integer value that defines number of fields within the structure, for which you want ability to search and sort data.

FLTDTYPE

is type of field that can be sorted. Only possible value that can make sense here is "void *" or LPARAM. I deliberated a lot on this and cannot think of any other type to use. Send me your comments if you come up with something better. It is basically a place holder that is used while calling CompareElem function.

Sample usage of CExtendedArray<> is as follows:

class something
{
  public:
      char    ApplId[4];
      char    szApplName[81];
};

typedef something* LPSOMESTR;

class MyArray : public CExtendedArray<LPSOMESTR, 2, LPARAM>
{
  int CompareElem(int nSortFld, 
                  LPSOMESTR& first, 
                  LPSOMESTR& second, 
                  LPARAM lHint)
  {
    int r;
    if (nSortFld == 0)
    {
      r = lstrcmpi(first->szApplName, second->szApplName);
      //std::cerr << "Comparing " << first->szApplName 
      //   << " :with: " << second->szApplName 
      //   << "<->" << r << std::endl;
      return r;
    }
    else if (nSortFld == 1)
    {
      return lstrcmpi(first->ApplId , second->ApplId);
      //std::cerr << "Comparing " << first->ApplId 
      // << " :with: " << second->ApplId 
      // << "<->" << r << std::endl;
      return r;
    }

    return 0;
  };

  int CompareElem(int nSortFld, LPSOMESTR& first, 
                  LPARAM valLookup, LPARAM lHint)
  {
    int r;
    if (nSortFld == 0)
    {
      r = lstrcmpi(first->szApplName, (LPCTSTR)valLookup);
      std::cerr << "Comparing " << first->szApplName 
                << " :with: " << (LPCTSTR)valLookup 
                << "<->" << r << std::endl;
      return r;
    }
    else if (nSortFld == 1)
    {
      r = lstrcmpi(first->ApplId , (LPCTSTR)valLookup);
      std::cerr << "Comparing " << first->ApplId 
                << " :with: " << (LPCTSTR)valLookup 
                << "<->" << r << std::endl;
      return r;
    }

    return 0;
  };
};

void testinit()
{
    MyArray wordArray;
    MyArray wordArray2;
    MyArray wordArray3;

    LPSOMESTR w;

    wordArray.SetSize(3,3);

    for (int i = 0; i < 3; i++)
    {
        w = new something;
        // fill in w
        wordArray[i] = w;
    }

    wordArray.SortData(0, 0); // szApplName

    wordArray.SortData(1, 0); // APPL
    LPSOMESTR retVal = NULL;

    if (wordArray.Lookup(1, (LPARAM)"EXT", retVal, 0))
        std::cout << retVal->szApplName << std::endl;

    retVal = NULL;

    if (wordArray.Lookup(1, (LPARAM)"OTH", retVal, 0))
        std::cout << retVal->szApplName << std::endl;
    else
        std::cout << "Not found" << std::endl;
}

Downloads

Download source - 6 Kb


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds