2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCSet.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Set::Set(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_set = new set<Object *>;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Set::Set(const Set &rSetObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
_set = new set<Object *>(*rSetObject._set);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// call retain of members
|
2013-06-20 14:13:12 +08:00
|
|
|
SetIterator iter;
|
2013-06-15 14:03:30 +08:00
|
|
|
for (iter = _set->begin(); iter != _set->end(); ++iter)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (! (*iter))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*iter)->retain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Set::~Set(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-11-30 21:38:27 +08:00
|
|
|
removeAllObjects();
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_set);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Set::acceptVisitor(DataVisitor &visitor)
|
2013-05-10 15:07:05 +08:00
|
|
|
{
|
|
|
|
visitor.visit(this);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Set * Set::create()
|
2013-03-14 01:58:24 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Set * pRet = new Set();
|
2013-03-14 01:58:24 +08:00
|
|
|
|
|
|
|
if (pRet != NULL)
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Set* Set::copy(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Set *pSet = new Set(*this);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return pSet;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Set* Set::mutableCopy(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return copy();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
int Set::count(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (int)_set->size();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Set::addObject(Object *pObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(pObject);
|
2013-06-15 14:03:30 +08:00
|
|
|
_set->insert(pObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Set::removeObject(Object *pObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_set->erase(pObject);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_RELEASE(pObject);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Set::removeAllObjects()
|
2012-11-30 09:00:34 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
SetIterator it;
|
2013-06-15 14:03:30 +08:00
|
|
|
for (it = _set->begin(); it != _set->end(); ++it)
|
2012-11-30 21:38:27 +08:00
|
|
|
{
|
|
|
|
if (! (*it))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*it)->release();
|
|
|
|
}
|
2012-11-30 09:00:34 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Set::containsObject(Object *pObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _set->find(pObject) != _set->end();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SetIterator Set::begin(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _set->begin();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SetIterator Set::end(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _set->end();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* Set::anyObject()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_set || _set->empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SetIterator it;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for( it = _set->begin(); it != _set->end(); ++it)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
if (*it)
|
|
|
|
{
|
|
|
|
return (*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|