?
Solved

C++ STL MAP and complex keys

Posted on 2006-03-23
9
Medium Priority
?
561 Views
Last Modified: 2013-11-20
I have variable key types which need to index to a record pointer. Within a map the type of key will be the same. So either i handle keys generically or have multiple map classes. However as one of these is a byte array i am not sure how to handle this.

Key types are :
ulong, string (case and case insensitive) (length varies), byte array (essentially a string but a binary string, it can have any chars in it) length fixed.

I have handled the the numeric case and string before (not the case insensitive but that is just forcinf the key to always be lowercase) but nor an arbitary number of binary bytes.

I tried using a class as the key via herbet schildt's STL book but can get the operators you need to do the < and == comapres to compile let alone work.

>> Suggestions, code samples would be appreaciated particular of a key which is a class or a binary stream

Thanks
0
Comment
Question by:greg_roberts
  • 4
  • 3
  • 2
9 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16269252
Give some examples
0
 
LVL 86

Expert Comment

by:jkr
ID: 16270846
>>but can get the operators you need to do the < and == comapres to compile let alone work.

You need global operators like

class BLOB;

bool operator < (const BLOB& l, const BLOB& r);
bool operator == (const BLOB& l, const BLOB& r);

class BLOB

friend bool operator < (const BLOB& l, const BLOB& r);
friend bool operator == (const BLOB& l, const BLOB& r);

public:

//...

private:

BYTE m_strBinary[255];
};

bool operator < (const BLOB& l, const BLOB& r) {

    return 0 > memcmp(l.m_strBinary,r.m_strBinary,255);
}

bool operator == (const BLOB& l, const BLOB& r) {

    return 0 == memcmp(l.m_strBinary,r.m_strBinary,255);
}
0
 
LVL 86

Expert Comment

by:jkr
ID: 16270850
Ooops, there's a parenthesis missing, that should be

class BLOB {

friend bool operator < (const BLOB& l, const BLOB& r);
friend bool operator == (const BLOB& l, const BLOB& r);

public:

//...

private:

BYTE m_strBinary[255];
};
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:greg_roberts
ID: 16274282
Thanks, this is close to what i have so i will give it another try and post a result and your points soon.
0
 

Author Comment

by:greg_roberts
ID: 16276354


My code, my tabs are 4 chars, this form system seems to use 8 , sorry

>> It would be good to resolve this however, in my case i ned to insert keys to records,
find them quickly and delete them. I don't technically need to sort them or iterate through them sorted ...

So are these operators needed by STL to build up fast accessors or are they only needed for sort operations ? i.e. could i survive without them ?

I included ythe code below , thanks !!

Compiler errors

Error      1      error C2804: binary 'operator <' has too many parameters      d:\Dev\Panama\Source\api\common\ct_symmapping.cpp      91      
Error      2      error C2333: 'SymKeyClass::operator <' : error in function declaration; skipping function body      d:\Dev\Panama\Source\api\common\ct_symmapping.cpp      91      
Error      3      error C2804: binary 'operator ==' has too many parameters      d:\Dev\Panama\Source\api\common\ct_symmapping.cpp      114      
Error      4      error C2333: 'SymKeyClass::operator ==' : error in function declaration; skipping function body      d:\Dev\Panama\Source\api\common\ct_symmapping.cpp      114      


class SymKeyClass;

typedef std::map<SymKeyClass, void*> SYM_KEY_PAIR;

struct SymKeyObj
{
      union
      {
            unsigned long   ulong;
            unsigned char* uText;
      } sKey;
      unsigned short      sKeyLength;
      char            sKeyType;
};


class SymKeyClass
{
public:

      SymKeyClass(SymKeyObj* pKey)
      {
                               // Fill out class based on key type
            m_Key.sKeyType = pKey->sKeyType;

            if (IsSymTypeVarLength(m_Key.sKeyType))
            {
                  m_Key.sKeyLength = (unsigned short)strlen((const char*)pKey->sKey.uText);
            }
            else if (IsSymTypeFixedLength(m_Key.sKeyType))
            {
                  m_Key.sKeyLength = pKey->sKeyLength;
            }
            else
            {
                  m_Key.sKeyLength = sizeof(unsigned long);
            }

            if (IsSymTypePointer(m_Key.sKeyType))
            {
                  m_Key.sKey.uText = new unsigned char [m_Key.sKeyLength];
                  memcpy(m_Key.sKey.uText, pKey->sKey.uText, m_Key.sKeyLength);
            }
            else
            {
                  m_Key.sKey.ulong = pKey->sKey.ulong;
            }
      }

      ~SymKeyClass()
      {
            // Only if the type was a buffer pointer do we deallocate
            if (IsSymTypePointer(m_Key.sKeyType))
            {
                  delete m_Key.sKey.uText;
                  m_Key.sKey.uText = NULL;
            }
      }

public:
      SymKeyObj* get()
      {
            return &m_Key;
      }

      // Is one less than the other
      bool      operator< (SymKeyClass& A, SymKeyClass& B)
      {
            bool bLess = false;

            if (A->m_Key.sKeyType == B->m_Key.sKeyType)
            {
                  if (A->m_Key.sKeyLength == B->m_Key.sKeyLength)
                  {
                        if (IsSymTypePointer(A->m_Key.sKeyType))
                        {
                              bLess = memcmp(A->m_Key.sKey.uText, B->m_Key.sKey.uText, A->m_Key.sKeyLength) < 0;
                        }
                        else
                        {
                              bLess = (A->m_Key.sKey.ulong < B->m_Key.sKey.ulong);
                        }
                  }
            }

            return bLess;
      }

      // Are these keys equal
      bool      operator==(SymKeyObj& A, SymKeyObj& B)
      {
            bool bEqual = false;

            if (A->sKeyType == B->sKeyType)
            {
                  if (A->sKeyLength == B->sKeyLength)
                  {
                        if (IsSymTypePointer(A->sKeyType))
                        {
                              bEqual = memcmp(A->sKey.uText, B->sKey.uText, A->sKeyLength);
                        }
                        else
                        {
                              bEqual = (A->sKey.ulong == B->sKey->ulong);
                        }
                  }
            }

            return bEqual;
      }

private:
      SymKeyObj m_Key;
};
0
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 750 total points
ID: 16276924
Corrected code:

These three functions are not defined.

      bool IsSymTypeVarLength(char){return true;}
      bool IsSymTypeFixedLength(char){return true;}
      bool IsSymTypePointer(char){return true;}

class SymKeyClass;

typedef std::map<SymKeyClass, void*> SYM_KEY_PAIR;

struct SymKeyObj
{
     union
     {
          unsigned long   ulong;
          unsigned char* uText;
     } sKey;
     unsigned short     sKeyLength;
     char          sKeyType;
};


class SymKeyClass
{
public:
     SymKeyClass(SymKeyObj* pKey)
     {
                               // Fill out class based on key type
          m_Key.sKeyType = pKey->sKeyType;

          if (IsSymTypeVarLength(m_Key.sKeyType))
          {
               m_Key.sKeyLength = (unsigned short)strlen((const char*)pKey->sKey.uText);
          }
          else if (IsSymTypeFixedLength(m_Key.sKeyType))
          {
               m_Key.sKeyLength = pKey->sKeyLength;
          }
          else
          {
               m_Key.sKeyLength = sizeof(unsigned long);
          }

          if (IsSymTypePointer(m_Key.sKeyType))
          {
               m_Key.sKey.uText = new unsigned char [m_Key.sKeyLength];
               memcpy(m_Key.sKey.uText, pKey->sKey.uText, m_Key.sKeyLength);
          }
          else
          {
               m_Key.sKey.ulong = pKey->sKey.ulong;
          }
     }

     ~SymKeyClass()
     {
          // Only if the type was a buffer pointer do we deallocate
          if (IsSymTypePointer(m_Key.sKeyType))
          {
               delete m_Key.sKey.uText;
               m_Key.sKey.uText = NULL;
          }
     }

public:
     SymKeyObj* get()
     {
          return &m_Key;
     }

     // Is one less than the other
     bool operator < (SymKeyClass& B) // < takes one parameter
     {
          bool bLess = false;

          if (m_Key.sKeyType == B.m_Key.sKeyType)
          {
               if (m_Key.sKeyLength == B.m_Key.sKeyLength)
               {
                    if (IsSymTypePointer(m_Key.sKeyType))
                    {
                         bLess = memcmp(m_Key.sKey.uText, B.m_Key.sKey.uText, m_Key.sKeyLength) < 0;
                    }
                    else
                    {
                         bLess = (m_Key.sKey.ulong < B.m_Key.sKey.ulong);
                    }
               }
          }

          return bLess;
     }

     // Are these keys equal
     bool operator==(SymKeyObj& B) // < takes one parameter
     {
          bool bEqual = false;

          if (m_Key.sKeyType == B.sKeyType)
          {
               if (m_Key.sKeyLength == B.sKeyLength)
               {
                    if (IsSymTypePointer(m_Key.sKeyType))
                    {
                         bEqual = memcmp(m_Key.sKey.uText, B.sKey.uText, m_Key.sKeyLength);
                    }
                    else
                    {
                         bEqual = (m_Key.sKey.ulong == B.sKey.ulong);
                    }
               }
          }

          return bEqual;
     }

private:
     SymKeyObj m_Key;
};

0
 

Author Comment

by:greg_roberts
ID: 16278000
Thanks so the example if the STL book was wrong, it showed both items.

>> Can you tell me if these operators are actually needed if you do not need a sorted order, but just want fast key to record lookup ?

Thanks
0
 

Author Comment

by:greg_roberts
ID: 16278009
P.S: Is a class the only way to have a search for a key with X bytes but X is known at the time of the search ?

More points on offer
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16278956
>> P.S: Is a class the only way to have a search for a key with X bytes but X is known at the time of the search ?
No
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question