/** * @brief * Sets the node position */ void GraphNode::SetPos(float fX, float fY, float fZ) { // Set new position m_vPos.SetXYZ(fX, fY, fZ); { // Calculate all node distances Iterator<Neighbour*> cIterator = m_lstNeighbours.GetIterator(); while (cIterator.HasNext()) { Neighbour *pNeighbour = cIterator.Next(); pNeighbour->fDistance = GetDistance(*pNeighbour->pNode); } } { // Update all neighbour node distances Iterator<GraphNode*> cIterator = m_lstIsNeighbourFrom.GetIterator(); while (cIterator.HasNext()) { // Find this node within the neighbour Iterator<Neighbour*> cNeighbourIterator = cIterator.Next()->m_lstNeighbours.GetIterator(); while (cNeighbourIterator.HasNext()) { Neighbour *pNeighbour = cNeighbourIterator.Next(); if (pNeighbour->pNode == this) { pNeighbour->fDistance = GetDistance(*pNeighbour->pNode); break; } } } } }
short Mastermind::CleanList(Color* _tabColorRef, short* _tabVerdicts) { Color couleurVerdict; Iterator<ArrayI<Color>> iter; for (int i = 0; i < 4; i++) { iter.SetCurrent(list->Begin()); switch(_tabVerdicts[i]) { case 1: //Bonne couleur, bonne place //Si la séquence de couleurs traitée n'a pas la couleur à la bonne place, il faut la retirer de la liste. while (iter.GetCurrent() != NULL) { couleurVerdict = _tabColorRef[i]; if (couleurVerdict!=(iter.GetCurrentElement()->GetElement(i))) { list->Erase(iter); } iter.Next(); } case 2: //Bonne couleur, mauvaise place //Si la séquence de couleurs traitée n'a pas la couleur à un autre emplacement que celui de la couleur de référence, //il faut la retirer de la liste. while (iter.GetCurrent() != NULL) { couleurVerdict = _tabColorRef[i]; if (couleurVerdict==(iter.GetCurrentElement()->GetElement(i))) { list->Erase(iter); } iter.Next(); } case 3: //Mauvaise couleur //Si la séquence de couleurs traitée a la couleur, il faut la retirer de la liste. while (iter.GetCurrent() != NULL) { couleurVerdict = _tabColorRef[i]; for (int j = 0; j < 4; j++) { if (couleurVerdict==(iter.GetCurrentElement()->GetElement(j))) { list->Erase(iter); } } iter.Next(); } }; } return 1; //Pour Compiler }
int main() { int ret; DB *db; struct Options op; op.create_if_missing = true; Status s = DB::Open(op, "/tmp/testdb", &db); char *value = (char *)malloc(1024 * 1024); if (value == NULL) { cout << "malloc error" << endl; return -1; } memset(value, 0, 1024*1024); if (s.ok()) { cout << "create db successfully!"<<endl; int i = 0; for (i = 0; i < 50; i++) { char temp[10] = {0}; sprintf(temp, "%d", i); s = db->Put(WriteOptions(), temp, value); if (s.ok()) { cout << "put " << temp << " successful" << endl; } else { cout << "put " << temp << " failed" << endl; } } Iterator *iter = db->NewIterator(ReadOptions()); for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { cout << "key is " << iter->key().data() << ", value is " << iter->value().data() << endl; iter->Next(); } delete iter; } else { cout << "create failed " << endl; } delete db; return 0; }
BNetworkCookieJar::~BNetworkCookieJar() { BNetworkCookie* cookiePtr; for (Iterator it = GetIterator(); (cookiePtr = it.Next()) != NULL;) delete it.Remove(); }
/** * @brief * Destructor */ World::~World() { // Destroy all visibility information Iterator<void*> cIterator = m_lstFreeBodyInformation.GetIterator(); while (cIterator.HasNext()) delete static_cast<Sensor::BodyInfo*>(cIterator.Next()); }
///Iterator 模式应该是最为熟悉的模式了,最简单的证明就是我在实现 Composite 模式、 Flyweight 模式、Observer 模式中就直接用到了 STL 提供的 Iterator 来遍历 Vector 或者 List 数据结构。 ///Iterator 模式也正是用来解决对一个聚合对象的遍历问题,将对聚合的遍历封装到一个 类中进行,这样就避免了暴露这个聚合对象的内部表示的可能。 void IteratorTest() { Aggregate* ag = new ConcreteAggregate(); Iterator* it = new ConcreteIterator(ag); for (; !(it->IsDone()) ; it->Next()) { cout<<it->CurrentItem()<<endl; } }
int main() { //ConcreteAggregate *pName = NULL; ConcreteAggregate *pName = new ConcreteAggregate(); if(NULL != pName) { pName->Push("Hello"); pName->Push("word"); pName->Push("kevin"); } Iterator *iter = NULL; iter = pName->CreateIterator(); if(NULL != iter) { string strItem = iter->First(); while(!iter->IsEnd()) { cout << iter->GetCur() << " is ok " << endl; iter->Next(); } } delete pName; return 0; }
List<SRPWindows*> *Gui::GetMouseEnabledWindows() { // create an empty list List<SRPWindows*> *pList = new List<SRPWindows*>; // get the iterator for all the windows Iterator<SRPWindows*> cIterator = m_pWindows->GetIterator(); // loop trough the windows while (cIterator.HasNext()) { SRPWindows *pSRPWindows = cIterator.Next(); if (pSRPWindows->GetData()->bIsVisable && pSRPWindows->GetData()->bMouseEnabled) { // if the window is visible and the mouse for the window is enabled then add the window to the list pList->Add(pSRPWindows); } } if (pList->GetNumOfElements() > 0) { // list has items so return the list return pList; } else { // return nothing because the list is empty return nullptr; } }
void Ardb::PrintDB(const DBID& db) { Slice empty; KeyObject start(empty, KV, db); Iterator* iter = FindValue(start); while (NULL != iter && iter->Valid()) { Slice tmpkey = iter->Key(); KeyObject* kk = decode_key(tmpkey, NULL); if (kk->db != db) { DELETE(kk); break; } ValueObject v; Buffer readbuf(const_cast<char*>(iter->Value().data()), 0, iter->Value().size()); decode_value(readbuf, v, false); if (NULL != kk) { std::string str; DEBUG_LOG("[%d]Key=%s, Value=%s", kk->type, kk->key.data(), v.ToString(str).c_str()); } DELETE(kk); iter->Next(); } DELETE(iter); }
int Ardb::FlushScripts() { KeyObject start(Slice(), SCRIPT, ARDB_GLOBAL_DB); Iterator* iter = FindValue(start, false); BatchWriteGuard guard(GetEngine()); while (NULL != iter && iter->Valid()) { Slice tmpkey = iter->Key(); KeyObject* kk = decode_key(tmpkey, NULL); if (NULL != kk) { if (kk->type == SCRIPT) { DelValue(*kk); } else { break; } } DELETE(kk); iter->Next(); } DELETE(iter); return 0; }
void Ardb::Walk(WalkHandler* handler) { KeyObject start(Slice(), KV, 0); Iterator* iter = FindValue(start); uint32 cursor = 0; while (NULL != iter && iter->Valid()) { Slice tmpkey = iter->Key(); KeyObject* kk = decode_key(tmpkey, NULL); if (NULL == kk) { break; } ValueObject v; Buffer readbuf(const_cast<char*>(iter->Value().data()), 0, iter->Value().size()); decode_value(readbuf, v, false); int ret = handler->OnKeyValue(kk, &v, cursor++); DELETE(kk); if (ret < 0) { break; } iter->Next(); } DELETE(iter); }
/** * @brief * Draws recursive */ void SRPDebugWireframesFixedFunctions::DrawRec(Renderer &cRenderer, const SQCull &cCullQuery) const { // Get the fixed functions interface (when we're in here, we know that it must exist!) FixedFunctions *pFixedFunctions = cRenderer.GetFixedFunctions(); // Get scene container SceneContainer &cSceneContainer = cCullQuery.GetSceneContainer(); const VisContainer &cVisContainer = cCullQuery.GetVisContainer(); // Set the new scissor rectangle cRenderer.SetScissorRect(&cVisContainer.GetProjection().cRectangle); // Draw the container scene node if (cSceneContainer.GetMeshHandler()) DrawMesh(cRenderer, cVisContainer, *cSceneContainer.GetMeshHandler()); // Render all visible scene nodes of this scene container Iterator<VisNode*> cIterator = cVisContainer.GetVisNodes().GetIterator(); while (cIterator.HasNext()) { // Get visibility node and scene node const VisNode *pVisNode = cIterator.Next(); SceneNode *pSceneNode = pVisNode->GetSceneNode(); if (pSceneNode) { // Set the current world matrix pFixedFunctions->SetTransformState(FixedFunctions::Transform::World, pVisNode->GetWorldMatrix()); // Is this scene node a portal? if (pVisNode->IsPortal()) { // Get the target cell visibility container const VisContainer *pVisCell = static_cast<const VisPortal*>(pVisNode)->GetTargetVisContainer(); if (pVisCell && pVisCell->GetCullQuery()) { // Draw the target cell DrawRec(cRenderer, *pVisCell->GetCullQuery()); // Set the previous scissor rectangle cRenderer.SetScissorRect(&cVisContainer.GetProjection().cRectangle); } // Draw the portal itself if (pSceneNode->GetMeshHandler()) DrawMesh(cRenderer, *pVisNode, *pSceneNode->GetMeshHandler()); // Is this scene node a container? We do not need to check for cells because we will // NEVER receive cells from SQCull directly, they are ONLY visible through portals! (see above) } else if (pVisNode->IsContainer()) { // Draw this container without special processing if (static_cast<const VisContainer*>(pVisNode)->GetCullQuery()) DrawRec(cRenderer, *static_cast<const VisContainer*>(pVisNode)->GetCullQuery()); // Set the previous scissor rectangle cRenderer.SetScissorRect(&cVisContainer.GetProjection().cRectangle); // This must just be a quite boring scene node :) } else { if (pSceneNode->GetMeshHandler()) DrawMesh(cRenderer, *pVisNode, *pSceneNode->GetMeshHandler()); } } } }
/** * @brief * Removes a neighbour connection */ bool GraphNode::RemoveNeighbour(GraphNode &cNode) { // Find neighbour list entry Neighbour *pNeighbour = nullptr; { Iterator<Neighbour*> cIterator = m_lstNeighbours.GetIterator(); while (cIterator.HasNext()) { Neighbour *pNeighbourT = cIterator.Next(); if (pNeighbourT->pNode == &cNode) { pNeighbour = pNeighbourT; break; } } } // Remove if (pNeighbour) { cNode.m_lstIsNeighbourFrom.Remove(this); delete pNeighbour; m_lstNeighbours.Remove(pNeighbour); // Done return true; } else return false; // Error! }
void Gui::DefaultCallBackHandler() { // get the iterator for the windows Iterator<SRPWindows*> cIterator = m_pWindows->GetIterator(); // loop trough the windows while (cIterator.HasNext()) { SRPWindows *pSRPWindows = cIterator.Next(); // check if callback is present if (pSRPWindows->GetNumberOfCallBacks() > 0) { if (pSRPWindows->GetCallBack(DRAGWINDOW)) { m_pDragWindow = pSRPWindows; } if (pSRPWindows->GetCallBack(HIDEWINDOW)) { /*hide window*/ } if (pSRPWindows->GetCallBack(CLOSEWINDOW)) { /*close window*/ } // call back is processed so we clear them pSRPWindows->ClearCallBacks(); } } }
void PointArrayParticleSystem::init(IStorm3D* s3d, IStorm3D_Scene* scene) { defaultInit(s3d, scene, *m_eds); std::string fileName = m_eds->modelFile; if(fileName.empty()) return; Matrix sm; Matrix rm; QUAT q; q.MakeFromAngles(m_eds->rotation.x, m_eds->rotation.y, m_eds->rotation.z); rm.CreateRotationMatrix(q); sm.CreateScaleMatrix(m_eds->scale); sm.Multiply(rm); IStorm3D_Model* model = s3d->CreateNewModel(); assert(model != NULL); if(model->LoadS3D(fileName.c_str())) { Iterator<IStorm3D_Model_Object*>* obj = model->ITObject->Begin(); assert(obj != NULL); boost::shared_ptr<PointArray> pm(new PointArray()); for(; !obj->IsEnd(); obj->Next()) { IStorm3D_Mesh* mesh = obj->GetCurrent()->GetMesh(); VC3 opos = obj->GetCurrent()->GetPosition(); if(mesh) { int base = pm->verts.size(); pm->verts.resize(base + mesh->GetVertexCount()); pm->normals.resize(base + mesh->GetVertexCount()); Storm3D_Vertex *v = mesh->GetVertexBuffer(); for(int i = 0; i < mesh->GetVertexCount(); i++) { Vector pos = v[i].position + opos; Vector nor = v[i].normal; sm.TransformVector(pos); rm.RotateVector(nor); pm->verts[base + i] = pos; pm->normals[base + i] = nor; } } } m_parray.swap(pm); if(m_eds->firstVertex < 0) m_eds->firstVertex = 0; if(m_eds->lastVertex >= (int)m_parray->verts.size()) m_eds->lastVertex = m_parray->verts.size()-1; delete obj; } delete model; }
int main(int argc, char* argv[]) { Aggregate* ag = new ConcreteAggregate(); Iterator* it = new ConcreteIterator(ag); for (; !(it->IsDone()); it->Next()) { cout << it->CurrentItem() << endl; } return 0; }
Set<ElemType> *Set<ElemType>::Union(Set *otherSet) { Iterator<ElemType> *iterator; Set *result; result = new Set<ElemType>(cmp); iterator = CreateIterator(); while (iterator->HasNext()) { result->AddElement(iterator->Next()); } delete iterator; iterator = otherSet->CreateIterator(); while (iterator->HasNext()) { result->AddElement(iterator->Next()); } delete iterator; return (result); }
void test_iterator() { Aggregate* pAg = new ConcreteAggregate(); Iterator* pItor = new ConcreteIterator(pAg); for (; !pItor->IsDone(); pItor->Next()) { std::cout << pItor->CurrentItem() << std::endl; } }
//} ostream& operator <<(ostream& out,const polynomial& A) { //cout << "AHEAD = " << *A.head; //node *walker = A.head; Iterator<polyterm> P = A.Begin(); while(P.GetNext() != NULL) { if(P.GetNode()->GetData().Coef() == 0) { cout << ""; // walker=walker->next; P.GetNext(); } else { if(P.GetNext()->GetData().Coef() > 0) { out << P; cout << " + "; //walker = walker->next; P.Next(); } else if(P.GetNext()->GetData().Coef()<0) { out << P; //walker = walker->next; P.Next(); } else if(P.GetNext()->GetData().Coef()==0) { out << P; out << " + "; //walker = walker->next; P.Next(); } } } out << P; //P.Next(); return out; }
/** * @brief * Called when the scene node needs to be updated */ void PGSparkles::OnUpdate() { // If this scene node wasn't drawn at the last frame, we can skip some update stuff if ((GetFlags() & ForceUpdate) || m_bUpdate) { m_bUpdate = true; // If there are free particles, create new particles if (!(System::GetInstance()->GetMicroseconds() % BuildPerSec)) { while (Math::GetRand() % 5) { Particle *pParticle = AddParticle(); if (pParticle) InitParticle(*pParticle); else break; } } { // Update particles float fTimeDiff = Timing::GetInstance()->GetTimeDifference(); Iterator<Particle> cIterator = GetParticleIterator(); while (cIterator.HasNext()) { Particle &cParticle = cIterator.Next(); // Update velocity cParticle.vVelocity += GetGravity()*fTimeDiff*(200-cParticle.fEnergy)/10; cParticle.vVelocity -= cParticle.vVelocity*fTimeDiff; // Self induction to make the sparkles a bit more interesting ;-) if (!(System::GetInstance()->GetMicroseconds() % 500) && !(Math::GetRand() % 5)) { cParticle.fEnergy += 100; cParticle.fSize += 0.2f; } // Update position Vector3 vMove = cParticle.vVelocity*fTimeDiff*cParticle.fEnergy/100; cParticle.vPos += vMove/10; // Update distortion cParticle.fCustom1 += fTimeDiff; float dLength = cParticle.fCustom1/vMove.GetLength()*5; cParticle.vDistortion = vMove*dLength; // Update energy, size and lifetime cParticle.fEnergy -= fTimeDiff*EnergyPerSec; cParticle.fSize -= fTimeDiff; if (cParticle.fEnergy <= 0 || cParticle.fSize <= 0) InitParticle(cParticle); else cParticle.vColor[3] = cParticle.fEnergy/255; } } // We have to recalculate the current axis align bounding box in 'scene node space' DirtyAABoundingBox(); } }
Currency CompositeEquipment::NetPrice () { Iterator<Equipment*>* i = CreateIterator(); Currency total = 0; for (i->First(); !i->IsDone(); i->Next()) { total += i->CurrentItem()->NetPrice(); } delete i; return total; }
Bag *Bag::Union(Bag* other) const { assert(other != nullptr); Bag *result = new Bag(); //add elements from first Bag Iterator *it = NewIterator(); for(Object *o = it->Next(); o != nullptr;o = it->Next()) { result->Add(o); } delete it; //add elements from second Bag; it = other->NewIterator(); for(Object *o = it->Next(); o != nullptr;o = it->Next()) { result->Add(o); } delete it; return result; }
int List<TYPE>::GetNbElements() const { Iterator<TYPE> iter; iter.SetCurrent(first); int nbEle = 0; while (iter.GetCurrent() != NULL) { nbEle ++; iter.Next(); } return nbEle; }
/** * @brief * Clears all neighbour connectivity */ void GraphNode::ClearNeighbours() { { // Update neighbour connections Iterator<GraphNode*> cIterator = m_lstIsNeighbourFrom.GetIterator(); while (cIterator.HasNext()) { // Remove this node from the neighbours connectivity list cIterator.Next()->RemoveNeighbour(*this); } } m_lstIsNeighbourFrom.Clear(); { // Clear neighbours Iterator<Neighbour*> cIterator = m_lstNeighbours.GetIterator(); while (cIterator.HasNext()) { Neighbour *pNN = cIterator.Next(); pNN->pNode->m_lstIsNeighbourFrom.Remove(this); delete pNN; } } m_lstNeighbours.Clear(); }
void Gui::UnFocusAllWindows() { // focused window needs to be a nullptr m_pFocusedWindow = nullptr; // get the iterator for all the windows Iterator<SRPWindows*> cIterator = m_pWindows->GetIterator(); // loop trough the windows while (cIterator.HasNext()) { // unfocus the window cIterator.Next()->GetAwesomiumWindow()->Unfocus(); } }
virtual const string GetName() { string _nameList = _name; Iterator<Component*>* it = CreateIterator(); for(it->First(); !it->IsDone(); it->Next()) { _nameList += it->CurrentItem()->GetName(); } return _nameList; }
ArrayI<Color>* Mastermind::GetElement() const { //Utiliser la classe RandomNumber pour ne pas offrir toujours le premier élément de la liste (les parties seraient toutes pareilles avec la même séquence cachée) RandomNumber index; Iterator<ArrayI<Color>> iter; iter.SetCurrent(list->Begin()); for(int i=1; i<index.GetNumber(GetNbElements()); i++) { iter.Next(); } return iter.GetCurrentElement(); }
Bag *Bag::Intersect(Bag* other) const { assert(other != nullptr); Bag *result = new Bag(); Iterator *it = NewIterator(); for(Object *o = it->Next(); o != nullptr;o = it->Next()) { if(other->Contains(o)) { // other bag contains o //Object not already in result bag if(!result->Contains(o)) { BagNode *thisBagNode = Find(o); BagNode *otherBagNode = other->Find(o); int count = min(thisBagNode->count, otherBagNode->count); for(int i = 0; i < count; i++){ result->Add(o); } } } } delete it; return result; }
/** * @brief * Called when the scene node needs to be updated */ void PGImage::OnUpdate() { // If this scene node wasn't drawn at the last frame, we can skip some update stuff if ((GetFlags() & ForceUpdate) || m_bUpdate) { m_bUpdate = false; { // Update particles float fTimeDiff = Timing::GetInstance()->GetTimeDifference(); Iterator<Particle> cIterator = GetParticleIterator(); while (cIterator.HasNext()) { Particle &cParticle = cIterator.Next(); cParticle.fSize = cParticle.fCustom1; for (int i=0; i<3; i++) { if (cParticle.vPos[i] > cParticle.vDistortion[i]) cParticle.vVelocity[i] -= fTimeDiff*10; else { if (cParticle.vPos[i] < cParticle.vDistortion[i]) cParticle.vVelocity[i] += fTimeDiff*10; } if (cParticle.vVelocity[i] > 100.0f) cParticle.vVelocity[i] = 100.0f; if (cParticle.vVelocity[i] < -100.0f) cParticle.vVelocity[i] = -100.0f; cParticle.vPos[i] += cParticle.vVelocity[i]*fTimeDiff; if ((cParticle.vPos[i] <= cParticle.vDistortion[i]+0.1f && cParticle.vPos[i] >= cParticle.vDistortion[i]-0.1f) || cParticle.vPos[i] >= cParticle.vFixPos[i]+0.2f || cParticle.vPos[i] <= cParticle.vFixPos[i]-0.2f) { if (cParticle.vDistortion[i] != cParticle.vFixPos[i]) cParticle.vVelocity[i] *= 0.8f; if (cParticle.vPos[i] > cParticle.vFixPos[i]+0.2f || cParticle.vPos[i] < cParticle.vFixPos[i]-0.2f) cParticle.vDistortion[i] = cParticle.vFixPos[i]; else { if (Math::GetRand() % 2) cParticle.vDistortion[i] = static_cast<float>(Math::GetRand() % 100)/1000+cParticle.vFixPos[i]; else cParticle.vDistortion[i] = static_cast<float>(Math::GetRand() % 100)/1000-cParticle.vFixPos[i]; } } } } } // We have to recalculate the current axis align bounding box in 'scene node space' DirtyAABoundingBox(); } }
/** * @brief * Returns whether a node is a neighbour of this node or not */ bool GraphNode::IsNeighbour(const GraphNode &cNode) const { // Check whether the given node is a neighbour of this node or not Iterator<Neighbour*> cIterator = m_lstNeighbours.GetIterator(); while (cIterator.HasNext()) { if (cIterator.Next()->pNode == &cNode) { // The given node is a neighbour of this node return true; } } // The given node is no neighbour of this node return false; }