aboutsummaryrefslogtreecommitdiff
path: root/libcontextprovider/string_set.vala
blob: 44e4ef45d7ca74241666c83af5282a0c790d4b40 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 *
 * Contact: Marius Vollmer <marius.vollmer@nokia.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This program 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 program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

namespace ContextProvider {

	/**
	 * SECTION:string_set
	 * @short_description: An unorder efficient set of strings
	 * @include: string_set.h
	 * @see_also: <link linkend="libcontextprovider-Context-provider-setup">Context Provider</link>
	 *
	 * A data structure which efficiently holds a set of strings. A string set has a maximum memory usage
	 * of (total number of strings stored)/8 bytes.
	 * All operations other than iteration are O(1)
	 */

	internal class StringSet : GLib.Object {
	/**
	 * ContextProviderStringSet:
	 *
	 * Structure representing a string set
	 */
		private IntSet intset;

		StringSet.from_intset (IntSet #intset) {
			this.intset = #intset;
		}

		/**
		 * context_provider_string_set_new:
		 *
		 * Create a new #ContextProviderStringSet
		 *
		 * Returns: a new #ContextProviderStringSet
		 */
		public StringSet () {
			/* TODO:
			   we should have a way of statically initialising any keys that may be used in a stringset
			   so keys outside of this set are rejected
			   */


			intset = new IntSet();
		}

		/**
		 * context_provider_string_set_new_sized:
		 *
		 * Create a new #ContextProviderStringSet with an initially allocated size.
		 *
		 * Returns: a new #ContextProviderStringSet
		 */
		public StringSet.sized (uint size) {
			intset = new IntSet.sized (size);
		}

		/**
		 * context_provider_string_set_new_from_array:
		 * @array: an exising array of strings, null terminated
		 *
		 * Create a new #ContextProviderStringSet from an existing string array.
		 *
		 * Returns: a new #ContextProviderStringSet
		 */
		public StringSet.from_array ([CCode (array_length = false, array_null_terminated = true)] string[] array) {
			intset = new IntSet();
			foreach (var s in array) {
				add (s);
			}
		}

		/**
		 * context_provider_string_set_new_intersection:
		 * @left: a #ContextProviderStringSet
		 * @right: a #ContextProviderStringSet
		 *
		 * Create a new #ContextProviderStringSet by intersecting two existing string sets.
		 *
		 * Returns: a new #ContextProviderStringSet
		 */
		public StringSet.intersection (StringSet left, StringSet right) {
			this.from_intset (new IntSet.intersection (left.intset, right.intset));
		}

		/**
		 * context_provider_string_set_new_symmetric_difference:
		 * @left: a #ContextProviderStringSet
		 * @right: a #ContextProviderStringSet
		 *
		 * Create a new #ContextProviderStringSet by finding the symmetric difference
		 * between two existing string sets.
		 * Symmetric difference gives you the elements that are in either set, but not in both
		 *
		 * Returns: a new #ContextProviderStringSet
		 */
		public StringSet.symmetric_difference (StringSet left, StringSet right) {
			this.from_intset (new IntSet.symmetric_difference (left.intset, right.intset));
		}

		/**
		 * context_provider_string_set_new_difference:
		 * @left: a #ContextProviderStringSet
		 * @right: a #ContextProviderStringSet
		 *
		 * Create a new #ContextProviderStringSet by finding the difference
		 * between two existing string sets.
		 * Difference gives you the elements that are in #left, but not in #right.
		 *
		 * Returns: a new #ContextProviderStringSet
		 */
		public StringSet.difference (StringSet left, StringSet right) {
			this.from_intset (new IntSet.difference (left.intset, right.intset));
		}

		/**
		 * context_provider_string_set_new_union:
		 * @left: a #ContextProviderStringSet
		 * @right: a #ContextProviderStringSet
		 *
		 * Create a new #ContextProviderStringSet by finding the union
		 * of two existing string sets.
		 *
		 * Returns: a new #ContextProviderStringSet
		 */
		public StringSet.union (StringSet left, StringSet right) {
			this.from_intset (new IntSet.union (left.intset, right.intset));
		}

		/**
		 * context_provider_string_set_add:
		 * @self: a #ContextProviderStringSet
		 * @element: a string
		 *
		 * Add a string to a stringset
		 */
		public void add (string element) {
			intset.add (Quark.from_string (element));
		}

		/**
		 * context_provider_string_set_remove:
		 * @self: a #ContextProviderStringSet
		 * @element: a string
		 *
		 * Remove a string from a stringset
		 *
		 * Returns: TRUE if #element was a member of #self
		 */
		public bool remove (string element) {
			Quark q = Quark.try_string (element);
			if (q != 0)
				return intset.remove (q);
			else
				return false;
		}

		/**
		 * context_provider_string_set_size:
		 * @self: a #ContextProviderStringSet
		 *
		 * Find the number of elements in a string set.
		 *
		 * Returns: the number of elements in #self.
		 */
		public uint size () {
			return intset.size();
		}


		/**
		 * context_provider_string_set_clear:
		 * @self: a #ContextProviderStringSet
		 *
		 * Remove all elements from a string set.
		 */
		public void clear () {
			intset.clear ();
		}

		/**
		 * context_provider_string_set_is_member:
		 * @self: a #ContextProviderStringSet
		 * @element: a string
		 *
		 * Find if #element is a member of the set.
		 *
		 * Returns: TRUE if #element is a member of #self
		 */
		public bool is_member (string element) {
			Quark q = Quark.try_string (element);
			if (q != 0)
				return intset.is_member (q);
			else
				return false;
		}

		/**
		 * context_provider_string_set_is_equal:
		 * @self: a #ContextProviderStringSet
		 * @string_set: a #ContextProviderStringSet
		 *
		 * Find if #string_set contains exactly the same elements as #self
		 *
		 * Returns: TRUE if #string_set is equivalent #self
		 */
		public bool is_equal (StringSet string_set) {
			return intset.is_equal (string_set.intset);
		}


		/**
		 * context_provider_string_set_is_subset_of:
		 * @self: a #ContextProviderStringSet
		 * @string_set: a #ContextProviderStringSet
		 *
		 * Find if all the elements of #self are contained in #string_set
		 *
		 * Returns: TRUE if #string_set is a subset of #self
		 */
		public bool is_subset_of (StringSet string_set) {
			return intset.is_subset_of (string_set.intset);
		}

		/**
		 * context_provider_string_set_is_disjoint:
		 * @self: a #ContextProviderStringSet
		 * @string_set: a #ContextProviderStringSet
		 *
		 * Find if #string_set contains exactly no elements in common with #self
		 *
		 * Returns: TRUE if #string_set is disjoint with #self
		 */
		public bool is_disjoint (StringSet string_set) {
			return intset.is_disjoint (string_set.intset);
		}

		/**
		 * ContextProviderStringSetIter:
		 *
		 * A structure used for iterating over a #ContextKitStringSet.
		 */
		public class Iter {
			StringSet parent;
			IntSetIter iter;

			private Iter (StringSet parent) {
				this.parent = parent;
				this.iter = IntSetIter (parent.intset);
			}
			/**
			 * context_provider_string_set_iter_next:
			 * @self: a #ContextProviderStringSetIter
			 *
			 * Move the iterator onto the next element in the string set
			 *
			 * Returns: FALSE if there are no more elements in the set, TRUE otherwise.
			 */
			public bool next() {
				return iter.next();
			}

			/**
			 * context_provider_string_set_iter_get:
			 * @self: a #ContextProviderStringSetIter
			 *
			 * Get the string currently pointed to by this iterator
			 *
			 * Returns: a string from the string set
			 */
			public string get() {
				return ((Quark) iter.element).to_string();
			}

		}


		/**
		 * context_provider_string_set_iterator:
		 * @self: a #ContextProviderStringSet
		 *
		 * Return a #ContextProviderStringSetIter onto @self.
		 *
		 * Returns: a ContextProviderStringSetIter
		 */
		public StringSet.Iter iterator () {
			StringSet.Iter iter = new StringSet.Iter(this);
			return iter;
		}

		public Gee.ArrayList<string> to_array () {

			IntFunc iter = (i, data) => {
				weak Gee.ArrayList<string> ret = (Gee.ArrayList<string>) data;
				ret.add (((Quark)i).to_string());
			};

			Gee.ArrayList<string> ret = new Gee.ArrayList<string>();
			intset.foreach (iter, ret);

			return ret;
		}

		/**
		 * context_provider_string_set_debug:
		 * @self: a #ContextProviderStringSet
		 *
		 * Used to print the contents of a stringset.
		 *
		 * Returns: a string with the contents of "#self comma seperated.
		 */
		public string debug () {
			Gee.ArrayList<string> strings = to_array();
			if (strings.size < 1) {
				return "";
			}

			string ret = strings[0];
			for (int i=1; i<strings.size; i++) {
				ret = ret + ", " + strings[i];
			}
			return ret;
		}

	}
}