~ubuntu-branches/ubuntu/precise/folks/precise-201306070638

« back to all changes in this revision

Viewing changes to folks/linked-hash-set.vala

  • Committer: Bazaar Package Importer
  • Author(s): Brian Curtis
  • Date: 2011-02-02 14:22:14 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20110202142214-ngpuu3mhjxyl430f
Tags: 0.3.4-0ubuntu1
* New Upstream Release
  - libfolks-telepathy.so exports private symbols
  - Add a HACKING file that outlines development policies and coding style
  - Review coding conventions in folks
  - Add folks command line application
  - libfolks hard-codes backend names for debugging
  - Print stack traces for failed tests to improve remote debugging
  - Add static aggregation tests
  - Logger service unavailable in make check
  - Add tests for LinkedHashSet
  - Use better interface names
* debian/control
  -changed Maintainer to XSBC-Original-Maintainer
  -added Maintainer of Ubuntu Core Developers
  -changed libfolks19* > libfolks20*
* debian
  -changed .install and .symbols files from 19 > 20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Collabora Ltd.
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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/>.
 
16
 *
 
17
 * Authors:
 
18
 *       Eitan Isaacson <eitan.isaacson@collabora.co.uk>
 
19
 */
 
20
 
 
21
using Gee;
 
22
 
 
23
/**
 
24
 * Linked list implementation of the {@link Gee.Set} interface.
 
25
 * This implementation provides an ordered set with predictable iteration.
 
26
 *
 
27
 * @since 0.3.4
 
28
 */
 
29
public class Folks.LinkedHashSet<G> : AbstractList<G>,
 
30
    Set<G>
 
31
{
 
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;
 
36
 
 
37
  /**
 
38
   * Constructs a new empty set.
 
39
   *
 
40
   * If no function parameters are provided, the default functions for the
 
41
   * set's item type are used.
 
42
   *
 
43
   * @param hash_func an optional hash function
 
44
   * @param equal_func an optional equality testing function
 
45
   *
 
46
   * @since 0.3.4
 
47
   */
 
48
  public LinkedHashSet (HashFunc? hash_func = null,
 
49
      EqualFunc? equal_func = null)
 
50
  {
 
51
    this._hash_set = new HashSet<G> (hash_func, equal_func);
 
52
    this._linked_list = new LinkedList<G> (equal_func);
 
53
  }
 
54
 
 
55
  /**
 
56
   * The number of items in this collection.
 
57
   *
 
58
   * @see Gee.AbstractCollection.size
 
59
   *
 
60
   * @since 0.3.4
 
61
   */
 
62
  public override int size
 
63
  {
 
64
    get { return this._linked_list.size; }
 
65
  }
 
66
 
 
67
  /**
 
68
   * Returns whether this structure contains the given item.
 
69
   *
 
70
   * @param item the element to find
 
71
   *
 
72
   * @return `true` if this collection contains the specified item.
 
73
   * @see Gee.AbstractCollection.contains
 
74
   *
 
75
   * @since 0.3.4
 
76
   */
 
77
  public override bool contains (G item)
 
78
  {
 
79
    return this._hash_set.contains (item);
 
80
  }
 
81
 
 
82
  /**
 
83
   * Add the given element.
 
84
   *
 
85
   * @param item element to add
 
86
   *
 
87
   * @return `true` if the element was added.
 
88
   * @see Gee.AbstractCollection.add
 
89
   *
 
90
   * @since 0.3.4
 
91
   */
 
92
  public override bool add (G item)
 
93
  {
 
94
    if (this._hash_set.add (item))
 
95
      {
 
96
        this._linked_list.add (item);
 
97
        return true;
 
98
      }
 
99
 
 
100
    return false;
 
101
  }
 
102
 
 
103
  /**
 
104
   * Remove the instance of the given element.
 
105
   *
 
106
   * @param item element to remove
 
107
   *
 
108
   * @return `true` if the element was removed.
 
109
   * @see Gee.AbstractCollection.remove
 
110
   *
 
111
   * @since 0.3.4
 
112
   */
 
113
  public override bool remove (G item)
 
114
  {
 
115
    if (this._hash_set.remove (item))
 
116
      {
 
117
        this._linked_list.remove (item);
 
118
        return true;
 
119
      }
 
120
 
 
121
    return false;
 
122
  }
 
123
 
 
124
  /**
 
125
   * Removes all items from this collection. Must not be called on read-only
 
126
   * collections.
 
127
   *
 
128
   * @see Gee.AbstractCollection.clear
 
129
   *
 
130
   * @since 0.3.4
 
131
   */
 
132
  public override void clear ()
 
133
  {
 
134
    this._hash_set.clear ();
 
135
    this._linked_list.clear ();
 
136
  }
 
137
 
 
138
  /**
 
139
   * Returns the item at the given position.
 
140
   *
 
141
   * @param index the position of an element to retrieve.
 
142
   *
 
143
   * @return the item at the specified index in this list.
 
144
   * @see Gee.AbstractList.get
 
145
   *
 
146
   * @since 0.3.4
 
147
   */
 
148
  public override G get (int index)
 
149
  {
 
150
    return this._linked_list.get (index);
 
151
  }
 
152
 
 
153
  /**
 
154
   * Unimplemented method (incompatable with ordered set).
 
155
   */
 
156
  public override void set (int index, G item)
 
157
  {
 
158
    assert_not_reached ();
 
159
  }
 
160
 
 
161
  /**
 
162
   * Unimplemented method (incompatable with ordered set).
 
163
   */
 
164
  public override void insert (int index, G item)
 
165
  {
 
166
    assert_not_reached ();
 
167
  }
 
168
 
 
169
  /**
 
170
   * Returns the position of the given item.
 
171
   *
 
172
   * @param item an element to find within this structure.
 
173
   *
 
174
   * @return the index of the occurence of the specified item in this list.
 
175
   * @see Gee.AbstractList.index_of
 
176
   *
 
177
   * @since 0.3.4
 
178
   */
 
179
  public override int index_of (G item)
 
180
  {
 
181
    if (!this._hash_set.contains (item))
 
182
      return -1;
 
183
    return this._linked_list.index_of (item);
 
184
  }
 
185
 
 
186
  /**
 
187
   * Remove the element at the given index.
 
188
   *
 
189
   * @param index position of the element to remove.
 
190
   *
 
191
   * @return the removed element.
 
192
   * @see Gee.AbstractList.remove_at
 
193
   *
 
194
   * @since 0.3.4
 
195
   */
 
196
  public override G remove_at (int index)
 
197
  {
 
198
    G item = this._linked_list.remove_at (index);
 
199
    if (item != null)
 
200
      this._hash_set.remove (item);
 
201
    return item;
 
202
  }
 
203
 
 
204
  /**
 
205
   * Returns a new sub-list of this structure. Caller does not own the new
 
206
   * list's elements.
 
207
   *
 
208
   * @param start position of first element in sub-list
 
209
   * @param stop position of last element in sub-list
 
210
   *
 
211
   * @return the sub-list specified by start and stop.
 
212
   * @see Gee.AbstractList.slice
 
213
   *
 
214
   * @since 0.3.4
 
215
   */
 
216
  public override Gee.List<G>? slice (int start, int stop)
 
217
  {
 
218
    return this._linked_list.slice (start, stop);
 
219
  }
 
220
 
 
221
  /**
 
222
   * Returns the first element in this structure.
 
223
   *
 
224
   * @return the first element in the structure. Fails if the structure is
 
225
   * empty.
 
226
   * @see Gee.AbstractList.first
 
227
   *
 
228
   * @since 0.3.4
 
229
   */
 
230
  public override G first ()
 
231
  {
 
232
    return this._linked_list.first ();
 
233
  }
 
234
 
 
235
  /**
 
236
   * Returns the first element in this structure.
 
237
   *
 
238
   * @return the last element in the structure. Fails if the structure is empty.
 
239
   * @see Gee.AbstractList.last
 
240
   *
 
241
   * @since 0.3.4
 
242
   */
 
243
  public override G last ()
 
244
  {
 
245
    return this._linked_list.last ();
 
246
  }
 
247
 
 
248
  /**
 
249
   * Adds all the elements of the given collection to this one (as necessary).
 
250
   *
 
251
   * @param collection a {@link Gee.Collection} of elements to add.
 
252
   *
 
253
   * @return `true` if new elements were added to the collection.
 
254
   * @see Gee.AbstractCollection.add_all
 
255
   *
 
256
   * @since 0.3.4
 
257
   */
 
258
  public override bool add_all (Collection<G> collection)
 
259
  {
 
260
    bool modified = false;
 
261
 
 
262
    foreach (G item in collection)
 
263
      modified |= add(item);
 
264
 
 
265
    return modified;
 
266
  }
 
267
 
 
268
 
 
269
  /**
 
270
   * Returns the Iterator for this structure.
 
271
   *
 
272
   * @return a {@link Gee.Iterator} that can be used for iteration over this
 
273
   * structure.
 
274
   * @see Gee.Iterator
 
275
   *
 
276
   * @since 0.3.4
 
277
   */
 
278
  public override Gee.Iterator<G> iterator ()
 
279
  {
 
280
    return this._linked_list.iterator ();
 
281
  }
 
282
 
 
283
  /**
 
284
   * Returns the ListIterator for this structure.
 
285
   *
 
286
   * @return a {@link Gee.ListIterator} that can be used for iteration over this
 
287
   * structure (as a list).
 
288
   * @see Gee.ListIterator
 
289
   *
 
290
   * @since 0.3.4
 
291
   */
 
292
  public override ListIterator<G> list_iterator ()
 
293
  {
 
294
    return this._linked_list.list_iterator ();
 
295
  }
 
296
}