1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
/*************************************************************************
*
* HTCd - Copyright (C) 1998-2006 Henrik Rydberg
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SORTABLEH
#define SORTABLEH
#include <hdb/filemap.h>
#include <ops/express.h>
#include <mt/mset.h>
////////////////////////////////////////////////////
//
// NOTE that these classes are not MT safe:
// need to be called from dbamp
//
struct record_t : public filemap::rec_t {
static const int maxtabs=filemap::maxtabs;
static const int recsize=(maxtabs+1)*sizeof(long);
byte4 pos[maxtabs+1];
char data[sizeof(byte4)];
int size() const { return pos[maxtabs].get()-pos[0].get(); }
int size(int i) const { return pos[i+1].get()-pos[i].get(); }
sref at(int i) const { return sref(data+pos[i].get(),size(i)); }
/////////////////////////////////////////////////
record_t(const record_t& rec) {
for(int i=0;i<=maxtabs;i++) pos[i]=rec.pos[i];
memcpy(data,rec.data,rec.caps.get());
}
record_t(const spile& list);
/////////////////////////////////////////////////
void get(spile& list) const {
for(int i=0;i<list.size();i++) list[i]=at(i);
}
/////////////////////////////////////////////////
int setreq(int col,const sref& s) const {
return recsize+size()+s.size()-size(col);
}
inline static int req(const spile& list) {
int n=0; for(int i=0;i<list.size();i++) n+=list[i].size();
return recsize+n;
}
/////////////////////////////////////////////////
void set(int col,const sref& s) {
resize(col,s.size());
memcpy(data+pos[col].get(),s.data(),s.size());
}
void set(const spile& list);
/////////////////////////////////////////////////
void insert(int col) {
for(int i=maxtabs;i>col;i--) pos[i]=pos[i-1];
}
void remove(int col) {
resize(col,0);
for(int i=col;i<maxtabs;i++) pos[i]=pos[i+1];
}
/////////////////////////////////////////////////
void resize(int col,int n);
};
////////////////////////////////////////////////////////////////////
struct int_stream : public mstream {
int val;
int_stream() : val(0) {}
int appc(char c);
int appi(int i);
int appf(double f);
int apps(const sref& s);
};
////////////////////////////////////////////////////////////////////
struct idx_t {
sref key; record_t* rec;
idx_t(const sref& t,record_t* p) : key(t),rec(p) {}
};
class indexmap : public mrvec<idx_t> {
public:
typedef mrvec<idx_t>::iterator iterator;
typedef mrvec<idx_t>::const_iterator const_iterator;
struct comp_t {
op_t::op2_t func;
comp_t() : func(0) {}
comp_t(const op_t* op) : func(op->f2) {}
bool operator()(const idx_t& a,const idx_t& b) const {
int_stream res;
op_env env;
func(res,env,a.key,b.key);
return res.val;
}
};
int get(const sref& t) const;
int add(const sref& t,record_t* rec);
void sort(const op_t* op);
private:
comp_t comp;
};
#endif
|