2012-03-19 14:53:44 +08:00
|
|
|
#include "CCDictionary.h"
|
|
|
|
#include "CCString.h"
|
|
|
|
#include "CCInteger.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictionary::CCDictionary()
|
2012-03-19 14:53:44 +08:00
|
|
|
: m_pElements(NULL)
|
|
|
|
, m_eDictType(kCCDictUnknown)
|
|
|
|
, m_eOldDictType(kCCDictUnknown)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictionary::~CCDictionary()
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
|
|
|
removeAllObjects();
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
unsigned int CCDictionary::count()
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
|
|
|
return HASH_COUNT(m_pElements);
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCArray* CCDictionary::allKeys()
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
|
|
|
int iKeyCount = this->count();
|
|
|
|
if (iKeyCount <= 0) return NULL;
|
|
|
|
CCArray* pArray = CCArray::arrayWithCapacity(iKeyCount);
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictElement *pElement, *tmp;
|
2012-03-19 14:53:44 +08:00
|
|
|
if (m_eDictType == kCCDictStr)
|
|
|
|
{
|
|
|
|
HASH_ITER(hh, m_pElements, pElement, tmp)
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCString* pOneKey = new CCString(pElement->m_szKey);
|
2012-03-19 14:53:44 +08:00
|
|
|
pOneKey->autorelease();
|
|
|
|
pArray->addObject(pOneKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_eDictType == kCCDictInt)
|
|
|
|
{
|
|
|
|
HASH_ITER(hh, m_pElements, pElement, tmp)
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCInteger* pOneKey = new CCInteger(pElement->m_iKey);
|
2012-03-19 14:53:44 +08:00
|
|
|
pOneKey->autorelease();
|
|
|
|
pArray->addObject(pOneKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pArray;
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCArray* CCDictionary::allKeysForObject(CCObject* object)
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
|
|
|
int iKeyCount = this->count();
|
|
|
|
if (iKeyCount <= 0) return NULL;
|
|
|
|
CCArray* pArray = CCArray::array();
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictElement *pElement, *tmp;
|
2012-03-19 14:53:44 +08:00
|
|
|
|
|
|
|
if (m_eDictType == kCCDictStr)
|
|
|
|
{
|
|
|
|
HASH_ITER(hh, m_pElements, pElement, tmp)
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
if (object == pElement->m_pObject)
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCString* pOneKey = new CCString(pElement->m_szKey);
|
2012-03-19 14:53:44 +08:00
|
|
|
pOneKey->autorelease();
|
|
|
|
pArray->addObject(pOneKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_eDictType == kCCDictInt)
|
|
|
|
{
|
|
|
|
HASH_ITER(hh, m_pElements, pElement, tmp)
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
if (object == pElement->m_pObject)
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCInteger* pOneKey = new CCInteger(pElement->m_iKey);
|
2012-03-19 14:53:44 +08:00
|
|
|
pOneKey->autorelease();
|
|
|
|
pArray->addObject(pOneKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pArray;
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCObject* CCDictionary::objectForKey(const char* key)
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
if (m_eDictType == kCCDictUnknown && m_eDictType == kCCDictUnknown) return NULL;
|
2012-03-19 14:53:44 +08:00
|
|
|
CCAssert(m_eDictType == kCCDictStr, "this dictionary does not use string as key.");
|
|
|
|
|
|
|
|
CCObject* pRetObject = NULL;
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictElement *pElement = NULL;
|
2012-03-19 14:53:44 +08:00
|
|
|
HASH_FIND_STR(m_pElements,key, pElement);
|
|
|
|
if (pElement != NULL)
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
pRetObject = pElement->m_pObject;
|
2012-03-19 14:53:44 +08:00
|
|
|
}
|
|
|
|
return pRetObject;
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCObject* CCDictionary::objectForKey(int key)
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
if (m_eDictType == kCCDictUnknown && m_eDictType == kCCDictUnknown) return NULL;
|
2012-03-19 14:53:44 +08:00
|
|
|
CCAssert(m_eDictType == kCCDictInt, "this dictionary does not use integer as key.");
|
|
|
|
|
|
|
|
CCObject* pRetObject = NULL;
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictElement *pElement = NULL;
|
2012-03-19 14:53:44 +08:00
|
|
|
HASH_FIND_INT(m_pElements, &key, pElement);
|
|
|
|
if (pElement != NULL)
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
pRetObject = pElement->m_pObject;
|
2012-03-19 14:53:44 +08:00
|
|
|
}
|
|
|
|
return pRetObject;
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
bool CCDictionary::setObject(CCObject* pObject, const char* key)
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
|
|
|
CCAssert(key != NULL && strlen(key) > 0 && pObject != NULL, "Invalid Argument!");
|
|
|
|
if (m_eOldDictType == kCCDictUnknown)
|
|
|
|
{
|
|
|
|
m_eOldDictType = kCCDictStr;
|
|
|
|
}
|
|
|
|
m_eDictType = kCCDictStr;
|
|
|
|
CCAssert(m_eDictType == m_eOldDictType, "this dictionary does not use string as key.");
|
|
|
|
|
|
|
|
bool bRet = false;
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictElement *pElement = NULL;
|
2012-03-19 14:53:44 +08:00
|
|
|
HASH_FIND_STR(m_pElements, key, pElement);
|
|
|
|
if (pElement == NULL)
|
|
|
|
{
|
|
|
|
pObject->retain();
|
2012-03-20 15:04:53 +08:00
|
|
|
pElement = new CCDictElement(key, pObject);
|
|
|
|
HASH_ADD_STR(m_pElements, m_szKey, pElement);
|
2012-03-19 14:53:44 +08:00
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCObject* pTmpObj = pElement->m_pObject;
|
|
|
|
pTmpObj->retain();
|
2012-03-19 14:53:44 +08:00
|
|
|
removeObjectForKey(key);
|
|
|
|
setObject(pObject, key);
|
2012-03-20 15:04:53 +08:00
|
|
|
pTmpObj->release();
|
2012-03-19 14:53:44 +08:00
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
bool CCDictionary::setObject(CCObject* pObject, int key)
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
|
|
|
CCAssert(pObject != NULL, "Invalid Argument!");
|
|
|
|
if (m_eOldDictType == kCCDictUnknown)
|
|
|
|
{
|
|
|
|
m_eOldDictType = kCCDictInt;
|
|
|
|
}
|
|
|
|
m_eDictType = kCCDictInt;
|
|
|
|
CCAssert(m_eDictType == m_eOldDictType, "this dictionary does not use integer as key.");
|
|
|
|
|
|
|
|
bool bRet = false;
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictElement *pElement = NULL;
|
2012-03-19 14:53:44 +08:00
|
|
|
HASH_FIND_INT(m_pElements, &key, pElement);
|
|
|
|
if (pElement == NULL)
|
|
|
|
{
|
|
|
|
pObject->retain();
|
2012-03-20 15:04:53 +08:00
|
|
|
pElement = new CCDictElement(key, pObject);
|
|
|
|
HASH_ADD_INT(m_pElements, m_iKey, pElement);
|
2012-03-19 14:53:44 +08:00
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCObject* pTmpObj = pElement->m_pObject;
|
|
|
|
pTmpObj->retain();
|
2012-03-19 14:53:44 +08:00
|
|
|
removeObjectForKey(key);
|
|
|
|
setObject(pObject, key);
|
2012-03-20 15:04:53 +08:00
|
|
|
pTmpObj->release();
|
2012-03-19 14:53:44 +08:00
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCDictionary::removeObjectForKey(const char* key)
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
|
|
|
CCAssert(m_eDictType == kCCDictStr, "this dictionary does not use string as its key");
|
|
|
|
CCAssert(key != NULL && strlen(key) > 0, "Invalid Argument!");
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictElement *pElement = NULL;
|
2012-03-19 14:53:44 +08:00
|
|
|
HASH_FIND_STR(m_pElements, key, pElement);
|
|
|
|
if (pElement)
|
|
|
|
{
|
|
|
|
HASH_DEL(m_pElements, pElement);
|
2012-03-20 15:04:53 +08:00
|
|
|
pElement->m_pObject->release();
|
|
|
|
CC_SAFE_DELETE(pElement);
|
2012-03-19 14:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCDictionary::removeObjectForKey(int key)
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
|
|
|
CCAssert(m_eDictType == kCCDictInt, "this dictionary does not use integer as its key");
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictElement *pElement = NULL;
|
2012-03-19 14:53:44 +08:00
|
|
|
HASH_FIND_INT(m_pElements, &key, pElement);
|
|
|
|
if (pElement != NULL)
|
|
|
|
{
|
|
|
|
HASH_DEL(m_pElements, pElement);
|
2012-03-20 15:04:53 +08:00
|
|
|
pElement->m_pObject->release();
|
|
|
|
CC_SAFE_DELETE(pElement);
|
2012-03-19 14:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
void CCDictionary::removeAllObjects()
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictElement *pElement, *tmp;
|
2012-03-19 14:53:44 +08:00
|
|
|
HASH_ITER(hh, m_pElements, pElement, tmp)
|
|
|
|
{
|
|
|
|
HASH_DEL(m_pElements, pElement);
|
2012-03-20 15:04:53 +08:00
|
|
|
pElement->m_pObject->release();
|
|
|
|
CC_SAFE_DELETE(pElement);
|
2012-03-19 14:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
CCObject* CCDictionary::copyWithZone(CCZone* pZone)
|
2012-03-19 14:53:44 +08:00
|
|
|
{
|
2012-04-07 12:02:40 +08:00
|
|
|
CCAssert(pZone == NULL, "CCDirctionary should not be inherited.");
|
2012-03-20 15:04:53 +08:00
|
|
|
CCDictionary* pNewDict = new CCDictionary();
|
|
|
|
|
|
|
|
CCDictElement* pElement = NULL;
|
|
|
|
if (m_eDictType == kCCDictInt)
|
|
|
|
{
|
|
|
|
CCDICT_FOREACH(this, pElement)
|
|
|
|
{
|
2012-04-07 12:02:40 +08:00
|
|
|
pNewDict->setObject(pElement->getObject()->copy(), pElement->getIntKey());
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_eDictType == kCCDictStr)
|
|
|
|
{
|
|
|
|
CCDICT_FOREACH(this, pElement)
|
|
|
|
{
|
2012-04-07 12:02:40 +08:00
|
|
|
pNewDict->setObject(pElement->getObject()->copy(), pElement->getStrKey());
|
2012-03-20 15:04:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pNewDict;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* need deep copy ?*/
|
|
|
|
|
|
|
|
CCDictionary* CCDictionary::dictionaryWithDictionary(CCDictionary* srcDict)
|
|
|
|
{
|
|
|
|
CCDictionary* pNewDict = (CCDictionary*)srcDict->copyWithZone(NULL);
|
|
|
|
pNewDict->autorelease();
|
|
|
|
return pNewDict;
|
2012-03-19 14:53:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|