C++ STL MAP and complex keys

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
greg_robertsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajeev_devinCommented:
Give some examples
jkrCommented:
>>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);
}
jkrCommented:
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];
};
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

greg_robertsAuthor Commented:
Thanks, this is close to what i have so i will give it another try and post a result and your points soon.
greg_robertsAuthor Commented:


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;
};
rajeev_devinCommented:
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;
};

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
greg_robertsAuthor Commented:
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
greg_robertsAuthor Commented:
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
rajeev_devinCommented:
>> 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.