3 * Copyright (C) 2007 Jürg Billeter
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.
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.
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
20 * Jürg Billeter <j@bitron.ch>
24 * Serves as the base interface for implementing collection classes. Defines
25 * size, iteration, and modification methods.
27 public abstract class Vala
.Collection
<G
> : Iterable
<G
> {
29 * The number of items in this collection.
31 public abstract int size
{ get; }
34 * Specifies whether this collection is empty.
36 public virtual bool is_empty
{ get { return size
== 0; } }
39 * Determines whether this collection contains the specified item.
41 * @param item the item to locate in the collection
43 * @return true if item is found, false otherwise
45 public abstract bool contains (G item
);
48 * Adds an item to this collection. Must not be called on read-only
51 * @param item the item to add to the collection
53 * @return true if the collection has been changed, false otherwise
55 public abstract bool add (G item
);
58 * Removes the first occurrence of an item from this collection. Must not
59 * be called on read-only collections.
61 * @param item the item to remove from the collection
63 * @return true if the collection has been changed, false otherwise
65 public abstract bool remove (G item
);
68 * Removes all items from this collection. Must not be called on
69 * read-only collections.
71 public abstract void clear ();
74 * Adds all items in the input collection to this collection.
76 * @param collection the collection which items will be added to this
79 * @return ``true`` if the collection has been changed, ``false`` otherwise
81 public virtual bool add_all (Collection
<G
> collection
) {
83 for (Iterator
<G
> iter
= collection
.iterator (); iter
.next ();) {
85 if (!contains (item
)) {
94 * Returns an array containing all of items from this collection.
96 * @return an array containing all of items from this collection
98 public virtual G
[] to_array () {
100 if (t
== typeof (bool)) {
101 return (G
[]) to_bool_array ((Collection
<bool>) this
);
102 } else if (t
== typeof (char)) {
103 return (G
[]) to_char_array ((Collection
<char>) this
);
104 } else if (t
== typeof (uchar)) {
105 return (G
[]) to_uchar_array ((Collection
<uchar>) this
);
106 } else if (t
== typeof (int)) {
107 return (G
[]) to_int_array ((Collection
<int>) this
);
108 } else if (t
== typeof (uint)) {
109 return (G
[]) to_uint_array ((Collection
<uint>) this
);
110 } else if (t
== typeof (int64)) {
111 return (G
[]) to_int64_array ((Collection
<int64>) this
);
112 } else if (t
== typeof (uint64)) {
113 return (G
[]) to_uint64_array ((Collection
<uint64>) this
);
114 } else if (t
== typeof (long)) {
115 return (G
[]) to_long_array ((Collection
<long>) this
);
116 } else if (t
== typeof (ulong)) {
117 return (G
[]) to_ulong_array ((Collection
<ulong>) this
);
118 } else if (t
== typeof (float)) {
119 return (G
[]) to_float_array ((Collection
<float>) this
);
120 } else if (t
== typeof (double)) {
121 return (G
[]) to_double_array ((Collection
<double>) this
);
122 } else if (t
.is_enum () || t
.is_flags ()) {
123 return (G
[]) to_int_array ((Collection
<int>) this
);
125 G
[] array
= new G
[size
];
127 foreach (G element
in this
) {
128 array
[index
++] = (owned
)element
;
134 private static bool[] to_bool_array (Collection
<bool> coll
) {
135 bool[] array
= new
bool[coll
.size
];
137 foreach (bool element
in coll
) {
138 array
[index
++] = element
;
143 private static char[] to_char_array (Collection
<char> coll
) {
144 char[] array
= new
char[coll
.size
];
146 foreach (char element
in coll
) {
147 array
[index
++] = element
;
152 private static uchar[] to_uchar_array (Collection
<uchar> coll
) {
153 uchar[] array
= new
uchar[coll
.size
];
155 foreach (uchar element
in coll
) {
156 array
[index
++] = element
;
161 private static int[] to_int_array (Collection
<int> coll
) {
162 int[] array
= new
int[coll
.size
];
164 foreach (int element
in coll
) {
165 array
[index
++] = element
;
170 private static uint[] to_uint_array (Collection
<uint> coll
) {
171 uint[] array
= new
uint[coll
.size
];
173 foreach (uint element
in coll
) {
174 array
[index
++] = element
;
179 private static int64?
[] to_int64_array (Collection
<int64?
> coll
) {
180 int64?
[] array
= new
int64?
[coll
.size
];
182 foreach (int64? element
in coll
) {
183 array
[index
++] = (owned
)element
;
188 private static uint64?
[] to_uint64_array (Collection
<uint64?
> coll
) {
189 uint64?
[] array
= new
uint64?
[coll
.size
];
191 foreach (uint64? element
in coll
) {
192 array
[index
++] = (owned
)element
;
197 private static long[] to_long_array (Collection
<long> coll
) {
198 long[] array
= new
long[coll
.size
];
200 foreach (long element
in coll
) {
201 array
[index
++] = element
;
206 private static ulong[] to_ulong_array (Collection
<ulong> coll
) {
207 ulong[] array
= new
ulong[coll
.size
];
209 foreach (ulong element
in coll
) {
210 array
[index
++] = element
;
215 private static float?
[] to_float_array (Collection
<float?
> coll
) {
216 float?
[] array
= new
float?
[coll
.size
];
218 foreach (float? element
in coll
) {
219 array
[index
++] = (owned
)element
;
224 private static double?
[] to_double_array (Collection
<double?
> coll
) {
225 double?
[] array
= new
double?
[coll
.size
];
227 foreach (double? element
in coll
) {
228 array
[index
++] = (owned
)element
;