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
27
* A BiMap defines a bidirectionnal mapping relationship between elements of
28
* classes {@link X} and {@link Y}.
30
public class BiMap<X, Y> : Object {
32
public GLib.HashFunc x_hash_func {
34
private set construct;
37
public GLib.EqualFunc x_equal_func {
39
private set construct;
42
public GLib.HashFunc y_hash_func {
44
private set construct;
47
public GLib.EqualFunc y_equal_func {
49
private set construct;
52
public Gee.Map<X, Y> x_to_y {
54
private set construct;
57
public Gee.Map<X, Y> y_to_x {
64
x_to_y = new Gee.HashMap<X, Y> (x_hash_func, x_equal_func, y_equal_func);
68
* Constructs this BiMap based on the given {@link Map}.
70
* @param x_to_y a mapping of {@link X} elements to {@link Y}
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}
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) {
85
* Returns the image of the given element under this BiMap.
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
91
public Y get_y (X x) {
92
return x_to_y.get (x);
96
* Returns the image of the given element under this BiMap.
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
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);
106
foreach (X x in x_to_y.get_keys ()) {
107
y_to_x.set ((Y) (x_to_y.get (x)), x);
110
return y_to_x.get (y);
114
* Establishes a bidirectionnal mapping relationship between the given
115
* elements under this BiMap.
117
* @param x an element of {@link X}
118
* @param y an element of {@link Y}
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);
124
foreach (X x in x_to_y.get_keys ()) {
125
y_to_x.set ((Y) (x_to_y.get (x)), x);
128
if (x_to_y.contains(x)) {
129
y_to_x.remove (x_to_y.get(x));
132
if (y_to_x.contains(y)) {
133
x_to_y.remove (y_to_x.get(y));
141
* Establishes a bidirectionnal mapping relationship between the given
142
* elements under this BiMap.
144
* @param y an element of {@link Y}
145
* @param x an element of {@link X}
147
public void set_y (Y y, X x) {
152
* Removes the relationship involving the given element of {@link X}.
154
* @param x an element of {@link X}
156
public void remove_x (X x) {
158
y_to_x.remove (x_to_y.get (x));
163
* Removes the relationship involving the given element of {@link Y}.
165
* @param y an element of {@link Y}
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);
171
foreach (X x in x_to_y.get_keys ()) {
172
y_to_x.set ((Y) (x_to_y.get (x)), x);
175
x_to_y.remove (y_to_x.get (y));
180
* Returns the sub-set of {@link X} of elements used in this BiMap.
182
* @return a {@link Set} of elements of {@link X}
184
public Gee.Set<X> get_x_set () {
185
return new Gee.ReadOnlySet<X> (x_to_y.get_keys ());
189
* Returns the sub-set of {@link Y} of elements used in this BiMap.
191
* @return a {@link Set} of elements of {@link Y}
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);
197
foreach (X x in x_to_y.get_keys ()) {
198
var y = x_to_y.get (x);
202
return new Gee.ReadOnlySet<Y> (y_to_x.get_keys ());