/// <summary> /// Compares this set to the specified set.</summary> /// <param name="other">the other set</param> /// <returns><c>true</c> if this set equals <c>other</c>; /// <c>false</c> otherwise</returns> /// public override bool Equals(object other) { if (other.GetType() != this.GetType()) { return(false); } SET <Key> that = other as SET <Key>; return(Equals(that)); }
public static void MainTest(string[] args) { SET <string> set = new SET <string>(); // insert some keys set.Add("www.cs.princeton.edu"); set.Add("www.cs.princeton.edu"); // overwrite old value set.Add("www.princeton.edu"); set.Add("www.math.princeton.edu"); set.Add("www.yale.edu"); set.Add("www.amazon.com"); set.Add("www.simpsons.com"); set.Add("www.stanford.edu"); set.Add("www.google.com"); set.Add("www.ibm.com"); set.Add("www.apple.com"); set.Add("www.slashdot.com"); set.Add("www.whitehouse.gov"); set.Add("www.espn.com"); set.Add("www.snopes.com"); set.Add("www.movies.com"); set.Add("www.cnn.com"); set.Add("www.iitb.ac.in"); Console.WriteLine(set.Contains("www.cs.princeton.edu")); Console.WriteLine(!set.Contains("www.harvardsucks.com")); Console.WriteLine(set.Contains("www.simpsons.com")); Console.WriteLine(); Console.WriteLine("ceiling(www.simpsonr.com) = " + set.Ceiling("www.simpsonr.com")); Console.WriteLine("ceiling(www.simpsons.com) = " + set.Ceiling("www.simpsons.com")); Console.WriteLine("ceiling(www.simpsont.com) = " + set.Ceiling("www.simpsont.com")); Console.WriteLine("floor(www.simpsonr.com) = " + set.Floor("www.simpsonr.com")); Console.WriteLine("floor(www.simpsons.com) = " + set.Floor("www.simpsons.com")); Console.WriteLine("floor(www.simpsont.com) = " + set.Floor("www.simpsont.com")); Console.WriteLine(); // print out all keys in this set in lexicographic order foreach (string s in set) { Console.WriteLine(s); } Console.WriteLine(); SET <string> set2 = new SET <string>(set); Console.WriteLine("Set equality: {0}", set.Equals(set2)); Console.WriteLine("ToString() equality: {0}", set2.ToString().Equals(set.ToString())); }
/// <summary> /// Returns the union of this set and that set.</summary> /// /// <param name="that"> that the other set</param> /// <returns>the union of this set and that set</returns> /// <exception cref="ArgumentNullException">if <c>that</c> is <c>null</c></exception> /// public SET <Key> Union(SET <Key> that) { if (that == null) { throw new ArgumentNullException("called union() with a null argument"); } SET <Key> c = new SET <Key>(); foreach (Key x in this) { c.Add(x); } foreach (Key x in that) { c.Add(x); } return(c); }
/// <summary> /// Returns a random rooted-out DAG on <c>V</c> vertices and <c>E</c> edges. /// A rooted out-tree is a DAG in which every vertex is reachable from a /// single vertex. /// The DAG returned is not chosen uniformly at random among all such DAGs.</summary> /// <param name="V">the number of vertices</param> /// <param name="E">the number of edges</param> /// <returns>a random rooted-out DAG on <c>V</c> vertices and <c>E</c> edges</returns> /// public static Digraph RootedOutDAG(int V, int E) { if (E > (long)V * (V - 1) / 2) { throw new ArgumentException("Too many edges"); } if (E < V - 1) { throw new ArgumentException("Too few edges"); } Digraph G = new Digraph(V); SET <Edge> set = new SET <Edge>(); // fix a topological order int[] vertices = new int[V]; for (int i = 0; i < V; i++) { vertices[i] = i; } StdRandom.Shuffle(vertices); // one edge pointing from each vertex, other than the root = vertices[V-1] for (int v = 0; v < V - 1; v++) { int w = StdRandom.Uniform(v + 1, V); Edge e = new Edge(w, v); set.Add(e); G.AddEdge(vertices[w], vertices[v]); } while (G.E < E) { int v = StdRandom.Uniform(V); int w = StdRandom.Uniform(V); Edge e = new Edge(w, v); if ((v < w) && !set.Contains(e)) { set.Add(e); G.AddEdge(vertices[w], vertices[v]); } } return(G); }
public static void MainTest(string[] args) { TextInput StdIn = new TextInput(); SET <string> set = new SET <string>(); // read in strings and add to set while (!StdIn.IsEmpty) { string key = StdIn.ReadString(); if (key.Equals("")) { continue; } if (!set.Contains(key)) { set.Add(key); Console.WriteLine(key); } } }
public static void MainTest(string[] args) { TextInput StdIn = new TextInput(); // key = word, value = set of files containing that word ST <string, SET <string> > st = new ST <string, SET <string> >(); // create inverted index of all files Console.WriteLine("Indexing files"); string[] allFileNames = FileIndex.GetFileNames(args); foreach (string filename in allFileNames) { Console.WriteLine(" " + filename); TextInput input = new TextInput(filename); while (!input.IsEmpty) { string word = input.ReadString(); if (!st.Contains(word)) { st[word] = new SET <string>(); } SET <string> set = st[word]; set.Add(filename); } } // read queries from standard input, one per line while (!StdIn.IsEmpty) { string query = StdIn.ReadString(); if (st.Contains(query)) { SET <string> set = st[query]; foreach (string filename in set) { Console.WriteLine(" " + filename); } } } }
public static void MainTest(string[] args) { TextInput StdIn = new TextInput(); SET <string> set = new SET <string>(); // read in strings and add to set TextInput input = new TextInput(args[0]); while (!input.IsEmpty) { string word = input.ReadString(); set.Add(word); } // read in string from standard input, printing out all exceptions while (!StdIn.IsEmpty) { string word = StdIn.ReadString(); if (!set.Contains(word)) { Console.WriteLine(word); } } }
/// <summary> /// Returns a random simple digraph on <c>V</c> vertices, <c>E</c> /// edges and (at least) <c>c</c> strong components. The vertices are randomly /// assigned integer labels between <c>0</c> and <c>c-1</c> (corresponding to /// strong components). Then, a strong component is creates among the vertices /// with the same label. Next, random edges (either between two vertices with /// the same labels or from a vetex with a smaller label to a vertex with a /// larger label). The number of components will be equal to the number of /// distinct labels that are assigned to vertices.</summary> /// <param name="V">the number of vertices</param> /// <param name="E">the number of edges</param> /// <param name="c">the (maximum) number of strong components</param> /// <returns>a random simple digraph on <c>V</c> vertices and /// <c>E</c> edges, with (at most) <c>c</c> strong components</returns> /// <exception cref="ArgumentException">if <c>c</c> is larger than <c>V</c></exception> /// public static Digraph Strong(int V, int E, int c) { if (c >= V || c <= 0) { throw new ArgumentException("Number of components must be between 1 and V"); } if (E <= 2 * (V - c)) { throw new ArgumentException("Number of edges must be at least 2(V-c)"); } if (E > (long)V * (V - 1) / 2) { throw new ArgumentException("Too many edges"); } // the digraph Digraph G = new Digraph(V); // edges added to G (to avoid duplicate edges) SET <Edge> set = new SET <Edge>(); int[] label = new int[V]; for (int v = 0; v < V; v++) { label[v] = StdRandom.Uniform(c); } // make all vertices with label c a strong component by // combining a rooted in-tree and a rooted out-tree for (int i = 0; i < c; i++) { // how many vertices in component c int count = 0; for (int v = 0; v < G.V; v++) { if (label[v] == i) { count++; } } // if (count == 0) System.err.println("less than desired number of strong components"); int[] vertices = new int[count]; int j = 0; for (int v = 0; v < V; v++) { if (label[v] == i) { vertices[j++] = v; } } StdRandom.Shuffle(vertices); // rooted-in tree with root = vertices[count-1] for (int v = 0; v < count - 1; v++) { int w = StdRandom.Uniform(v + 1, count); Edge e = new Edge(w, v); set.Add(e); G.AddEdge(vertices[w], vertices[v]); } // rooted-out tree with root = vertices[count-1] for (int v = 0; v < count - 1; v++) { int w = StdRandom.Uniform(v + 1, count); Edge e = new Edge(v, w); set.Add(e); G.AddEdge(vertices[v], vertices[w]); } } while (G.E < E) { int v = StdRandom.Uniform(V); int w = StdRandom.Uniform(V); Edge e = new Edge(v, w); if (!set.Contains(e) && v != w && label[v] <= label[w]) { set.Add(e); G.AddEdge(v, w); } } return(G); }
/// <summary> /// Initializes a new set that is an independent copy of the specified set.</summary> /// <param name="x">the set to copy to this set</param> public SET(SET <Key> x) { set = new SortedSet <Key>(x.set); }