生活随笔
收集整理的這篇文章主要介紹了
【玩转cocos2d-x之二十六】数据结构CCDictionary
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原創作品,轉載請標明:http://blog.csdn.net/jackystudio/article/details/16958587
CCDictionary在cocos2d-x中被大量的應用,比如CCTexureCache,CCSpriteFramCache等等。
1.實現原理
1.1.uthash
CCDictionary是使用uthash實現的,而過時的CCMutableDictionary則是使用STL實現,就效率而言CCDictionary至少提升了兩倍,而且CCDictionary并沒有使用C++模版,因此也很容易綁定到腳本。
uthash是一個C/C++的哈希表實現,它以宏定義的方式實現了哈希表,不僅加快了運行速度,而且與key類型無關。它的github地址是https://github.com/troydhanson/uthash。cocos2d-x的頭文件在\cocos2d-x-2.2.0\cocos2dx\support\data_support\uthash.h。
如果想在C++中直接使用也很簡單,userguide在這里:http://troydhanson.github.io/uthash/userguide.html??梢院芊奖愕剡M行增加,刪除,查找,計數,迭代,排序等操作。
1.2.鍵(key)
uthash支持4種標準類型的鍵:整型,字符串,指針和結構體,不過到了CCDictionary就只支持整型和字符串型了。
[cpp]?view plaincopy
enum?CCDictType?? {?? ????kCCDictUnknown?=?0,?? ????kCCDictStr,?? ????kCCDictInt?? };??
2.CCDictElement
在了解CCDictionary之前還要看一下CCDictElement,很明顯,CCDictElement是CCDictionary的一個元素,包含了一個key-value。key支持整型和字符串,使用的時候要注意在同一個CCDictionary中key類型必須要一致,value可以不一致。
3.API
3.1.創建
[cpp]?view plaincopy
?? static?CCDictionary*?create();?? ?? static?CCDictionary*?createWithDictionary(CCDictionary*?srcDict);?? ?? static?CCDictionary*?createWithContentsOfFile(const?char?*pFileName);?? ?? ?? static?CCDictionary*?createWithContentsOfFileThreadSafe(const?char?*pFileName); ?
3.2.查找
[cpp]?view plaincopy
?? CCObject*?objectForKey(const?std::string&?key);?? ?? CCObject*?objectForKey(intptr_t?key);?? ?? const?CCString*?valueForKey(const?std::string&?key);?? ?? const?CCString*?valueForKey(intptr_t?key); ?
3.3.增加
[cpp]?view plaincopy
?? ?? void?setObject(CCObject*?pObject,?const?std::string&?key);?? ?? ?? void?setObject(CCObject*?pObject,?intptr_t?key); ?
3.4.移除
[cpp]?view plaincopy
?? void?removeObjectForKey(const?std::string&?key);?? void?removeObjectForKey(intptr_t?key);?? ?? void?removeObjectsForKeys(CCArray*?pKeyArray);?? ?? void?removeObjectForElememt(CCDictElement*?pElement);?? ?? void?removeAllObjects(); ?
3.5.其他
[cpp]?view plaincopy
?? CCObject*?randomObject();?? ?? CCArray*?allKeys();?? ?? CCArray*?allKeysForObject(CCObject*?object);?? ?? unsigned?int?count();?? ?? bool?writeToFile(const?char?*fullPath);??
4.示例
[cpp]?view plaincopy
?? CCDictionary*?pDict?=?CCDictionary::create();?? ?? ?? CCString*?pValue1?=?CCString::create("100");?? CCString*?pValue2?=?CCString::create("120");?? CCInteger*?pValue3?=?CCInteger::create(200);?? pDict->setObject(pValue1,?"key1");?? pDict->setObject(pValue2,?"key2");?? pDict->setObject(pValue3,?"key3");?? ?? ?? CCString*?pStr1?=?(CCString*)pDict->objectForKey("key1");?? CCLog("{?key1:?%s?}",?pStr1->getCString());?? CCInteger*?pInteger?=?(CCInteger*)pDict->objectForKey("key3");?? CCLog("{?key3:?%d?}",?pInteger->getValue());?? CCString*?pStr3=static_cast<CCString*>(pDict->randomObject());?? CCLog("{?random?key:?%s?}",pStr3->getCString());???????????????? if(pDict->writeToFile("pdic.plist"))???????????????????????????? ????CCLog("Write?to?file?success!"); ?
Resource/pdic.plist
[html]?view plaincopy
<?xml?version="1.0"?encoding="UTF-8"?>?? <!DOCTYPE?plist?PUBLIC?"-//Apple//DTD?PLIST?1.0//EN"?"http://www.apple.com/DTDs/PropertyList-1.0.dtd"/>?? ?? <plist?version="1.0">?? ????<dict>?? ????????<key>key1</key>?? ????????<string>100</string>?? ????????<key>key2</key>?? ????????<string>120</string>?? ????????<key>key3</key>??????????<!key3對應整型數據無法寫入/>?? ????</dict>?? </plist>??
5.CCDICT_FOREACH
5.1.概況
宏定義,用于遍歷CCDictionary的value。
[cpp]?view plaincopy
#define?CCDICT_FOREACH(__dict__,?__el__)?\?? ????CCDictElement*?pTmp##__dict__##__el__?=?NULL;?\?? ????if?(__dict__)?\?? ????HASH_ITER(hh,?(__dict__)->m_pElements,?__el__,?pTmp##__dict__##__el__) ?
5.2.示例
[cpp]?view plaincopy
CCDictElement*?pElement;?? CCDICT_FOREACH(dict,?pElement)?? {?? ????const?char*key?=?pElement->getStrKey();?? ?????? ????CCSprite*?pSprite?=?(CCSprite*)pElement->getObject();?? ?????? } ?
總結
以上是生活随笔為你收集整理的【玩转cocos2d-x之二十六】数据结构CCDictionary的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。