Release 0.41.92
[vala-gnome.git] / gee / collection.vala
blob84162232a7cfeebebda41e21bd161910427e898b
1 /* collection.vala
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
19 * Author:
20 * Jürg Billeter <j@bitron.ch>
23 /**
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> {
28 /**
29 * The number of items in this collection.
31 public abstract int size { get; }
33 /**
34 * Specifies whether this collection is empty.
36 public virtual bool is_empty { get { return size == 0; } }
38 /**
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);
47 /**
48 * Adds an item to this collection. Must not be called on read-only
49 * collections.
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);
57 /**
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);
67 /**
68 * Removes all items from this collection. Must not be called on
69 * read-only collections.
71 public abstract void clear ();
73 /**
74 * Adds all items in the input collection to this collection.
76 * @param collection the collection which items will be added to this
77 * collection.
79 * @return ``true`` if the collection has been changed, ``false`` otherwise
81 public virtual bool add_all (Collection<G> collection) {
82 bool changed = false;
83 for (Iterator<G> iter = collection.iterator (); iter.next ();) {
84 G item = iter.get ();
85 if (!contains (item)) {
86 add (item);
87 changed = true;
90 return changed;
93 /**
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 () {
99 var t = typeof (G);
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);
124 } else {
125 G[] array = new G[size];
126 int index = 0;
127 foreach (G element in this) {
128 array[index++] = (owned)element;
130 return array;
134 private static bool[] to_bool_array (Collection<bool> coll) {
135 bool[] array = new bool[coll.size];
136 int index = 0;
137 foreach (bool element in coll) {
138 array[index++] = element;
140 return array;
143 private static char[] to_char_array (Collection<char> coll) {
144 char[] array = new char[coll.size];
145 int index = 0;
146 foreach (char element in coll) {
147 array[index++] = element;
149 return array;
152 private static uchar[] to_uchar_array (Collection<uchar> coll) {
153 uchar[] array = new uchar[coll.size];
154 int index = 0;
155 foreach (uchar element in coll) {
156 array[index++] = element;
158 return array;
161 private static int[] to_int_array (Collection<int> coll) {
162 int[] array = new int[coll.size];
163 int index = 0;
164 foreach (int element in coll) {
165 array[index++] = element;
167 return array;
170 private static uint[] to_uint_array (Collection<uint> coll) {
171 uint[] array = new uint[coll.size];
172 int index = 0;
173 foreach (uint element in coll) {
174 array[index++] = element;
176 return array;
179 private static int64?[] to_int64_array (Collection<int64?> coll) {
180 int64?[] array = new int64?[coll.size];
181 int index = 0;
182 foreach (int64? element in coll) {
183 array[index++] = (owned)element;
185 return array;
188 private static uint64?[] to_uint64_array (Collection<uint64?> coll) {
189 uint64?[] array = new uint64?[coll.size];
190 int index = 0;
191 foreach (uint64? element in coll) {
192 array[index++] = (owned)element;
194 return array;
197 private static long[] to_long_array (Collection<long> coll) {
198 long[] array = new long[coll.size];
199 int index = 0;
200 foreach (long element in coll) {
201 array[index++] = element;
203 return array;
206 private static ulong[] to_ulong_array (Collection<ulong> coll) {
207 ulong[] array = new ulong[coll.size];
208 int index = 0;
209 foreach (ulong element in coll) {
210 array[index++] = element;
212 return array;
215 private static float?[] to_float_array (Collection<float?> coll) {
216 float?[] array = new float?[coll.size];
217 int index = 0;
218 foreach (float? element in coll) {
219 array[index++] = (owned)element;
221 return array;
224 private static double?[] to_double_array (Collection<double?> coll) {
225 double?[] array = new double?[coll.size];
226 int index = 0;
227 foreach (double? element in coll) {
228 array[index++] = (owned)element;
230 return array;