summaryrefslogtreecommitdiff
path: root/gee/arraylist.vala
blob: 97ce9292d35a4f28a96ce03710e365d01a7b46d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* arraylist.vala
 *
 * Copyright (C) 2004-2005  Novell, Inc
 * Copyright (C) 2005  David Waite
 * Copyright (C) 2007-2008  Jürg Billeter
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Jürg Billeter <j@bitron.ch>
 */

using GLib;

/**
 * Arrays of arbitrary elements which grow automatically as elements are added.
 */
public class Gee.ArrayList<G> : Object, Iterable<G>, Collection<G>, List<G> {
	public int size {
		get { return _size; }
	}

	public EqualFunc equal_func {
		set { _equal_func = value; }
	}

	private G[] _items = new G[4];
	private int _size;
	private EqualFunc _equal_func;

	// concurrent modification protection
	private int _stamp = 0;

	public ArrayList (EqualFunc equal_func = GLib.direct_equal) {
		this.equal_func = equal_func;
	}

	public Type get_element_type () {
		return typeof (G);
	}

	public Gee.Iterator<G> iterator () {
		return new Iterator<G> (this);
	}

	public bool contains (G item) {
		return (index_of (item) != -1);
	}

	public int index_of (G item) {
		for (int index = 0; index < _size; index++) {
			if (_equal_func (_items[index], item)) {
				return index;
			}
		}
		return -1;
	}

	public G? get (int index) {
		assert (index >= 0 && index < _size);

		return _items[index];
	}

	public void set (int index, G item) {
		assert (index >= 0 && index < _size);

		_items[index] = item;
	}

	public bool add (G item) {
		if (_size == _items.length) {
			grow_if_needed (1);
		}
		_items[_size++] = item;
		_stamp++;
		return true;
	}

	public void insert (int index, G item) {
		assert (index >= 0 && index <= _size);

		if (_size == _items.length) {
			grow_if_needed (1);
		}
		shift (index, 1);
		_items[index] = item;
		_stamp++;
	}

	public bool remove (G item) {
		for (int index = 0; index < _size; index++) {
			if (_equal_func (_items[index], item)) {
				remove_at (index);
				return true;
			}
		}
		return false;
	}

	public void remove_at (int index) {
		assert (index >= 0 && index < _size);

		_items[index] = null;

		shift (index + 1, -1);

		_stamp++;
	}

	public void clear () {
		for (int index = 0; index < _size; index++) {
			_items[index] = null;
		}
		_size = 0;
		_stamp++;
	}

	private void shift (int start, int delta) {
		assert (start >= 0 && start <= _size && start >= -delta);

		_items.move (start, start + delta, _size - start);

		_size += delta;
	}

	private void grow_if_needed (int new_count) {
		assert (new_count >= 0);

		int minimum_size = _size + new_count;
		if (minimum_size > _items.length) {
			// double the capacity unless we add even more items at this time
			set_capacity (new_count > _items.length ? minimum_size : 2 * _items.length);
		}
	}

	private void set_capacity (int value) {
		assert (value >= _size);

		_items.resize (value);
	}

	private class Iterator<G> : Object, Gee.Iterator<G> {
		public ArrayList<G> list {
			set {
				_list = value;
				_stamp = _list._stamp;
			}
		}

		private ArrayList<G> _list;
		private int _index = -1;

		// concurrent modification protection
		public int _stamp = 0;

		public Iterator (ArrayList list) {
			this.list = list;
		}

		public bool next () {
			assert (_stamp == _list._stamp);
			if (_index < _list._size) {
				_index++;
			}
			return (_index < _list._size);
		}

		public G? get () {
			assert (_stamp == _list._stamp);

			if (_index < 0 || _index >= _list._size) {
				return null;
			}

			return _list.get (_index);
		}
	}
}