blob: 965988770382ac773d0a4cde24485a3806e033f1 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href='https://fonts.googleapis.com/css?family=Architects+Daughter' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen" />
<link rel="stylesheet" type="text/css" href="stylesheets/pygment_trac.css" media="screen" />
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<title>Golang-set by deckarep</title>
</head>
<body>
<header>
<div class="inner">
<h1>Golang-set</h1>
<h2>A simple set type for the Go language.</h2>
<a href="https://github.com/deckarep/golang-set" class="button"><small>View project on</small>GitHub</a>
</div>
</header>
<div id="content-wrapper">
<div class="inner clearfix">
<section id="main-content">
<p><a href="https://travis-ci.org/deckarep/golang-set"><img src="https://travis-ci.org/deckarep/golang-set.png?branch=master" alt="Build Status"></a></p>
<h1>
<a name="golang-set" class="anchor" href="#golang-set"><span class="octicon octicon-link"></span></a>golang-set</h1>
<p>A simple set type for the Go language.</p>
<p>Coming from Python one of the things I miss is the superbly wonderful set collection. This is my attempt to mimic the primary features of the set from Python.
You can of course argue that there is no need for a set in Go, otherwise the creators would have added one to the standard library. To those I say simply ignore this repository
and carry-on and to the rest that find this useful please contribute in helping me make it better by:</p>
<ul>
<li>Helping to make more idiomatic improvements to the code.</li>
<li>Helping to make it better for more generic use across types.</li>
<li>Helping to increase the performance of it. <del>(So far, no attempt has been made, but since it uses a map internally, I expect it to be mostly performant.)</del>
</li>
<li>Helping to make the unit-tests more robust and kick-ass.</li>
<li>Helping to fill in the <a href="http://godoc.org/github.com/deckarep/golang-set">documentation.</a>
</li>
<li>Simply offering feedback and suggestions, since I am a Go n00b. (Positive, constructive feedback is appreciated.)</li>
</ul><p>I have to give some credit for helping seed the idea with this post on <a href="http://programmers.stackexchange.com/questions/177428/sets-data-structure-in-golang">stackoverflow.</a></p>
<p>Please see the unit test file for additional usage examples. The Python set documentation will also do a better job than I can of explaining how a set typically <a href="http://docs.python.org/2/library/sets.html">works.</a> Please keep in mind
however that the Python set is a built-in type and supports additional features and syntax that make it awesome. This set for Go is nowhere near as comprehensive as the Python set
also, this set has not been battle-tested or used in production. Also, this set is not goroutine safe...you have been warned.</p>
<p>Examples but not exhaustive:</p>
<div class="highlight"><pre><span class="nx">requiredClasses</span> <span class="o">:=</span> <span class="nx">NewSet</span><span class="p">()</span>
<span class="nx">requiredClasses</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="s">"Cooking"</span><span class="p">)</span>
<span class="nx">requiredClasses</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="s">"English"</span><span class="p">)</span>
<span class="nx">requiredClasses</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="s">"Math"</span><span class="p">)</span>
<span class="nx">requiredClasses</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="s">"Biology"</span><span class="p">)</span>
<span class="nx">scienceSlice</span> <span class="o">:=</span> <span class="p">[]</span><span class="kd">interface</span><span class="p">{}{</span><span class="s">"Biology"</span><span class="p">,</span> <span class="s">"Chemistry"</span><span class="p">}</span>
<span class="nx">scienceClasses</span> <span class="o">:=</span> <span class="nx">NewSetFromSlice</span><span class="p">(</span><span class="nx">scienceSlice</span><span class="p">)</span>
<span class="nx">electiveClasses</span> <span class="o">:=</span> <span class="nx">NewSet</span><span class="p">()</span>
<span class="nx">electiveClasses</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="s">"Welding"</span><span class="p">)</span>
<span class="nx">electiveClasses</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="s">"Music"</span><span class="p">)</span>
<span class="nx">electiveClasses</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="s">"Automotive"</span><span class="p">)</span>
<span class="nx">bonusClasses</span> <span class="o">:=</span> <span class="nx">NewSet</span><span class="p">()</span>
<span class="nx">bonusClasses</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="s">"Go Programming"</span><span class="p">)</span>
<span class="nx">bonusClasses</span><span class="p">.</span><span class="nx">Add</span><span class="p">(</span><span class="s">"Python Programming"</span><span class="p">)</span>
<span class="c1">//Show me all the available classes I can take</span>
<span class="nx">allClasses</span> <span class="o">:=</span> <span class="nx">requiredClasses</span><span class="p">.</span><span class="nx">Union</span><span class="p">(</span><span class="nx">scienceClasses</span><span class="p">).</span><span class="nx">Union</span><span class="p">(</span><span class="nx">electiveClasses</span><span class="p">).</span><span class="nx">Union</span><span class="p">(</span><span class="nx">bonusClasses</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">allClasses</span><span class="p">)</span> <span class="c1">//Set{Cooking, English, Math, Chemistry, Welding, Biology, Music, Automotive, Go Programming, Python Programming}</span>
<span class="c1">//Is cooking considered a science class?</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">scienceClasses</span><span class="p">.</span><span class="nx">Contains</span><span class="p">(</span><span class="s">"Cooking"</span><span class="p">))</span> <span class="c1">//false</span>
<span class="c1">//Show me all classes that are not science classes, since I hate science.</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">allClasses</span><span class="p">.</span><span class="nx">Difference</span><span class="p">(</span><span class="nx">scienceClasses</span><span class="p">))</span> <span class="c1">//Set{Music, Automotive, Go Programming, Python Programming, Cooking, English, Math, Welding}</span>
<span class="c1">//Which science classes are also required classes?</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">scienceClasses</span><span class="p">.</span><span class="nx">Intersect</span><span class="p">(</span><span class="nx">requiredClasses</span><span class="p">))</span> <span class="c1">//Set{Biology}</span>
<span class="c1">//How many bonus classes do you offer?</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">bonusClasses</span><span class="p">.</span><span class="nx">Size</span><span class="p">())</span> <span class="c1">//2</span>
<span class="c1">//Do you have the following classes? Welding, Automotive and English?</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">allClasses</span><span class="p">.</span><span class="nx">ContainsAll</span><span class="p">(</span><span class="s">"Welding"</span><span class="p">,</span> <span class="s">"Automotive"</span><span class="p">,</span> <span class="s">"English"</span><span class="p">))</span> <span class="c1">//true</span>
</pre></div>
<p>Thanks!</p>
<p>-Ralph</p>
<p><a href="https://bitdeli.com/free" title="Bitdeli Badge"><img src="https://d2weczhvl823v0.cloudfront.net/deckarep/golang-set/trend.png" alt="Bitdeli Badge"></a></p>
</section>
<aside id="sidebar">
<a href="https://github.com/deckarep/golang-set/zipball/master" class="button">
<small>Download</small>
.zip file
</a>
<a href="https://github.com/deckarep/golang-set/tarball/master" class="button">
<small>Download</small>
.tar.gz file
</a>
<p class="repo-owner"><a href="https://github.com/deckarep/golang-set"></a> is maintained by <a href="https://github.com/deckarep">deckarep</a>.</p>
<p>This page was generated by <a href="pages.github.com">GitHub Pages</a> using the Architect theme by <a href="https://twitter.com/jasonlong">Jason Long</a>.</p>
</aside>
</div>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-42584447-1");
pageTracker._trackPageview();
} catch(err) {}
</script>
</body>
</html>