Line data Source code
1 : /* collection.vala
2 : *
3 : * Copyright (C) 2007 Jürg Billeter
4 : *
5 : * This library is free software; you can redistribute it and/or
6 : * modify it under the terms of the GNU Lesser General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 2.1 of the License, or (at your option) any later version.
9 :
10 : * This library is distributed in the hope that it will be useful,
11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 :
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with this library; if not, write to the Free Software
17 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 : *
19 : * Author:
20 : * Jürg Billeter <j@bitron.ch>
21 : */
22 :
23 : /**
24 : * Serves as the base interface for implementing collection classes. Defines
25 : * size, iteration, and modification methods.
26 : */
27 52660101 : public abstract class Vala.Collection<G> : Iterable<G> {
28 : /**
29 : * The number of items in this collection.
30 : */
31 339577166 : public abstract int size { get; }
32 :
33 : /**
34 : * Specifies whether this collection is empty.
35 : */
36 26 : public virtual bool is_empty { get { return size == 0; } }
37 :
38 : /**
39 : * Determines whether this collection contains the specified item.
40 : *
41 : * @param item the item to locate in the collection
42 : *
43 : * @return true if item is found, false otherwise
44 : */
45 8682486 : public abstract bool contains (G item);
46 :
47 : /**
48 : * Adds an item to this collection. Must not be called on read-only
49 : * collections.
50 : *
51 : * @param item the item to add to the collection
52 : *
53 : * @return true if the collection has been changed, false otherwise
54 : */
55 55090570 : public abstract bool add (G item);
56 :
57 : /**
58 : * Removes the first occurrence of an item from this collection. Must not
59 : * be called on read-only collections.
60 : *
61 : * @param item the item to remove from the collection
62 : *
63 : * @return true if the collection has been changed, false otherwise
64 : */
65 15054 : public abstract bool remove (G item);
66 :
67 : /**
68 : * Removes all items from this collection. Must not be called on
69 : * read-only collections.
70 : */
71 1801256 : public abstract void clear ();
72 :
73 : /**
74 : * Adds all items in the input collection to this collection.
75 : *
76 : * @param collection the collection which items will be added to this
77 : * collection.
78 : *
79 : * @return ``true`` if the collection has been changed, ``false`` otherwise
80 : */
81 20708 : public virtual bool add_all (Collection<G> collection) {
82 10354 : bool changed = false;
83 57940 : for (Iterator<G> iter = collection.iterator (); iter.next ();) {
84 23793 : G item = iter.get ();
85 23793 : if (!contains (item)) {
86 23793 : add (item);
87 23793 : changed = true;
88 : }
89 : }
90 : return changed;
91 : }
92 :
93 : /**
94 : * Returns an array containing all of items from this collection.
95 : *
96 : * @return an array containing all of items from this collection
97 : */
98 0 : public virtual G[] to_array () {
99 0 : var t = typeof (G);
100 0 : if (t == typeof (bool)) {
101 0 : return (G[]) to_bool_array ((Collection<bool>) this);
102 : } else if (t == typeof (char)) {
103 0 : return (G[]) to_char_array ((Collection<char>) this);
104 : } else if (t == typeof (uchar)) {
105 0 : return (G[]) to_uchar_array ((Collection<uchar>) this);
106 : } else if (t == typeof (int)) {
107 0 : return (G[]) to_int_array ((Collection<int>) this);
108 : } else if (t == typeof (uint)) {
109 0 : return (G[]) to_uint_array ((Collection<uint>) this);
110 : } else if (t == typeof (int64)) {
111 0 : return (G[]) to_int64_array ((Collection<int64?>) this);
112 : } else if (t == typeof (uint64)) {
113 0 : return (G[]) to_uint64_array ((Collection<uint64?>) this);
114 : } else if (t == typeof (long)) {
115 0 : return (G[]) to_long_array ((Collection<long>) this);
116 : } else if (t == typeof (ulong)) {
117 0 : return (G[]) to_ulong_array ((Collection<ulong>) this);
118 : } else if (t == typeof (float)) {
119 0 : return (G[]) to_float_array ((Collection<float?>) this);
120 : } else if (t == typeof (double)) {
121 0 : return (G[]) to_double_array ((Collection<double?>) this);
122 0 : } else if (t.is_enum () || t.is_flags ()) {
123 0 : return (G[]) to_int_array ((Collection<int>) this);
124 : } else {
125 0 : G[] array = new G[size];
126 0 : int index = 0;
127 0 : foreach (G element in this) {
128 0 : array[index++] = (owned)element;
129 : }
130 0 : return array;
131 : }
132 : }
133 :
134 0 : private static bool[] to_bool_array (Collection<bool> coll) {
135 0 : bool[] array = new bool[coll.size];
136 0 : int index = 0;
137 0 : foreach (bool element in coll) {
138 0 : array[index++] = element;
139 : }
140 0 : return array;
141 : }
142 :
143 0 : private static char[] to_char_array (Collection<char> coll) {
144 0 : char[] array = new char[coll.size];
145 0 : int index = 0;
146 0 : foreach (char element in coll) {
147 0 : array[index++] = element;
148 : }
149 0 : return array;
150 : }
151 :
152 0 : private static uchar[] to_uchar_array (Collection<uchar> coll) {
153 0 : uchar[] array = new uchar[coll.size];
154 0 : int index = 0;
155 0 : foreach (uchar element in coll) {
156 0 : array[index++] = element;
157 : }
158 0 : return array;
159 : }
160 :
161 0 : private static int[] to_int_array (Collection<int> coll) {
162 0 : int[] array = new int[coll.size];
163 0 : int index = 0;
164 0 : foreach (int element in coll) {
165 0 : array[index++] = element;
166 : }
167 0 : return array;
168 : }
169 :
170 0 : private static uint[] to_uint_array (Collection<uint> coll) {
171 0 : uint[] array = new uint[coll.size];
172 0 : int index = 0;
173 0 : foreach (uint element in coll) {
174 0 : array[index++] = element;
175 : }
176 0 : return array;
177 : }
178 :
179 0 : private static int64?[] to_int64_array (Collection<int64?> coll) {
180 0 : int64?[] array = new int64?[coll.size];
181 0 : int index = 0;
182 0 : foreach (int64? element in coll) {
183 0 : array[index++] = (owned)element;
184 : }
185 0 : return array;
186 : }
187 :
188 0 : private static uint64?[] to_uint64_array (Collection<uint64?> coll) {
189 0 : uint64?[] array = new uint64?[coll.size];
190 0 : int index = 0;
191 0 : foreach (uint64? element in coll) {
192 0 : array[index++] = (owned)element;
193 : }
194 0 : return array;
195 : }
196 :
197 0 : private static long[] to_long_array (Collection<long> coll) {
198 0 : long[] array = new long[coll.size];
199 0 : int index = 0;
200 0 : foreach (long element in coll) {
201 0 : array[index++] = element;
202 : }
203 0 : return array;
204 : }
205 :
206 0 : private static ulong[] to_ulong_array (Collection<ulong> coll) {
207 0 : ulong[] array = new ulong[coll.size];
208 0 : int index = 0;
209 0 : foreach (ulong element in coll) {
210 0 : array[index++] = element;
211 : }
212 0 : return array;
213 : }
214 :
215 0 : private static float?[] to_float_array (Collection<float?> coll) {
216 0 : float?[] array = new float?[coll.size];
217 0 : int index = 0;
218 0 : foreach (float? element in coll) {
219 0 : array[index++] = (owned)element;
220 : }
221 0 : return array;
222 : }
223 :
224 0 : private static double?[] to_double_array (Collection<double?> coll) {
225 0 : double?[] array = new double?[coll.size];
226 0 : int index = 0;
227 0 : foreach (double? element in coll) {
228 0 : array[index++] = (owned)element;
229 : }
230 0 : return array;
231 : }
232 : }
233 :
|