2
* Copyright (C) 2011 Collabora Ltd.
4
* This library is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU Lesser General Public License as published by
6
* the Free Software Foundation, either version 2.1 of the License, or
7
* (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public License
15
* along with this library. If not, see <http://www.gnu.org/licenses/>.
18
* Eitan Isaacson <eitan.isaacson@collabora.co.uk>
24
* Linked list implementation of the {@link Gee.Set} interface.
25
* This implementation provides an ordered set with predictable iteration.
29
public class Folks.LinkedHashSet<G> : AbstractList<G>,
32
/* A hash set that maintains a unique set. */
33
private HashSet<G> _hash_set;
34
/* A linked list that maintains the order of the items. */
35
private LinkedList<G> _linked_list;
38
* Constructs a new empty set.
40
* If no function parameters are provided, the default functions for the
41
* set's item type are used.
43
* @param hash_func an optional hash function
44
* @param equal_func an optional equality testing function
48
public LinkedHashSet (HashFunc? hash_func = null,
49
EqualFunc? equal_func = null)
51
this._hash_set = new HashSet<G> (hash_func, equal_func);
52
this._linked_list = new LinkedList<G> (equal_func);
56
* The number of items in this collection.
58
* @see Gee.AbstractCollection.size
62
public override int size
64
get { return this._linked_list.size; }
68
* Returns whether this structure contains the given item.
70
* @param item the element to find
72
* @return `true` if this collection contains the specified item.
73
* @see Gee.AbstractCollection.contains
77
public override bool contains (G item)
79
return this._hash_set.contains (item);
83
* Add the given element.
85
* @param item element to add
87
* @return `true` if the element was added.
88
* @see Gee.AbstractCollection.add
92
public override bool add (G item)
94
if (this._hash_set.add (item))
96
this._linked_list.add (item);
104
* Remove the instance of the given element.
106
* @param item element to remove
108
* @return `true` if the element was removed.
109
* @see Gee.AbstractCollection.remove
113
public override bool remove (G item)
115
if (this._hash_set.remove (item))
117
this._linked_list.remove (item);
125
* Removes all items from this collection. Must not be called on read-only
128
* @see Gee.AbstractCollection.clear
132
public override void clear ()
134
this._hash_set.clear ();
135
this._linked_list.clear ();
139
* Returns the item at the given position.
141
* @param index the position of an element to retrieve.
143
* @return the item at the specified index in this list.
144
* @see Gee.AbstractList.get
148
public override G get (int index)
150
return this._linked_list.get (index);
154
* Unimplemented method (incompatable with ordered set).
156
public override void set (int index, G item)
158
assert_not_reached ();
162
* Unimplemented method (incompatable with ordered set).
164
public override void insert (int index, G item)
166
assert_not_reached ();
170
* Returns the position of the given item.
172
* @param item an element to find within this structure.
174
* @return the index of the occurence of the specified item in this list.
175
* @see Gee.AbstractList.index_of
179
public override int index_of (G item)
181
if (!this._hash_set.contains (item))
183
return this._linked_list.index_of (item);
187
* Remove the element at the given index.
189
* @param index position of the element to remove.
191
* @return the removed element.
192
* @see Gee.AbstractList.remove_at
196
public override G remove_at (int index)
198
G item = this._linked_list.remove_at (index);
200
this._hash_set.remove (item);
205
* Returns a new sub-list of this structure. Caller does not own the new
208
* @param start position of first element in sub-list
209
* @param stop position of last element in sub-list
211
* @return the sub-list specified by start and stop.
212
* @see Gee.AbstractList.slice
216
public override Gee.List<G>? slice (int start, int stop)
218
return this._linked_list.slice (start, stop);
222
* Returns the first element in this structure.
224
* @return the first element in the structure. Fails if the structure is
226
* @see Gee.AbstractList.first
230
public override G first ()
232
return this._linked_list.first ();
236
* Returns the first element in this structure.
238
* @return the last element in the structure. Fails if the structure is empty.
239
* @see Gee.AbstractList.last
243
public override G last ()
245
return this._linked_list.last ();
249
* Adds all the elements of the given collection to this one (as necessary).
251
* @param collection a {@link Gee.Collection} of elements to add.
253
* @return `true` if new elements were added to the collection.
254
* @see Gee.AbstractCollection.add_all
258
public override bool add_all (Collection<G> collection)
260
bool modified = false;
262
foreach (G item in collection)
263
modified |= add(item);
270
* Returns the Iterator for this structure.
272
* @return a {@link Gee.Iterator} that can be used for iteration over this
278
public override Gee.Iterator<G> iterator ()
280
return this._linked_list.iterator ();
284
* Returns the ListIterator for this structure.
286
* @return a {@link Gee.ListIterator} that can be used for iteration over this
287
* structure (as a list).
288
* @see Gee.ListIterator
292
public override ListIterator<G> list_iterator ()
294
return this._linked_list.list_iterator ();