We help IT Professionals succeed at work.

C++ STL MAP and complex keys

greg_roberts
greg_roberts asked
on
Medium Priority
595 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
Comment
Watch Question

Give some examples
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>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);
}
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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];
};

Author

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

Author

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

Author

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
>> 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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.