3
* Copyright (C) 2008 Johann Prieur <johann.prieur@gmail.com>
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
24
namespace People.Utils {
26
* A BiMap defines a bidirectionnal mapping relationship between elements of
27
* classes {@link X} and {@link Y}.
29
public class BiMap<X, Y> : Object {
31
public GLib.HashFunc x_hash_func {
33
private set construct;
36
public GLib.EqualFunc x_equal_func {
38
private set construct;
41
public GLib.HashFunc y_hash_func {
43
private set construct;
46
public GLib.EqualFunc y_equal_func {
48
private set construct;
51
public Gee.Map<X, Y> x_to_y {
53
private set construct;
56
public Gee.Map<X, Y> y_to_x {
63
x_to_y = new Gee.HashMap<X, Y> (x_hash_func, x_equal_func, y_equal_func);
67
* Constructs this BiMap based on the given {@link Map}.
69
* @param x_to_y a mapping of {@link X} elements to {@link Y}
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}
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) {
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;
89
* Returns the image of the given element under this BiMap.
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
95
public Y get_y (X x) {
96
return x_to_y.get (x);
100
* Returns the image of the given element under this BiMap.
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
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);
110
foreach (X x in x_to_y.get_keys ()) {
111
y_to_x.set ((Y) (x_to_y.get (x)), x);
114
return y_to_x.get (y);
118
* Establishes a bidirectionnal mapping relationship between the given
119
* elements under this BiMap.
121
* @param x an element of {@link X}
122
* @param y an element of {@link Y}
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);
128
foreach (X x in x_to_y.get_keys ()) {
129
y_to_x.set ((Y) (x_to_y.get (x)), x);
132
if (x_to_y.contains(x)) {
133
y_to_x.remove (x_to_y.get(x));
136
if (y_to_x.contains(y)) {
137
x_to_y.remove (y_to_x.get(y));
145
* Establishes a bidirectionnal mapping relationship between the given
146
* elements under this BiMap.
148
* @param y an element of {@link Y}
149
* @param x an element of {@link X}
151
public void set_y (Y y, X x) {
156
* Removes the relationship involving the given element of {@link X}.
158
* @param x an element of {@link X}
160
public void remove_x (X x) {
162
y_to_x.remove (x_to_y.get (x));
167
* Removes the relationship involving the given element of {@link Y}.
169
* @param y an element of {@link Y}
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);
175
foreach (X x in x_to_y.get_keys ()) {
176
y_to_x.set ((Y) (x_to_y.get (x)), x);
179
x_to_y.remove (y_to_x.get (y));
184
* Returns the sub-set of {@link X} of elements used in this BiMap.
186
* @return a {@link Set} of elements of {@link X}
188
public Gee.Set<X> get_x_set () {
189
return new Gee.ReadOnlySet<X> (x_to_y.get_keys ());
193
* Returns the sub-set of {@link Y} of elements used in this BiMap.
195
* @return a {@link Set} of elements of {@link Y}
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);
201
foreach (X x in x_to_y.get_keys ()) {
202
var y = x_to_y.get (x);
206
return new Gee.ReadOnlySet<Y> (y_to_x.get_keys ());