1/* hashmultimap.vala
2 *
3 * Copyright (C) 2009  Ali Sabil
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 * Author:
20 * 	Ali Sabil <ali.sabil@gmail.com>
21 */
22
23/**
24 * Hash table implementation of the {@link MultiMap} interface.
25 */
26public class Gee.HashMultiMap<K,V> : AbstractMultiMap<K,V> {
27	public HashFunc key_hash_func {
28		get { return ((HashMap<K, Set<V>>) _storage_map).key_hash_func; }
29	}
30
31	public EqualFunc key_equal_func {
32		get { return ((HashMap<K, Set<V>>) _storage_map).key_equal_func; }
33	}
34
35	public HashFunc value_hash_func { private set; get; }
36
37	public EqualFunc value_equal_func { private set; get; }
38
39	/**
40	 * Constructs a new, empty hash multimap.
41	 *
42	 * If not provided, the functions parameters are requested to the
43	 * {@link Functions} function factory methods.
44	 *
45	 * @param key_hash_func an optional key hash function
46	 * @param key_equal_func an optional key equality testing function
47	 * @param value_hash_func an optional value hash function
48	 * @param value_equal_func an optional value equality testing function
49	 */
50	public HashMultiMap (HashFunc? key_hash_func = null, EqualFunc? key_equal_func = null,
51	                     HashFunc? value_hash_func = null, EqualFunc? value_equal_func = null) {
52		base (new HashMap<K, Set<V>> (key_hash_func, key_equal_func, direct_equal));
53		if (value_hash_func == null) {
54			value_hash_func = Functions.get_hash_func_for (typeof (V));
55		}
56		if (value_equal_func == null) {
57			value_equal_func = Functions.get_equal_func_for (typeof (V));
58		}
59		this.value_hash_func = value_hash_func;
60		this.value_equal_func = value_equal_func;
61	}
62
63	protected override Collection<V> create_value_storage () {
64		return new HashSet<V> (_value_hash_func, _value_equal_func);
65	}
66
67	protected override MultiSet<K> create_multi_key_set () {
68		return new HashMultiSet<K> (key_hash_func, key_equal_func);
69	}
70
71	protected override EqualFunc get_value_equal_func () {
72		return _value_equal_func;
73	}
74}
75