1 |
// -*- Mode: C++ -*- |
2 |
|
3 |
// GiSTentry.h |
4 |
// |
5 |
// Copyright (c) 1996, Regents of the University of California |
6 |
|
7 |
|
8 |
#ifndef GISTENTRY_H |
9 |
#define GISTENTRY_H |
10 |
|
11 |
#include <string.h> // for memcpy |
12 |
#include "GiSTdefs.h" |
13 |
#include "GiSTpredicate.h" |
14 |
|
15 |
class GiSTnode; |
16 |
|
17 |
// GiSTcompressedEntry is the compressed format of a key-pointer pair. |
18 |
class GiSTcompressedEntry { |
19 |
public: |
20 |
char *key; |
21 |
int keyLen; |
22 |
GiSTpage ptr; |
23 |
|
24 |
GiSTcompressedEntry(): key(NULL), keyLen(0), ptr(0) {} |
25 |
const GiSTcompressedEntry operator = (const GiSTcompressedEntry c) { |
26 |
key=c.key; |
27 |
keyLen=c.keyLen; |
28 |
ptr=c.ptr; |
29 |
return(*this); |
30 |
} |
31 |
}; |
32 |
|
33 |
// GiSTpenalty is the token returned by the Penalty method. We don't |
34 |
// just use doubles here because it can be useful to use more |
35 |
// complex Penalty comparisons (e.g. to support multivalued Penalties that |
36 |
// break ties in a single value). |
37 |
class GiSTpenalty { |
38 |
public: |
39 |
GiSTpenalty(): amount(0) {} |
40 |
GiSTpenalty(const double d): amount(d) {} |
41 |
virtual double operator = (const double d) { |
42 |
amount=d; |
43 |
return amount; |
44 |
} |
45 |
virtual int operator < (const GiSTpenalty &p) const { |
46 |
if(amount<p.amount) return(1); |
47 |
else return(0); |
48 |
}
|
49 |
|
50 |
virtual ~GiSTpenalty() {} |
51 |
|
52 |
protected: |
53 |
double amount; |
54 |
}; |
55 |
|
56 |
// GiSTentry: a key/pointer pair inside a node. |
57 |
// The key is a pointer to a GiSTobject, which can be used to |
58 |
// point to just about anything you like. |
59 |
class GiSTentry : public GiSTobject { |
60 |
public: |
61 |
GiSTentry(): node(NULL), level(0) {} |
62 |
GiSTentry(GiSTpage ptr): node(NULL), ptr(ptr) {} |
63 |
GiSTentry(const GiSTentry& e): node(e.node), ptr(e.ptr), |
64 |
position(e.position), level(e.level) {} |
65 |
|
66 |
virtual ~GiSTentry() { if(key) delete key; } |
67 |
|
68 |
// The following methods must be overridden by descendant classes |
69 |
virtual GiSTpenalty *Penalty(const GiSTentry &key) const=0; |
70 |
virtual int CompressedLength() const=0; // length of compressed key |
71 |
#ifdef PRINTING_OBJECTS |
72 |
virtual void Print(ostream& os) const=0; |
73 |
#endif |
74 |
virtual GiSTobject *Copy() const=0; |
75 |
// Default compression is just to copy the key (i.e. no compression). |
76 |
// Since the copy method is virtual, the appropriate Copy method is called. |
77 |
// Besides compressing the key, Compress also sets the pointer. |
78 |
virtual GiSTcompressedEntry Compress() const { |
79 |
GiSTcompressedEntry compressedEntry; |
80 |
|
81 |
compressedEntry.key=(char *)key->Copy(); |
82 |
compressedEntry.keyLen=CompressedLength(); |
83 |
compressedEntry.ptr=ptr; |
84 |
return compressedEntry; |
85 |
} |
86 |
// Besides decompressing the key, Decompress also sets the pointer. |
87 |
virtual void Decompress(const GiSTcompressedEntry entry) { |
88 |
key=(GiSTobject *)new char[entry.keyLen]; |
89 |
memcpy(key, entry.key, entry.keyLen); |
90 |
ptr=entry.ptr; |
91 |
} |
92 |
|
93 |
// If you have ordered keys, this can be overridden a la qsort's compare |
94 |
virtual int Compare(const GiSTentry& entry) const { return 0; } |
95 |
|
96 |
// access to the protected info |
97 |
GiSTpage Ptr() const { return ptr; } |
98 |
void SetPtr(GiSTpage p) { ptr=p; } |
99 |
GiSTobject *Key() const { return key; } |
100 |
|
101 |
GiSTobjid IsA() const { return GISTENTRY_CLASS; } |
102 |
int IsLeaf() const { return level==0; } |
103 |
int Level() const { return level; } |
104 |
void SetLevel(int l) { level=l; } |
105 |
|
106 |
void SetPosition(int pos) { position=pos; } |
107 |
int Position() const { return position; } |
108 |
|
109 |
GiSTnode *Node() const { return node; } |
110 |
void SetNode(GiSTnode *n) { node=n; } |
111 |
|
112 |
protected: |
113 |
GiSTnode *node; |
114 |
GiSTpage ptr; |
115 |
int position; |
116 |
int level; |
117 |
GiSTobject *key; |
118 |
}; |
119 |
|
120 |
#endif |