<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: subset code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 13:28:08 GMT</pubDate>
    <description>DZone Snippets: subset code</description>
    <item>
      <title>Generate all subsets of a set</title>
      <link>http://snippets.dzone.com/posts/show/4631</link>
      <description>This code generates all the subsets of {1, 2, ..., n} and prints them.&lt;br /&gt;&lt;br /&gt;This also contains a very fast binary counter implementation.&lt;br /&gt;&lt;br /&gt;See &lt;a href="http://compprog.wordpress.com/2007/10/10/generating-subsets/"&gt;this&lt;/a&gt; for further explanations.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#include &lt;stdio.h&gt;&lt;br /&gt;&lt;br /&gt;/* Applies the mask to a set like {1, 2, ..., n} and prints it */&lt;br /&gt;void printv(int mask[], int n) {&lt;br /&gt;	int i;&lt;br /&gt;	printf("{ ");&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		if (mask[i])&lt;br /&gt;			printf("%d ", i + 1); /*i+1 is part of the subset*/&lt;br /&gt;	printf("\b }\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/* Generates the next mask*/&lt;br /&gt;int next(int mask[], int n) {&lt;br /&gt;	int i;&lt;br /&gt;	for (i = 0; (i &lt; n) &amp;&amp; mask[i]; ++i)&lt;br /&gt;		mask[i] = 0;&lt;br /&gt;&lt;br /&gt;	if (i &lt; n) {&lt;br /&gt;		mask[i] = 1;&lt;br /&gt;		return 1;&lt;br /&gt;	}&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int argc, char *argv[]) {&lt;br /&gt;	int n = 3;&lt;br /&gt;&lt;br /&gt;	int mask[16]; /* Guess what this is */&lt;br /&gt;	int i;&lt;br /&gt;	for (i = 0; i &lt; n; ++i)&lt;br /&gt;		mask[i] = 0;&lt;br /&gt;&lt;br /&gt;	/* Print the first set */&lt;br /&gt;	printv(mask, n);&lt;br /&gt;&lt;br /&gt;	/* Print all the others */&lt;br /&gt;	while (next(mask, n))&lt;br /&gt;		printv(mask, n);&lt;br /&gt;&lt;br /&gt;	return 0;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 10 Oct 2007 14:43:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4631</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>subset? and superset? functions</title>
      <link>http://snippets.dzone.com/posts/show/1123</link>
      <description>&lt;code&gt;&lt;br /&gt;    subset?: func [&lt;br /&gt;        {Returns true if A is a subset of B; false otherwise.}&lt;br /&gt;        a [series! bitset!]&lt;br /&gt;        b [series! bitset!]&lt;br /&gt;    ] [&lt;br /&gt;        empty? exclude a b&lt;br /&gt;    ]&lt;br /&gt;&lt;br /&gt;    superset?: func [&lt;br /&gt;        {Returns true if set1 is a superset of set2; false otherwise.}&lt;br /&gt;        set1 [series! bitset!]&lt;br /&gt;        set2 [series! bitset!]&lt;br /&gt;    ][&lt;br /&gt;        subset? set2 set1&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 06:15:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1123</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
  </channel>
</rss>
