1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
16 #ifndef SQUIRRELJME_COMPARATOR_H
17 #define SQUIRRELJME_COMPARATOR_H
23 #ifndef SJME_CXX_IS_EXTERNED
24 #define SJME_CXX_IS_EXTERNED
25 #define SJME_CXX_SQUIRRELJME_COMPARATOR_H
27 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
28 #endif /* #ifdef __cplusplus */
30 /*--------------------------------------------------------------------------*/
33 * A generic comparator.
35 * @param a The first item.
36 * @param b The second item.
37 * @param elementSize The size of each element.
38 * @return The resultant comparison, will be zero, negative, or positive.
41 typedef sjme_jint (*sjme_comparator
)(sjme_cpointer a
, sjme_cpointer b
,
45 * Determines the name of a given comparator.
47 * @param type The type to use.
48 * @param numPointerStars The number of pointer stars used.
51 #define SJME_COMPARATOR(type, numPointerStars) \
52 SJME_TOKEN_PASTE_PP(sjme_comparator_, SJME_TOKEN_PASTE_PP(type, \
53 SJME_TOKEN_SINGLE(SJME_TOKEN_STARS_C##numPointerStars)))
56 * Determines the name of a given comparator.
58 * @param type The type to use.
59 * @param numPointerStars The number of pointer stars used.
62 #define SJME_COMPARATOR_INSENSITIVE(type, numPointerStars) \
63 SJME_TOKEN_PASTE_PP(sjme_comparator_insensitive_, \
64 SJME_TOKEN_PASTE_PP(type, \
65 SJME_TOKEN_SINGLE(SJME_TOKEN_STARS_C##numPointerStars)))
68 * Defines a generic comparator, using a simple subtraction.
70 * @param type The type to compare.
71 * @param numPointerStars The number of pointer stars.
74 #define SJME_COMPARATOR_GENERIC(type, numPointerStars) \
75 static sjme_inline sjme_attrArtificial sjme_jint \
76 SJME_COMPARATOR(type, numPointerStars)( \
77 sjme_cpointer a, sjme_cpointer b, int elementSize) \
79 return (sjme_jint)(*((const type*)b) - *((const type*)a)); \
82 /** Generic @c sjme_jbyte comparator. */
83 SJME_COMPARATOR_GENERIC(sjme_jbyte
, 0)
85 /** Generic @c sjme_jubyte comparator. */
86 SJME_COMPARATOR_GENERIC(sjme_jubyte
, 0)
88 /** Generic @c sjme_jshort comparator. */
89 SJME_COMPARATOR_GENERIC(sjme_jshort
, 0)
91 /** Generic @c sjme_jchar comparator. */
92 SJME_COMPARATOR_GENERIC(sjme_jchar
, 0)
94 /** Generic @c sjme_jint comparator. */
95 SJME_COMPARATOR_GENERIC(sjme_jint
, 0)
97 /** Generic @c sjme_juint comparator. */
98 SJME_COMPARATOR_GENERIC(sjme_juint
, 0)
100 /** Generic @c sjme_cchar comparator. */
101 SJME_COMPARATOR_GENERIC(sjme_cchar
, 0)
104 * Compares two @c sjme_lpcstr .
106 * @param a The first item.
107 * @param b The second item.
108 * @param elementSize The size of each element.
109 * @return The resultant comparison, will be zero, negative, or positive.
112 sjme_jint
SJME_COMPARATOR(sjme_lpcstr
, 0)(sjme_cpointer a
, sjme_cpointer b
,
116 * Compares two @c sjme_lpcstr without regards to case.
118 * @param a The first item.
119 * @param b The second item.
120 * @param elementSize The size of each element.
121 * @return The resultant comparison, will be zero, negative, or positive.
124 sjme_jint
SJME_COMPARATOR_INSENSITIVE(sjme_lpcstr
, 0)(
125 sjme_cpointer a
, sjme_cpointer b
, int elementSize
);
127 /*--------------------------------------------------------------------------*/
131 #ifdef SJME_CXX_SQUIRRELJME_COMPARATOR_H
133 #undef SJME_CXX_SQUIRRELJME_COMPARATOR_H
134 #undef SJME_CXX_IS_EXTERNED
135 #endif /* #ifdef SJME_CXX_SQUIRRELJME_COMPARATOR_H */
136 #endif /* #ifdef __cplusplus */
138 #endif /* SQUIRRELJME_COMPARATOR_H */