Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Set.cs « MonoDevelop.Autotools « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ef7b93ae724d03158d32f2a2b0c374e276403a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
Copyright (C) 2006  Matthias Braun <matze@braunis.de>
 
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace MonoDevelop.Autotools
{
	public class Set<T>: IEnumerable<T>
	{
		private Dictionary<T,object> hashtable = new Dictionary<T,object>();
		
		public void Add (T o)
		{
			hashtable[o] = this;
		}
		
		public void Remove(T o)
		{
			hashtable.Remove(o);
		}
		
		public bool Contains(T o)
		{
			return hashtable.ContainsKey (o);
		}
		
		public IEnumerator<T> GetEnumerator()
		{
			return hashtable.Keys.GetEnumerator();
		}
		
		IEnumerator IEnumerable.GetEnumerator()
		{
			return hashtable.Keys.GetEnumerator();
		}
		
		public bool ContainsSet(Set<T> set)
		{
			foreach(T o in set) {
				if(!Contains(o))
					return false;
			}
			
			return true;
		}
		
		public void Union (IEnumerable<T> set)
		{
			foreach(T o in set) {
				hashtable[o] = this;
			}
		}

		public void Intersect (Set<T> set)
		{
			List<T> toRemove = new List<T> ();
			foreach (T o in hashtable.Keys)
				if (!set.Contains (o))
					toRemove.Add (o);

			foreach (T o in toRemove)
				hashtable.Remove (o);
		}

		public void Without(Set<T> set)
		{
			foreach(T o in set) {
				hashtable.Remove(o);
			}
		}
		
		public bool Empty {
			get {
				return hashtable.Count == 0;
			}
		}

		public int Count {
			get { return hashtable.Keys.Count; }
		}

		public override string ToString ()
		{
			StringBuilder sb = new StringBuilder ();
			sb.Append ("[ ");
			foreach (T o in hashtable.Keys)
				sb.AppendFormat ("{0}, ", o.ToString ());
			sb.Append (" ]");
			return sb.ToString ();
		}
	}
}