Adding first code drop
[demorecorder] / src / SimpleHashMap.vala
1 /*  Demo Recorder for MAEMO 5
2 *   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
3 *   This program is free software; you can redistribute it and/or modify
4 *   it under the terms of the GNU General Public License version 2,
5 *   or (at your option) any later version, as published by the Free
6 *   Software Foundation
7 *
8 *   This program is distributed in the hope that it will be useful,
9 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *   GNU General Public License for more details
12 *
13 *   You should have received a copy of the GNU General Public
14 *   License along with this program; if not, write to the
15 *   Free Software Foundation, Inc.,
16 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 namespace IdWorks {
19
20 public class SimpleHashMap<T> : Object {
21
22   List<string> _keys;
23   List<T> _values;
24   
25   public List<string> keys {
26     get {
27       return _keys;
28     }
29   }
30   
31   public List<T> values {
32     get {
33       return _values;
34     }
35   }
36   
37   construct {
38     _keys = new List<string>();
39     _values = new List<T>();
40   }
41   
42   public new void set(string key, T val) {
43     for (int idx = 0; idx < _keys.length(); ++idx) {
44       if (key == _keys.nth_data(idx)) {
45         // delete value at idx
46         _values.delete_link(_values.nth(idx));
47         // insert value at idx
48         _values.insert(val, idx);
49         return;
50       }
51     }
52     _keys.append(key);
53     _values.append(val);
54   }
55   
56   public new T get(string key) {
57     for (int idx = 0; idx < _keys.length(); ++idx) {
58       if (key == _keys.nth_data(idx)) {
59         return _values.nth_data(idx);
60       }
61     }
62     return null;
63   }
64   
65 }
66
67 }