• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

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
0
greg_roberts
Asked:
greg_roberts
  • 4
  • 3
  • 2
1 Solution
 
rajeev_devinCommented:
Give some examples
0
 
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);
}
0
 
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];
};
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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

0
 
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
0
 
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
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now