~people-project/people-core/deep-refactoring

« back to all changes in this revision

Viewing changes to libs/utils/bimap.vala

  • Committer: Ali Sabil
  • Date: 2009-01-29 22:35:23 UTC
  • Revision ID: ali.sabil@gmail.com-20090129223523-f834u01bufze5r0e
- Removed the libs/ folder, depending now on libcore to provide these features

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* bimap.vala
2
 
 *
3
 
 * Copyright (C) 2008 Johann Prieur <johann.prieur@gmail.com>
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
 
 */
20
 
 
21
 
using Gee;
22
 
 
23
 
 
24
 
namespace People.Utils {
25
 
        /**
26
 
         * A BiMap defines a bidirectionnal mapping relationship between elements of
27
 
         * classes {@link X} and {@link Y}.
28
 
         */
29
 
        public class BiMap<X, Y> : Object {
30
 
 
31
 
                public GLib.HashFunc x_hash_func {
32
 
                        get;
33
 
                        private set construct;
34
 
                }
35
 
 
36
 
                public GLib.EqualFunc x_equal_func {
37
 
                        get;
38
 
                        private set construct;
39
 
                }
40
 
 
41
 
                public GLib.HashFunc y_hash_func {
42
 
                        get;
43
 
                        private set construct;
44
 
                }
45
 
 
46
 
                public GLib.EqualFunc y_equal_func {
47
 
                        get;
48
 
                        private set construct;
49
 
                }
50
 
 
51
 
                public Gee.Map<X, Y> x_to_y {
52
 
                        get;
53
 
                        private set construct;
54
 
                }
55
 
 
56
 
                public Gee.Map<X, Y> y_to_x {
57
 
                        get;
58
 
                        private set;
59
 
                }
60
 
 
61
 
                construct {
62
 
                        if (x_to_y == null)
63
 
                                x_to_y = new Gee.HashMap<X, Y> (x_hash_func, x_equal_func, y_equal_func);
64
 
                }
65
 
 
66
 
                /**
67
 
                 * Constructs this BiMap based on the given {@link Map}.
68
 
                 *
69
 
                 * @param x_to_y       a mapping of {@link X} elements to {@link Y}
70
 
                 *                     elements
71
 
                 * @param x_hash_func  hash function for elements of {@link X}
72
 
                 * @param x_equal_func equal function for elements of {@link X}
73
 
                 * @param y_hash_func  hash function for elements of {@link Y}
74
 
                 * @param y_equal_func equal function for elements of {@link Y}
75
 
                 */
76
 
                public BiMap (Gee.Map<X, Y>? x_to_y=null,
77
 
                                GLib.HashFunc x_hash_func = GLib.direct_hash,
78
 
                                GLib.EqualFunc x_equal_func = GLib.direct_equal,
79
 
                                GLib.HashFunc y_hash_func = GLib.direct_hash,
80
 
                                GLib.EqualFunc y_equal_func= GLib.direct_equal) {
81
 
                        this.x_to_y = x_to_y;
82
 
                        this.x_hash_func = x_hash_func;
83
 
                        this.x_equal_func = x_equal_func;
84
 
                        this.y_hash_func = y_hash_func;
85
 
                        this.y_equal_func = y_equal_func;
86
 
                }
87
 
 
88
 
                /**
89
 
                 * Returns the image of the given element under this BiMap.
90
 
                 *
91
 
                 * @param x an element of {@link X}
92
 
                 * @return  an element of {@link Y} or <code>NULL</code> of the
93
 
                 *          relationship does not exist
94
 
                 */
95
 
                public Y get_y (X x) {
96
 
                        return x_to_y.get (x);
97
 
                }
98
 
 
99
 
                /**
100
 
                 * Returns the image of the given element under this BiMap.
101
 
                 *
102
 
                 * @param x an element of {@link Y}
103
 
                 * @return  an element of {@link X} or <code>NULL</code> of the
104
 
                 *          relationship does not exist
105
 
                 */
106
 
                public X get_x (Y y) {
107
 
                        if (y_to_x == null) {
108
 
                                y_to_x = new Gee.HashMap<Y, X> (y_hash_func, y_equal_func, x_equal_func);
109
 
 
110
 
                                foreach (X x in x_to_y.get_keys ()) {
111
 
                                        y_to_x.set ((Y) (x_to_y.get (x)), x);
112
 
                                }
113
 
                        }
114
 
                        return y_to_x.get (y);
115
 
                }
116
 
 
117
 
                /**
118
 
                 * Establishes a bidirectionnal mapping relationship between the given
119
 
                 * elements under this BiMap.
120
 
                 *
121
 
                 * @param x an element of {@link X}
122
 
                 * @param y an element of {@link Y}
123
 
                 */
124
 
                public void set_x (X x, Y y) {
125
 
                        if (y_to_x == null) {
126
 
                                y_to_x = new Gee.HashMap<Y, X> (y_hash_func, y_equal_func, x_equal_func);
127
 
 
128
 
                                foreach (X x in x_to_y.get_keys ()) {
129
 
                                        y_to_x.set ((Y) (x_to_y.get (x)), x);
130
 
                                }
131
 
                        }
132
 
                        if (x_to_y.contains(x)) {
133
 
                                y_to_x.remove (x_to_y.get(x));
134
 
                                x_to_y.remove (x);
135
 
                        }
136
 
                        if (y_to_x.contains(y)) {
137
 
                                x_to_y.remove (y_to_x.get(y));
138
 
                                y_to_x.remove (y);
139
 
                        }
140
 
                        x_to_y.set (x, y);
141
 
                        y_to_x.set (y, x);
142
 
                }
143
 
 
144
 
                /**
145
 
                 * Establishes a bidirectionnal mapping relationship between the given
146
 
                 * elements under this BiMap.
147
 
                 *
148
 
                 * @param y an element of {@link Y}
149
 
                 * @param x an element of {@link X}
150
 
                 */
151
 
                public void set_y (Y y, X x) {
152
 
                        set_x (x, y);
153
 
                }
154
 
 
155
 
                /**
156
 
                 * Removes the relationship involving the given element of {@link X}.
157
 
                 *
158
 
                 * @param x an element of {@link X}
159
 
                 */
160
 
                public void remove_x (X x) {
161
 
                        if (y_to_x != null)
162
 
                                y_to_x.remove (x_to_y.get (x));
163
 
                        x_to_y.remove (x);
164
 
                }
165
 
 
166
 
                /**
167
 
                 * Removes the relationship involving the given element of {@link Y}.
168
 
                 *
169
 
                 * @param y an element of {@link Y}
170
 
                 */
171
 
                public void remove_y (Y y) {
172
 
                        if (y_to_x == null) {
173
 
                                y_to_x = new Gee.HashMap<Y, X> (y_hash_func, y_equal_func, x_equal_func);
174
 
 
175
 
                                foreach (X x in x_to_y.get_keys ()) {
176
 
                                        y_to_x.set ((Y) (x_to_y.get (x)), x);
177
 
                                }
178
 
                        }
179
 
                        x_to_y.remove (y_to_x.get (y));
180
 
                        y_to_x.remove (y);
181
 
                }
182
 
 
183
 
                /**
184
 
                 * Returns the sub-set of {@link X} of elements used in this BiMap.
185
 
                 *
186
 
                 * @return a {@link Set} of elements of {@link X}
187
 
                 */
188
 
                public Gee.Set<X> get_x_set () {
189
 
                        return new Gee.ReadOnlySet<X> (x_to_y.get_keys ());
190
 
                }
191
 
 
192
 
                /**
193
 
                 * Returns the sub-set of {@link Y} of elements used in this BiMap.
194
 
                 *
195
 
                 * @return a {@link Set} of elements of {@link Y}
196
 
                 */
197
 
                public Gee.Set<Y> get_y_set () {
198
 
                        if (y_to_x == null) {
199
 
                                y_to_x = new Gee.HashMap<Y, X> (y_hash_func, y_equal_func, x_equal_func);
200
 
 
201
 
                                foreach (X x in x_to_y.get_keys ()) {
202
 
                                        var y = x_to_y.get (x);
203
 
                                        y_to_x.set (y, x);
204
 
                                }
205
 
                        }
206
 
                        return new Gee.ReadOnlySet<Y> (y_to_x.get_keys ());
207
 
                }
208
 
        }
209
 
 
210
 
}