Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore / ecore_value.c
1 /* ecore_value.c
2
3 Copyright (C) 2001 Christopher Rosendahl    <smugg@fatelabs.com>
4                    Nathan Ingersoll         <ningerso@d.umn.edu>
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to
8 deal in the Software without restriction, including without limitation the
9 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 sell copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies of the Software and its documentation and acknowledgment shall be
15 given in the documentation and software packages that this Software was
16 used.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
22 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25 */
26
27 #include "Ecore.h"
28 #include "Ecore_Data.h"
29 #include "ecore_private.h"
30
31 EAPI const unsigned int ecore_prime_table[] =
32 {
33    17, 31, 61, 127, 257, 509, 1021,
34      2053, 4093, 8191, 16381, 32771, 65537, 131071, 262147, 524287, 1048573,
35      2097143, 4194301, 8388617, 16777213
36 };
37
38 inline void 
39 ecore_print_warning(const char *function, const char *sparam)
40 {
41    fprintf(stderr, "***** Developer Warning ***** :\n"
42            "\tThis program is calling:\n\n"
43            "\t%s();\n\n"
44            "\tWith the parameter:\n\n"
45            "\t%s\n\n"
46            "\tbeing NULL. Please fix your program.\n", function, sparam);
47    fflush(stderr);
48    if (getenv("ECORE_ERROR_ABORT")) abort();
49 }
50
51 /**
52  * Just casts the key to an unsigned int
53  * @param  key The key to return compute a hash value
54  * @return The key cast to an unsigned int.
55  */
56 EAPI unsigned int 
57 ecore_direct_hash(const void *key)
58 {
59 #ifdef __LP64__
60    unsigned long int val = (unsigned long int)key;
61
62    return (unsigned int) ((val >> 32) ^ val);
63 #else
64    return (unsigned int) key;
65 #endif
66 }
67
68 /**
69  * Compute the hash value of a string
70  * @param  key A pointer to the string to compute a hash value
71  * @return A computed hash value for @a key.
72  */
73 EAPI unsigned int 
74 ecore_str_hash(const void *key)
75 {
76    int i;
77    unsigned int mask;
78    unsigned int value = 0;
79    const char *k = key;
80
81    if (!k)
82      return 0;
83
84    mask = (sizeof(unsigned int) * 8) - 1;
85
86    for (i = 0; k[i] != '\0'; i++)
87      {
88         value ^= ((unsigned int) k[i] << ((i * 5) & mask));
89      }
90
91    return value;
92 }
93
94 /**
95  * Perform a direct comparison of two keys' values
96  * @param  key1 The first key to compare
97  * @param  key2 The second key to compare
98  * @return A strcmp style value to indicate the larger key
99  */
100 EAPI int 
101 ecore_direct_compare(const void *key1, const void *key2)
102 {
103    unsigned int k1, k2;
104
105    k1 = (unsigned int) key1;
106    k2 = (unsigned int) key2;
107
108    if (k1 > k2)
109      return 1;
110
111    if (k1 < k2)
112      return -1;
113
114    return 0;
115 }
116
117 /**
118  * Perform a string comparison of two keys values
119  * @param  key1 The first key to compare
120  * @param  key2 The second key to compare
121  * @return A strcmp style value to indicate the larger key
122  */
123 EAPI int 
124 ecore_str_compare(const void *key1, const void *key2)
125 {
126    const char *k1, *k2;
127
128    if (!key1 || !key2)
129      return ecore_direct_compare(key1, key2);
130    else if (key1 == key2)
131      return 0;
132
133    k1 = key1;
134    k2 = key2;
135
136    return strcmp(k1, k2);
137 }