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

« back to all changes in this revision

Viewing changes to libpeople-address-book/bimap.vala

  • Committer: Johann Prieur
  • Date: 2008-04-27 20:43:14 UTC
  • mfrom: (219.1.29 people-address-book)
  • Revision ID: johann.prieur@gmail.com-20080427204314-d4u0drkeyfb5x3ua
- Merged libpeople-address-book

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