Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Store the list of variables, and then sort them based on a custom comparison operator. For example:</p> <pre><code> // Ordering int* _ordering; // Variable template&lt;size_t T&gt; class Variable : public bitset&lt;T&gt; { private: char* _name; // A variable has a name. public: // Constructors Variable() :bitset&lt;T&gt;(), _name(NULL) {}; Variable(string bitstring, char* name) :bitset&lt;T&gt;(bitstring), _name(name) {}; // Name accessor char*&amp; name(void) {return _name;}; // Comparison operator bool operator&lt;(const Variable&lt;T&gt; &amp;rhs) const { for (int oidx=0; oidx&lt;T; oidx++) if (bitset&lt;T&gt;::test(_ordering[oidx])) { if (!rhs.test(_ordering[oidx])) // Left is bigger. return false; } else if (rhs.test(_ordering[oidx])) // Right is bigger. return true; // They match at all values. return false; } }; </code></pre> <p>Here is a quick and dirty main program that tests this with variables with 8 values.</p> <pre><code> #include &lt;iostream&gt; #include &lt;bitset&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;vector&gt; #include &lt;algorithm&gt; ... #define BITS 8 int main(int argc, char* argv[]) { int i; // Create the ordering based on the command line arguments _ordering = new int[BITS]; for (i=0; i&lt;BITS; i++) _ordering[i] = atoi(argv[i+1]); // Read in each variable int size=(argc-BITS-1)/2; vector&lt; Variable&lt;BITS&gt; &gt; variables; for (i=0; i&lt;size*2; i+=2) { cout &lt;&lt; "Creating bitset " &lt;&lt; argv[BITS+1+i]&lt;&lt;":"&lt;&lt;argv[BITS+2+i]&lt;&lt;endl; variables.push_back(Variable&lt;BITS&gt;(string(argv[BITS+i+1]), argv[BITS+i+2])); } // Display the variables for (i=0; i&lt;variables.size(); i++) cout &lt;&lt; variables[i] &lt;&lt; ":" &lt;&lt; variables[i].name() &lt;&lt; endl; // Sort them cout &lt;&lt; endl &lt;&lt; "Sorting..." &lt;&lt; endl; sort(variables.begin(), variables.end()); // Display again for (i=0; i&lt;variables.size(); i++) cout &lt;&lt; variables[i] &lt;&lt; ":" &lt;&lt; variables[i].name() &lt;&lt; endl; } </code></pre> <p>And here is the output:</p> <pre><code>$ ./bitsort 0 1 2 3 4 5 6 7 01 a 11 b 10 c 00 d Creating bitset 01:a Creating bitset 11:b Creating bitset 10:c Creating bitset 00:d 00000001:a 00000011:b 00000010:c 00000000:d Sorting... 00000000:d 00000010:c 00000001:a 00000011:b </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload