<ahref="dynamic__permutation_8h.html">Go to the documentation of this file.</a><divclass="fragment"><divclass="line"><aname="l00001"></a><spanclass="lineno"> 1</span> <spanclass="comment">// Copyright 2010-2018 Google LLC</span></div>
<divclass="line"><aname="l00002"></a><spanclass="lineno"> 2</span> <spanclass="comment">// Licensed under the Apache License, Version 2.0 (the "License");</span></div>
<divclass="line"><aname="l00003"></a><spanclass="lineno"> 3</span> <spanclass="comment">// you may not use this file except in compliance with the License.</span></div>
<divclass="line"><aname="l00004"></a><spanclass="lineno"> 4</span> <spanclass="comment">// You may obtain a copy of the License at</span></div>
<divclass="line"><aname="l00008"></a><spanclass="lineno"> 8</span> <spanclass="comment">// Unless required by applicable law or agreed to in writing, software</span></div>
<divclass="line"><aname="l00009"></a><spanclass="lineno"> 9</span> <spanclass="comment">// distributed under the License is distributed on an "AS IS" BASIS,</span></div>
<divclass="line"><aname="l00010"></a><spanclass="lineno"> 10</span> <spanclass="comment">// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div>
<divclass="line"><aname="l00011"></a><spanclass="lineno"> 11</span> <spanclass="comment">// See the License for the specific language governing permissions and</span></div>
<divclass="line"><aname="l00012"></a><spanclass="lineno"> 12</span> <spanclass="comment">// limitations under the License.</span></div>
<divclass="line"><aname="l00018"></a><spanclass="lineno"> 18</span> <spanclass="preprocessor">#include <set></span><spanclass="comment">// TODO(user): remove when no longer used.</span></div>
<divclass="line"><aname="l00027"></a><spanclass="lineno"> 27</span> <spanclass="comment">// Maintains a 'partial' permutation of [0..n-1] onto itself, with a dynamic</span></div>
<divclass="line"><aname="l00028"></a><spanclass="lineno"> 28</span> <spanclass="comment">// API allowing it to be built incrementally, and allowing some backtracking.</span></div>
<divclass="line"><aname="l00029"></a><spanclass="lineno"> 29</span> <spanclass="comment">// This is tuned for a specific usage by ./find_graph_symmetries.cc.</span></div>
<divclass="line"><aname="l00031"></a><spanclass="lineno"> 31</span> <spanclass="comment">// RAM usage: as of 2014-04, this class needs less than:</span></div>
<divclass="line"><aname="l00035"></a><spanclass="lineno"> 35</span> <spanclass="comment">// Upon construction, every element i in [0..n-1] maps to itself.</span></div>
<divclass="line"><aname="l00040"></a><spanclass="lineno"> 40</span> <spanclass="comment">// Declares a set of mappings for this permutation: src[i] will map to dst[i].</span></div>
<divclass="line"><aname="l00041"></a><spanclass="lineno"> 41</span> <spanclass="comment">// Requirements that are DCHECKed:</span></div>
<divclass="line"><aname="l00042"></a><spanclass="lineno"> 42</span> <spanclass="comment">// - "src" and "dst" must have the same size.</span></div>
<divclass="line"><aname="l00043"></a><spanclass="lineno"> 43</span> <spanclass="comment">// - For all i, src[i] must not already be mapped to something.</span></div>
<divclass="line"><aname="l00044"></a><spanclass="lineno"> 44</span> <spanclass="comment">// - For all i, dst[i] must not already be the image of something.</span></div>
<divclass="line"><aname="l00049"></a><spanclass="lineno"> 49</span> <spanclass="comment">// Undoes the last AddMappings() operation, and fills the "undone_mapping_src"</span></div>
<divclass="line"><aname="l00050"></a><spanclass="lineno"> 50</span> <spanclass="comment">// vector with the src of that last operation. This works like an undo stack.</span></div>
<divclass="line"><aname="l00051"></a><spanclass="lineno"> 51</span> <spanclass="comment">// For example, applying the sequence (Add, Add, Add, Undo, Add, Undo, Undo)</span></div>
<divclass="line"><aname="l00052"></a><spanclass="lineno"> 52</span> <spanclass="comment">// has exactly the same effect as applying the first Add() alone.</span></div>
<divclass="line"><aname="l00053"></a><spanclass="lineno"> 53</span> <spanclass="comment">// If you call this too may times (i.e. there is nothing left to undo), it is</span></div>
<divclass="line"><aname="l00054"></a><spanclass="lineno"> 54</span> <spanclass="comment">// simply a no-op.</span></div>
<divclass="line"><aname="l00056"></a><spanclass="lineno"> 56</span> <spanclass="comment">// Complexity: same as the AddMappings() operation being undone.</span></div>
<divclass="line"><aname="l00059"></a><spanclass="lineno"> 59</span> <spanclass="comment">// Makes the permutation back to the identity (i.e. like right after</span></div>
<divclass="line"><aname="l00066"></a><spanclass="lineno"> 66</span> <spanclass="comment">// Returns the union of all "src" ever given to AddMappings().</span></div>
<divclass="line"><aname="l00069"></a><spanclass="lineno"> 69</span> <spanclass="comment">// While the permutation is partially being built, the orbit of elements will</span></div>
<divclass="line"><aname="l00070"></a><spanclass="lineno"> 70</span> <spanclass="comment">// either form unclosed paths, or closed cycles. In the former case,</span></div>
<divclass="line"><aname="l00071"></a><spanclass="lineno"> 71</span> <spanclass="comment">// RootOf(i) returns the start of the path where i lies. If i is on a cycle,</span></div>
<divclass="line"><aname="l00072"></a><spanclass="lineno"> 72</span> <spanclass="comment">// RootOf(i) will return some element of its cycle (meaning that if i maps to</span></div>
<divclass="line"><aname="l00075"></a><spanclass="lineno"> 75</span> <spanclass="comment">// Complexity: O(log(orbit size)) in average, assuming that the mappings are</span></div>
<divclass="line"><aname="l00076"></a><spanclass="lineno"> 76</span> <spanclass="comment">// added in a random order. O(orbit size) in the worst case.</span></div>
<divclass="line"><aname="l00079"></a><spanclass="lineno"> 79</span> <spanclass="comment">// The exhaustive set of the 'loose end' of the incomplete cycles</span></div>
<divclass="line"><aname="l00080"></a><spanclass="lineno"> 80</span> <spanclass="comment">// (e.g., paths) built so far.</span></div>
<divclass="line"><aname="l00081"></a><spanclass="lineno"> 81</span> <spanclass="comment">// TODO(user): use a faster underlying container like SparseBitSet, and</span></div>
<divclass="line"><aname="l00082"></a><spanclass="lineno"> 82</span> <spanclass="comment">// tweak this API accordingly.</span></div>
<divclass="line"><aname="l00085"></a><spanclass="lineno"> 85</span> <spanclass="comment">// Creates a SparsePermutation representing the current permutation.</span></div>
<divclass="line"><aname="l00086"></a><spanclass="lineno"> 86</span> <spanclass="comment">// Requirements: the permutation must only have cycles.</span></div>
<divclass="line"><aname="l00095"></a><spanclass="lineno"> 95</span> <spanclass="comment">// ancestor_[i] isn't exactly RootOf(i): it might itself have an ancestor, and</span></div>
<divclass="line"><aname="l00096"></a><spanclass="lineno"> 96</span> <spanclass="comment">// so on.</span></div>
<divclass="line"><aname="l00099"></a><spanclass="lineno"> 99</span> <spanclass="comment">// The concatenation of all "src" ever given to AddMappings(), and their</span></div>
<divclass="line"><aname="l00100"></a><spanclass="lineno"> 100</span> <spanclass="comment">// sizes, to implement the undo stack. Note that "mapping_src_stack_" contains</span></div>
<divclass="line"><aname="l00101"></a><spanclass="lineno"> 101</span> <spanclass="comment">// exactly the support of the permutation.</span></div>
<divclass="line"><aname="l00108"></a><spanclass="lineno"> 108</span> <spanclass="comment">// Used transiently by CreateSparsePermutation(). Its resting state is:</span></div>
<divclass="line"><aname="l00109"></a><spanclass="lineno"> 109</span> <spanclass="comment">// size=Size(), all elements are false.</span></div>
<divclass="ttc"id="aclassoperations__research_1_1DynamicPermutation_html_ae8f61c84a06024a7d6343902f44e7e59"><divclass="ttname"><ahref="classoperations__research_1_1DynamicPermutation.html#ae8f61c84a06024a7d6343902f44e7e59">operations_research::DynamicPermutation::UndoLastMappings</a></div><divclass="ttdeci">void UndoLastMappings(std::vector< int > *undone_mapping_src)</div></div>
<divclass="ttc"id="aclassoperations__research_1_1DynamicPermutation_html_aeb46445d022e7bc495f212704791824a"><divclass="ttname"><ahref="classoperations__research_1_1DynamicPermutation.html#aeb46445d022e7bc495f212704791824a">operations_research::DynamicPermutation::AddMappings</a></div><divclass="ttdeci">void AddMappings(const std::vector< int >&src, const std::vector< int >&dst)</div></div>