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