Fix build with vala master
[platform/upstream/libgee.git] / gee / functions.vala
1 /* functions.vala
2  *
3  * Copyright (C) 2009  Didier Villevalois, Maciej Piechotka
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  *      Didier 'Ptitjes' Villevalois <ptitjes@free.fr>
21  *      Maciej Piechotka <uzytkownik2@gmail.com>
22  */
23
24 using GLib;
25
26 namespace Gee {
27
28         /**
29          * Helpers for equal, hash and compare functions.
30          *
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.
34          *
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}.
38          *
39          */
40         namespace Functions {
41
42                 /**
43                  * Get a equality testing function for a given type.
44                  *
45                  * @param t the type which to get an equality testing function for.
46                  *
47                  * @return the equality testing function corresponding to the given type.
48                  */
49                 public static EqualFunc get_equal_func_for (Type t) {
50                         if (t == typeof (string)) {
51                                 return str_equal;
52                         } else {
53                                 return direct_equal;
54                         }
55                 }
56
57                 /**
58                  * Get a hash function for a given type.
59                  *
60                  * @param t the type which to get the hash function for.
61                  *
62                  * @return the hash function corresponding to the given type.
63                  */
64                 public static HashFunc get_hash_func_for (Type t) {
65                         if (t == typeof (string)) {
66                                 return str_hash;
67                         } else {
68                                 return direct_hash;
69                         }
70                 }
71
72                 /**
73                  * Get a comparator function for a given type.
74                  *
75                  * @param t the type which to get a comparator function for.
76                  *
77                  * @return the comparator function corresponding to the given type.
78                  */
79                 public static CompareFunc get_compare_func_for (Type t) {
80                         if (t == typeof (string)) {
81                                 return (CompareFunc) strcmp;
82                         } else if (t.is_a (typeof (Comparable))) {
83                                 return (CompareFunc) Comparable.compare_to;
84                         } else {
85                                 return (CompareFunc) direct_compare;
86                         }
87                 }
88         }
89
90         /**
91          * Compares two arbitrary elements together.
92          *
93          * The comparison is done on pointers and not on values behind.
94          *
95          * @param _val1 the first value to compare.
96          * @param _val2 the second value to compare.
97          *
98          * @return a negative value if _val1 is lesser than _val2, a positive value
99          *         if _val1 is greater then _val2 and zero if both are equal.
100          */
101         public static int direct_compare (void* _val1, void* _val2) {
102                 long val1 = (long)_val1, val2 = (long)_val2;
103                 if (val1 > val2) {
104                         return 1;
105                 } else if (val1 == val2) {
106                         return 0;
107                 } else {
108                         return -1;
109                 }
110         }
111 }