Introduce the Comparable interface and provide a CompareFunc for it
[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          * Helper class for equal, hash and compare functions.
30          */
31         namespace Functions {
32
33                 /**
34                  * Get a equality testing function for a given type.
35                  *
36                  * @param t the type which to get an equality testing function for.
37                  *
38                  * @return the equality testing function corresponding to the given type.
39                  */
40                 public static EqualFunc get_equal_func_for (Type t) {
41                         if (t == typeof (string)) {
42                                 return str_equal;
43                         } else {
44                                 return direct_equal;
45                         }
46                 }
47
48                 /**
49                  * Get a hash function for a given type.
50                  *
51                  * @param t the type which to get the hash function for.
52                  *
53                  * @return the hash function corresponding to the given type.
54                  */
55                 public static HashFunc get_hash_func_for (Type t) {
56                         if (t == typeof (string)) {
57                                 return str_hash;
58                         } else {
59                                 return direct_hash;
60                         }
61                 }
62
63                 /**
64                  * Get a comparator function for a given type.
65                  *
66                  * @param t the type which to get a comparator function for.
67                  *
68                  * @return the comparator function corresponding to the given type.
69                  */
70                 public static CompareFunc get_compare_func_for (Type t) {
71                         if (t == typeof (string)) {
72                                 return (CompareFunc) strcmp;
73                         } else if (t.is_a (typeof (Comparable))) {
74                                 return (CompareFunc) comparable_compare;
75                         } else {
76                                 return (CompareFunc) direct_compare;
77                         }
78                 }
79         }
80
81         internal static int comparable_compare (void* a, void* b) {
82                 return ((Comparable) a).compare_to ((Comparable) b);
83         }
84
85         /**
86          * Compares two arbitrary elements together.
87          *
88          * The comparison is done on pointers and not on values behind.
89          *
90          * @param _val1 the first value to compare.
91          * @param _val2 the second value to compare.
92          *
93          * @return a negative value if _val1 is lesser than _val2, a positive value
94          *         if _val1 is greater then _val2 and zero if both are equal.
95          */
96         public static int direct_compare (void* _val1, void* _val2) {
97                 long val1 = (long)_val1, val2 = (long)_val2;
98                 if (val1 > val2) {
99                         return 1;
100                 } else if (val1 == val2) {
101                         return 0;
102                 } else {
103                         return -1;
104                 }
105         }
106 }