3 * Copyright (C) 2009 Didier Villevalois, Maciej Piechotka
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
20 * Didier 'Ptitjes' Villevalois <ptitjes@free.fr>
21 * Maciej Piechotka <uzytkownik2@gmail.com>
29 * Helpers for equal, hash and compare functions.
31 * With those functions, you can retrieve the equal, hash and compare
32 * functions that best match your element, key or value types. Supported
33 * types are (non-boxed) primitive, string and ``Object`` types.
35 * A special care is taken for classes inheriting from the
36 * {@link Comparable} interface. For such types, an appropriate compare
37 * function is returned that calls {@link Comparable.compare_to}.
43 * Get a equality testing function for a given type.
45 * @param t the type which to get an equality testing function for.
47 * @return the equality testing function corresponding to the given type.
49 public static EqualDataFunc get_equal_func_for (Type t) {
50 if (t == typeof (string)) {
54 else if (a == null || b == null)
57 return str_equal ((string) a, (string) b);
59 } else if (t.is_a (typeof (Hashable))) {
63 else if (a == null || b == null)
66 return ((Hashable<Hashable>) a).equal_to ((Hashable) b);
68 } else if (t.is_a (typeof (Comparable))) {
72 else if (a == null || b == null)
75 return ((Comparable<Comparable>) a).compare_to ((Comparable) b) == 0;};
77 return (a, b) => {return direct_equal (a, b);};
82 * Get a hash function for a given type.
84 * @param t the type which to get the hash function for.
86 * @return the hash function corresponding to the given type.
88 public static HashDataFunc get_hash_func_for (Type t) {
89 if (t == typeof (string)) {
92 return (uint)0xdeadbeef;
94 return str_hash ((string) a);
96 } else if (t.is_a (typeof (Hashable))) {
99 return (uint)0xdeadbeef;
101 return ((Hashable) a).hash();
104 return (a) => {return direct_hash (a);};
109 * Get a comparator function for a given type.
111 * @param t the type which to get a comparator function for.
113 * @return the comparator function corresponding to the given type.
115 public static CompareDataFunc get_compare_func_for (Type t) {
116 if (t == typeof (string)) {
125 return strcmp((string) a, (string) b);
127 } else if (t.is_a (typeof (Comparable))) {
136 return ((Comparable<Comparable>) a).compare_to ((Comparable) b);
139 return (_val1, _val2) => {
140 long val1 = (long)_val1, val2 = (long)_val2;
143 } else if (val1 == val2) {