void TGraphCascade::TopologicalSort(TIntV& SortedNIdV) { int Nodes = Graph.GetNodes(); SortedNIdV.Gen(Nodes, 0); // result THash<TInt, TBool> Marks(Nodes); // nodeid -> mark map THash<TInt,TBool> TempMarks(Nodes); // nodeid -> temp mark map THash<TInt, TBool> Added(Nodes); TIntV NIdV; Graph.GetNIdV(NIdV); // all node ids // set marks for (int NodeN = 0; NodeN < Nodes; NodeN++) { int NodeId = NIdV[NodeN]; Marks.AddDat(NodeId, false); TempMarks.AddDat(NodeId, false); Added.AddDat(NodeId, false); } TSStack<TInt> Stack; for (int NodeN = 0; NodeN < Nodes; NodeN++) { int NodeId = NIdV[NodeN]; // select an unmarked node if (!Marks.GetDat(NodeId)) { Stack.Push(NodeId); while (!Stack.Empty()) { // visit TopNode int TopNodeId = Stack.Top(); Marks.GetDat(TopNodeId) = true; TempMarks.GetDat(TopNodeId) = true; // add children, set their temp marks to true TNGraph::TNodeI NI = Graph.GetNI(TopNodeId); int Children = NI.GetOutDeg(); bool IsFinal = true; for (int ChildN = 0; ChildN < Children; ChildN++) { int ChildId = NI.GetOutNId(ChildN); EAssertR(!TempMarks.GetDat(ChildId), "TGraphCascade::TopologicalSort: the graph is not a DAG!"); if (!Marks.GetDat(ChildId)) { // unvisited node IsFinal = false; Stack.Push(ChildId); } } if (IsFinal) { // push TopNode to tail if (!Added.GetDat(TopNodeId)) { SortedNIdV.Add(TopNodeId); Added.GetDat(TopNodeId) = true; } TempMarks.GetDat(TopNodeId) = false; Stack.Pop(); } } } } SortedNIdV.Reverse(); }
void EntitySystem::Change(Entity* pEntity) { bool contains = (m_systemBit & pEntity->GetSystemBits()) == m_systemBit; bool interest = (m_typeFlags & pEntity->GetTypeBits()) == m_typeFlags; bool excluded = (m_excludedTypeFlags & pEntity->GetTypeBits()).any(); if ((interest && !excluded) && !contains && m_typeFlags.any()) { m_activities.add(pEntity); pEntity->AddSystemBit(m_systemBit); Added(pEntity); } else if ((!interest || excluded) && contains && m_typeFlags.any()) { this->remove(pEntity); } }
void CTestRangeMap::TestRangeMap(void) const { Filling("CRangeMap"); typedef CRangeMultimap<CConstRef<CObject> > TMap; typedef TMap::const_iterator TMapCI; TMap m; // fill for ( int count = 0; count < m_RangeNumber; ) { TRange range = RandomRange(); m.insert(TMap::value_type(range, CConstRef<CObject>(0))); ++count; Added(range); } if ( m_PrintSize ) { Filled(m.size()); // Stat(m.stat()); } for ( TMapCI i = m.begin(); i; ++i ) { FromAll(i.GetInterval()); } size_t scannedCount = 0; for ( int count = 0; count < m_ScanCount; ++count ) { for ( int pos = 0; pos <= m_Length + 2*m_RangeLength; pos += m_ScanStep ) { TRange range; range.Set(pos, pos + m_ScanLength - 1); StartFrom(range); for ( TMapCI i = m.begin(range); i; ++i ) { From(range, i.GetInterval()); ++scannedCount; } } } PrintTotalScannedNumber(scannedCount); End(); }
void CTestRangeMap::TestIntervalTree(void) const { Filling("CIntervalTree"); typedef CIntervalTree TMap; typedef TMap::const_iterator TMapCI; TMap m; // fill for ( int count = 0; count < m_RangeNumber; ) { TRange range = RandomRange(); m.Insert(range, CConstRef<CObject>(0)); ++count; Added(range); } if ( m_PrintSize ) { Filled(m.Size()); Stat(m.Stat()); } for ( TMapCI i = m.AllIntervals(); i; ++i ) { FromAll(i.GetInterval()); } size_t scannedCount = 0; for ( int count = 0; count < m_ScanCount; ++count ) { for ( int pos = 0; pos <= m_Length + 2*m_RangeLength; pos += m_ScanStep ) { TRange range(pos, pos + m_ScanLength - 1); StartFrom(range); for ( TMapCI i = m.IntervalsOverlapping(range); i; ++i ) { From(range, i.GetInterval()); ++scannedCount; } } } PrintTotalScannedNumber(scannedCount); End(); }
inline Vector<T, N> operator + (const Vector<T, N>& a, const Vector<T, N>& b) { return Added(a, b); }
static GainType BestKOptMoveRec(int k, GainType G0,LKH::LKHAlg *Alg) { LKH::LKHAlg::Candidate *Nt2; LKH::LKHAlg::Node *t1, *t2, *t3, *t4; GainType G1, G2, G3, Gain; int X4, i; int Breadth2 = 0; t1 = (*t.get())[1]; t2 = (*t.get())[i = 2 * k - 2]; (*incl.get())[(*incl.get())[i] = i + 1] = i; (*incl.get())[(*incl.get())[1] = i + 2] = 1; /* Choose (t2,t3) as a candidate edge emanating from t2 */ for (Nt2 = t2->CandidateSet; (t3 = Nt2->To); Nt2++) { if (t3 == t2->Pred || t3 == t2->Suc || ((G1 = G0 - Nt2->Cost) <= 0 && Alg->GainCriterionUsed && Alg->ProblemType != LKH::HCP && Alg->ProblemType != LKH::HPP) || Added(t2, t3)) continue; if (++Breadth2 > Alg->MaxBreadth) break; MarkAdded(t2, t3); (*t.get())[2 * k - 1] = t3; (*G.get())[2 * k - 2] = G1 + t3->Pi; /* Choose t4 as one of t3's two neighbors on the tour */ for (X4 = 1; X4 <= 2; X4++) { t4 = X4 == 1 ? (Alg->Reversed == (t3)->Parent->Reversed ? (t3)->Pred : (t3)->Suc) : (Alg->Reversed == (t3)->Parent->Reversed ? (t3)->Suc : (t3)->Pred); if ((Fixed(t3, t4) || Alg->IsCommonEdge(t3, t4)) || Deleted(t3, t4)) continue; (*t.get())[2 * k] = t4; G2 = G1 + (Alg->*(Alg->C))(t3, t4); G3 = MINUS_INFINITY; if (t4 != t1 && !Alg->Forbidden(t4, t1) && !Added(t4, t1) && (!Alg->c || G2 - (Alg->*(Alg->c))(t4, t1) > 0) && (G3 = G2 - (Alg->*(Alg->C))(t4, t1)) > 0 && FeasibleKOptMove(k)) { UnmarkAdded(t2, t3); Alg->MakeKOptMove(k); return G3; } if (Alg->Backtracking && !Alg->Excludable(t3, t4)) continue; MarkDeleted(t3, t4); (*G.get())[2 * k - 1] = G2 - t4->Pi; if (k < *K) { if ((Gain = BestKOptMoveRec(k + 1, G2,Alg)) > 0) { UnmarkAdded(t2, t3); UnmarkDeleted(t3, t4); return Gain; } (*incl.get())[(*incl.get())[1] = 2 * k] = 1; } if (t4 != t1 && !Alg->Forbidden(t4, t1) && k + 1 < Alg->NonsequentialMoveType && Alg->PatchingC >= 2 && Alg->PatchingA >= 1 && (Alg->Swaps == 0 || Alg->SubsequentPatching)) { if (G3 == MINUS_INFINITY) G3 = G2 - (Alg->*(Alg->C))(t4, t1); if ((Alg->PatchingCRestricted ? G3 > 0 && Alg->IsCandidate(t4, t1) : Alg->PatchingCExtended ? G3 > 0 || Alg->IsCandidate(t4, t1) : G3 > 0) && (Gain = Alg->PatchCycles(k, G3)) > 0) { UnmarkAdded(t2, t3); UnmarkDeleted(t3, t4); return Gain; } } UnmarkDeleted(t3, t4); if (k == *K && t4 != t1 && t3 != t1 && G3 <= 0 && !Added(t4, t1) && (!Alg->GainCriterionUsed || G2 - Alg->Precision >= t4->Cost)) { if (!Alg->Backtracking || Alg->Swaps > 0) { if ((G2 > *BestG2 || (G2 == *BestG2 && !Near(t3, t4) && Near((*T.get())[2 * *K - 1], (*T.get())[2 * *K]))) && Alg->Swaps < Alg->MaxSwaps && Alg->Excludable(t3, t4) && !InInputTour(t3, t4)) { if (Alg->RestrictedSearch && *K > 2 && Alg->ProblemType != LKH::HCP && Alg->ProblemType != LKH::HPP) { /* Ignore the move if the gain does not vary */ (*G.get())[0] = (*G.get())[2 * *K - 2]; (*G.get())[1] = (*G.get())[2 * *K - 1]; for (i = 2 * *K - 3; i >= 2; i--) if ((*G.get())[i] != (*G.get())[i % 2]) break; if (i < 2) continue; } if (FeasibleKOptMove(*K)) { *BestG2 = G2; memcpy(T.get() + 1, t.get() + 1, 2 * *K * sizeof(LKH::LKHAlg::Node *)); } } } else if (Alg->MaxSwaps > 0 && FeasibleKOptMove(*K)) { LKH::LKHAlg::Node *SUCt1 = (Alg->Reversed == (t1)->Parent->Reversed ? (t1)->Suc : (t1)->Pred); Alg->MakeKOptMove(*K); for (i = 1; i < 2 * k; i += 2) { Alg->Exclude((*t.get())[i], (*t.get())[i + 1]); UnmarkDeleted((*t.get())[i], (*t.get())[i + 1]); } for (i = 2; i < 2 * k; i += 2) UnmarkAdded((*t.get())[i], (*t.get())[i + 1]); memcpy(tSaved.get() + 1, t.get() + 1, 2 * k * sizeof(LKH::LKHAlg::Node *)); while ((t4 = (Alg->*(Alg->BestSubsequentMove))(t1, t4, &G2, &Gain))); if (Gain > 0) { UnmarkAdded(t2, t3); return Gain; } Alg->RestoreTour(); *K = k; memcpy(t.get() + 1, tSaved.get() + 1, 2 * *K * sizeof(LKH::LKHAlg::Node *)); for (i = 1; i < 2 * *K - 2; i += 2) MarkDeleted((*t.get())[i], (*t.get())[i + 1]); for (i = 2; i < 2 * *K; i += 2) MarkAdded((*t.get())[i], (*t.get())[i + 1]); for (i = 2; i < 2 * *K; i += 2) (*incl.get())[(*incl.get())[i] = i + 1] = i; (*incl.get())[(*incl.get())[1] = 2 * *K] = 1; if (SUCt1 != (Alg->Reversed == (t1)->Parent->Reversed ? (t1)->Suc : (t1)->Pred)) Alg->Reversed ^= 1; (*T.get())[2 * *K] = 0; } } } UnmarkAdded(t2, t3); if (t3 == t1) continue; /* Try to delete an added edge, (_,t3) or (t3,_) */ for (i = 2 * k - 4; i >= 2; i--) { if (t3 == (*t.get())[i]) { t4 = (*t.get())[i ^ 1]; if (t4 == t1 || Alg->Forbidden(t4, t1) || (Fixed(t3, t4) || Alg->IsCommonEdge(t3, t4)) || Added(t4, t1)) continue; G2 = G1 + (Alg->*(Alg->C))(t3, t4); if ((!Alg->c || G2 - (Alg->*(Alg->c))(t4, t1) > 0) && (Gain = G2 - (Alg->*(Alg->C))(t4, t1)) > 0) { (*incl.get())[(*incl.get())[i ^ 1] = 1] = i ^ 1; (*incl.get())[(*incl.get())[i] = 2 * k - 2] = i; if (FeasibleKOptMove(k - 1)) { Alg->MakeKOptMove(k - 1); return Gain; } (*incl.get())[(*incl.get())[i ^ 1] = i] = i ^ 1; } } } (*incl.get())[1] = 2 * k; (*incl.get())[2 * k - 2] = 2 * k - 1; } return 0; }
/// Addition operator friend Quaternion operator + (const Quaternion& q1, const Quaternion& q2) { return Added(q1, q2); }
static GainType PatchCyclesRec(int k, int m, int M, GainType G0, LKH::LKHAlg *Alg) { LKH::LKHAlg::Node *s1, *s2, *s3, *s4, *s5, *s6, *S3 = 0, *S4 = 0; LKH::LKHAlg::Candidate *Ns2, *Ns4; GainType G1, G2, G3, G4, Gain, CloseUpGain, BestCloseUpGain = Alg->PatchingAExtended ? MINUS_INFINITY : 0; int X4, X6; int i, NewCycle, *cycleSaved = 0, *pSaved = 0; int Breadth2 = 0, Breadth4; s1 = (*t.get())[2 * k + 1]; s2 = (*t.get())[i = 2 * (k + m) - 2]; (*incl.get())[(*incl.get())[i] = i + 1] = i; /* Choose (s2,s3) as a candidate edge emanating from s2 */ for (Ns2 = s2->CandidateSet; (s3 = Ns2->To); Ns2++) { if (s3 == s2->Pred || s3 == s2->Suc || Added(s2, s3) || (NewCycle = Cycle(s3, k,Alg)) == *CurrentCycle) continue; if (++Breadth2 > Alg->MaxBreadth) break; MarkAdded(s2, s3); (*t.get())[2 * (k + m) - 1] = s3; G1 = G0 - Ns2->Cost; /* Choose s4 as one of s3's two neighbors on the tour */ for (X4 = 1; X4 <= 2; X4++) { s4 = X4 == 1 ? s3->Pred : s3->Suc; if ((Fixed(s3, s4) || Alg->IsCommonEdge(s3, s4)) || Deleted(s3, s4)) continue; MarkDeleted(s3, s4); (*t.get())[2 * (k + m)] = s4; G2 = G1 + (Alg->*(Alg->C))(s3, s4); if (M > 2) { if (!cycleSaved) { assert(cycleSaved = (int *) malloc(2 * k * sizeof(int))); memcpy(cycleSaved, cycle.get() + 1, 2 * k * sizeof(int)); } for (i = 1; i <= 2 * k; i++) if ((*cycle.get())[i] == NewCycle) (*cycle.get())[i] = *CurrentCycle; /* Extend the current alternating path */ if ((Gain = PatchCyclesRec(k, m + 1, M - 1, G2,Alg)) > 0) { UnmarkAdded(s2, s3); UnmarkDeleted(s3, s4); goto End_PatchCyclesRec; } memcpy(cycle.get() + 1, cycleSaved, 2 * k * sizeof(int)); if (Alg->PatchingA >= 2 && *Patchwork < Alg->Dimension && k + M < Alg->NonsequentialMoveType && !Alg->Forbidden(s4, s1) && (!Alg->PatchingARestricted || Alg->IsCandidate(s4, s1))) { GainType Bound = BestCloseUpGain >= 0 || Alg->IsCandidate(s4, s1) ? BestCloseUpGain : 0; if ((!Alg->c || G2 - (Alg->*(Alg->c))(s4, s1) > Bound) && (CloseUpGain = G2 - (Alg->*(Alg->C))(s4, s1)) > Bound) { S3 = s3; S4 = s4; BestCloseUpGain = CloseUpGain; } } } else if (!Alg->Forbidden(s4, s1) && (!Alg->c || G2 - (Alg->*(Alg->c))(s4, s1) > 0) && (Gain = G2 - (Alg->*(Alg->C))(s4, s1)) > 0) { (*incl.get())[(*incl.get())[2 * k + 1] = 2 * (k + m)] = 2 * k + 1; Alg->MakeKOptMove(k + m); UnmarkAdded(s2, s3); UnmarkDeleted(s3, s4); goto End_PatchCyclesRec; } UnmarkDeleted(s3, s4); } UnmarkAdded(s2, s3); } if (M == 2 && !Alg->PatchingCRestricted) { /* Try to patch the two cycles by a sequential 3-opt move */ (*incl.get())[(*incl.get())[2 * (k + m)] = 2 * (k + m) + 1] = 2 * (k + m); (*incl.get())[(*incl.get())[2 * k + 1] = 2 * (k + m) + 2] = 2 * k + 1; Breadth2 = 0; /* Choose (s2,s3) as a candidate edge emanating from s2 */ for (Ns2 = s2->CandidateSet; (s3 = Ns2->To); Ns2++) { if (s3 == s2->Pred || s3 == s2->Suc || Added(s2, s3)) continue; if (++Breadth2 > Alg->MaxBreadth) break; (*t.get())[2 * (k + m) - 1] = s3; G1 = G0 - Ns2->Cost; NewCycle = Cycle(s3, k,Alg); /* Choose s4 as one of s3's two neighbors on the tour */ for (X4 = 1; X4 <= 2; X4++) { s4 = X4 == 1 ? s3->Pred : s3->Suc; if ((Fixed(s3, s4) || Alg->IsCommonEdge(s3, s4)) || Deleted(s3, s4)) continue; (*t.get())[2 * (k + m)] = s4; G2 = G1 + (Alg->*(Alg->C))(s3, s4); Breadth4 = 0; /* Choose (s4,s5) as a candidate edge emanating from s4 */ for (Ns4 = s4->CandidateSet; (s5 = Ns4->To); Ns4++) { if (s5 == s4->Pred || s5 == s4->Suc || s5 == s1 || Added(s4, s5) || (NewCycle == *CurrentCycle && Cycle(s5, k,Alg) == *CurrentCycle)) continue; if (++Breadth4 > Alg->MaxBreadth) break; G3 = G2 - Ns4->Cost; /* Choose s6 as one of s5's two neighbors on the tour */ for (X6 = 1; X6 <= 2; X6++) { s6 = X6 == 1 ? s5->Pred : s5->Suc; if (s6 == s1 || Alg->Forbidden(s6, s1) || (Fixed(s5, s6) || Alg->IsCommonEdge(s5, s6)) || Deleted(s5, s6) || Added(s6, s1)) continue; G4 = G3 + (Alg->*(Alg->C))(s5, s6); if ((!Alg->c || G4 - (Alg->*(Alg->c))(s6, s1) > 0) && (Gain = G4 - (Alg->*(Alg->C))(s6, s1)) > 0) { if (!pSaved) { assert(pSaved = (int *) malloc(2 * k * sizeof(int))); memcpy(pSaved, p.get() + 1, 2 * k * sizeof(int)); } (*t.get())[2 * (k + m) + 1] = s5; (*t.get())[2 * (k + m) + 2] = s6; if (FeasibleKOptMove(k + m + 1)) { Alg->MakeKOptMove(k + m + 1); goto End_PatchCyclesRec; } memcpy(p.get() + 1, pSaved, 2 * k * sizeof(int)); for (i = 1; i <= 2 * k; i++) (*q.get())[(*p.get())[i]] = i; } } } } } } Gain = 0; if (S4) { int OldCycle = *CurrentCycle; if (!pSaved) { assert(pSaved = (int *) malloc(2 * k * sizeof(int))); memcpy(pSaved, p.get() + 1, 2 * k * sizeof(int)); } (*t.get())[2 * (k + m) - 1] = S3; (*t.get())[2 * (k + m)] = S4; (*incl.get())[(*incl.get())[2 * k + 1] = 2 * (k + m)] = 2 * k + 1; /* Find a new alternating cycle */ Alg->PatchingA--; (*RecLevel)++; MarkAdded(s2, S3); MarkDeleted(S3, S4); MarkAdded(S4, s1); Gain = Alg->PatchCycles(k + m, BestCloseUpGain); UnmarkAdded(s2, S3); UnmarkDeleted(S3, S4); UnmarkAdded(S4, s1); (*RecLevel)--; Alg->PatchingA++; if (Gain <= 0) { memcpy(cycle.get() + 1, cycleSaved, 2 * k * sizeof(int)); memcpy(p.get() + 1, pSaved, 2 * k * sizeof(int)); for (i = 1; i <= 2 * k; i++) (*q.get())[(*p.get())[i]] = i; *CurrentCycle = OldCycle; } } End_PatchCyclesRec: free(cycleSaved); free(pSaved); return Gain; }