initial
[prop.git] / include / AD / generic / ordering.h
blob5019ce263d302ada101ac5f981881a881d78d02b
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef hashing_and_ordering_functions_h
26 #define hashing_and_ordering_functions_h
28 #include <string.h>
29 #include <AD/generic/generic.h>
31 ////////////////////////////////////////////////////////////////////////////
32 // Some default hashing functions
33 ////////////////////////////////////////////////////////////////////////////
34 inline unsigned int hash(char c) { return c; }
35 inline unsigned int hash(unsigned char c) { return c; }
36 inline unsigned int hash(int i) { return i; }
37 inline unsigned int hash(unsigned int i) { return i; }
38 inline unsigned int hash(short i) { return i; }
39 inline unsigned int hash(unsigned short i) { return i; }
40 inline unsigned int hash(long i) { return i; }
41 inline unsigned int hash(unsigned long i) { return i; }
42 inline unsigned int hash(float d) { return (unsigned int)d; }
43 inline unsigned int hash(double d) { return (unsigned int)d; }
44 extern unsigned int hash(const char *);
46 ////////////////////////////////////////////////////////////////////////////
47 // Some default equality functions
48 ////////////////////////////////////////////////////////////////////////////
49 inline Bool equal(char i, char j) { return i == j; }
50 inline Bool equal(unsigned char i, unsigned char j) { return i == j; }
51 inline Bool equal(short i, short j) { return i == j; }
52 inline Bool equal(unsigned short i, unsigned short j) { return i == j; }
53 inline Bool equal(int i, int j) { return i == j; }
54 inline Bool equal(unsigned int i, unsigned int j) { return i == j; }
55 inline Bool equal(long i, long j) { return i == j; }
56 inline Bool equal(unsigned long i, unsigned long j) { return i == j; }
57 inline Bool equal(float a, float b) { return a == b; }
58 inline Bool equal(double a, double b) { return a == b; }
59 inline Bool equal(const char * a, const char * b) { return strcmp(a,b) == 0; }
61 ////////////////////////////////////////////////////////////////////////////
62 // Some default ordering functions
63 ////////////////////////////////////////////////////////////////////////////
64 inline Bool less(long i, long j) { return i < j; }
65 inline Bool less(short i, short j) { return i < j; }
66 inline Bool less(int i, int j) { return i < j; }
67 inline Bool less(double a, double b) { return a < b; }
68 inline Bool less(const char * a, const char * b) { return strcmp(a,b) < 0; }
69 inline Bool lesseq(long i, long j) { return i <= j; }
70 inline Bool lesseq(double a, double b) { return a <= b; }
71 inline Bool lesseq(const char * a, const char * b) { return strcmp(a,b) <= 0; }
72 inline int compare(int i, int j) { return i - j; }
73 inline int compare(short i, short j) { return i - j; }
74 inline int compare(long i, long j) { return i - j; }
75 inline int compare(double a, double b)
76 { return a == b ? 0 : (a > b ? 1 : 0); }
77 inline int compare(float a, float b)
78 { return a == b ? 0 : (a > b ? 1 : 0); }
79 inline int compare(const char * a, const char * b) { return strcmp(a,b); }
81 ////////////////////////////////////////////////////////////////////////////
82 // Constants used by hash table implementations.
83 ////////////////////////////////////////////////////////////////////////////
84 #define Cell_unused 0
85 #define Cell_used 1
86 #define Cell_deleted 2
88 /////////////////////////////////////////////////////////////////////////////
89 // This class represents a linear ordering parameterized by the type.
90 /////////////////////////////////////////////////////////////////////////////
91 template <class T>
92 class Ordering {
93 public:
94 Ordering() {}
95 ~Ordering() {}
96 int compare(const T&, const T&) const; // user defined
97 Bool less(const T& a, const T& b) const { return compare(a,b) < 0; }
98 Bool greater(const T& a, const T& b) const { return compare(a,b) > 0; }
99 Bool lesseq(const T& a, const T& b) const { return compare(a,b) <= 0; }
100 Bool greatereq(const T& a, const T& b) const { return compare(a,b) >= 0; }
103 /////////////////////////////////////////////////////////////////////////////
104 // A macro to generate an ordering. Inlined to allow multiple definition.
105 /////////////////////////////////////////////////////////////////////////////
106 #define MakeOrdering(TYPE, comparison_expression) \
107 inline int Ordering<TYPE>::compare(const TYPE& a, const TYPE& b) const \
108 { return (comparison_expression); }
110 #endif