-
Notifications
You must be signed in to change notification settings - Fork 1
/
subseq.cpp
310 lines (257 loc) · 8.31 KB
/
subseq.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
subseq.cpp, Lorenz Schiffmann 2021
SQLite extension for calculating interpolated curve of one dimensional scatterplot
of column values as well as corresponding standard deviations.
The MIT License (MIT)
Copyright (c) 2021 Lorenz Schiffmann
subseqission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to subseqit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this subseqission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <iostream>
#include <set>
#include <vector>
#include <cstdlib>
#include "RegistExt.h"
#include "utf8_unicode.hpp"
#include <assert.h>
#include <memory.h>
using std::string;
using std::vector;
using std::set;
using std::to_string;
#ifndef SQLITE_OMIT_VIRTUALTABLE
#ifdef __cplusplus
extern "C" {
#endif
// Finds and stores result in st for a given
// string s.
void generate_(set<vector<string>>& st, vector<string> vs)
{
int s_size = vs.size();
if (s_size == 0)
return;
// If current string is not already present.
if (st.find(vs) == st.end()) {
st.insert(vs);
// Traverse current string, one by one
// remove every character and recur.
for (int i = 0; i < s_size; i++) {
vector<string> t = vs;
t.erase(t.begin()+i);
generate_(st, t);
}
}
return;
}
/* subseq_cursor is a subclass of sqlite3_vtab_cursor which will
** serve as the underlying representation of a cursor that scans
** over rows of the result
*/
typedef struct subseq_cursor subseq_cursor;
struct subseq_cursor {
sqlite3_vtab_cursor base; /* Base class - must be first */
int isDesc; /* True to count down rather than up */
sqlite3_int64 iRowid; /* The rowid */
string subseq;
sqlite3_int64 nrows;
sqlite3_int64 len_utf8;
set<vector<string>> st;
set<vector<string>>::iterator it;
};
int subseqConnect(
sqlite3 *db,
void *pAux,
int argc, const char *const*argv,
sqlite3_vtab **ppVtab,
char **pzErr
)
{
sqlite3_vtab *pNew;
int rc;
/* The hidden columns serves as arguments to the subseq function as in:
SELECT * FROM subseq('tblname', 'xcolid', 'ycolid', nbins, minbin, maxbin);
They won't show up in the SQL tables.
*/
rc = sqlite3_declare_vtab(db,
// Order of columns MUST match the order of the above enum ColNum
"CREATE TABLE subseq(subseq TEXT, nrows INTEGER HIDDEN)");
if( rc==SQLITE_OK )
{
pNew = *ppVtab = (sqlite3_vtab *)sqlite3_malloc( sizeof(*pNew) );
if( pNew==0 ) return SQLITE_NOMEM;
memset(pNew, 0, sizeof(*pNew));
}
thisdb = db;
return rc;
}
/*
** This method is the destructor for subseq_cursor objects.
*/
int subseqDisconnect(sqlite3_vtab *pVtab){
sqlite3_free(pVtab);
return SQLITE_OK;
}
/*
** Constructor for a new subseq_cursor object.
*/
int subseqOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
subseq_cursor *pCur;
// allocate c++ object with new rather than sqlite3_malloc which doesn't call constructors
pCur = new subseq_cursor;
if (pCur == NULL) return SQLITE_NOMEM;
*ppCursor = &pCur->base;
return SQLITE_OK;
}
/*
** Destructor for a subseq_cursor.
*/
int subseqClose(sqlite3_vtab_cursor *cur){
delete cur;
return SQLITE_OK;
}
/*
** Advance a subseq_cursor to its next row of output.
*/
int subseqNext(sqlite3_vtab_cursor *cur){
subseq_cursor *pCur = (subseq_cursor*)cur;
pCur->iRowid++;
pCur->it++;
return SQLITE_OK;
}
/*
** Return values of columns for the row at which the subseq_cursor
** is currently pointing.
*/
int subseqColumn(
sqlite3_vtab_cursor *cur, /* The cursor */
sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
int i /* Which column to return */
){
subseq_cursor *pCur = (subseq_cursor*)cur;
if (i==0) { // only 1 column to return
pCur->subseq = vect2str(*pCur->it);
sqlite3_result_text(ctx, pCur->subseq.c_str(),pCur->subseq.size(),0);
//pCur->it++;
}
return SQLITE_OK;
}
/*
** Return the rowid for the current row. In this implementation, the
** rowid is the same as the output value.
*/
int subseqRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
subseq_cursor *pCur = (subseq_cursor*)cur;
*pRowid = pCur->iRowid;
return SQLITE_OK;
}
/*
** Return TRUE if the cursor has been moved off of the last
** row of output.
*/
int subseqEof(sqlite3_vtab_cursor *cur) {
subseq_cursor *pCur = (subseq_cursor*)cur;
//return pCur->iRowid >= pCur->nrows;
//return (pCur->it == pCur->st.end());
return pCur->iRowid >= pCur->nrows;
}
int subseqFilter(
sqlite3_vtab_cursor *pVtabCursor,
int idxNum, const char *idxStr,
int argc, sqlite3_value **argv
){
subseq_cursor *pCur = (subseq_cursor *)pVtabCursor;
const int max_len = 16;
int l = 0;
string s;
if (argc > 0) {
s = sqlite3_mprintf("%s", (const char*)sqlite3_value_text(argv[0]));
l = utf8_split(s).size();
}
else {
const char *zText = "Function subseq(INPUT1) requires exactly one non-empty argument.\n";
pCur->base.pVtab->zErrMsg = sqlite3_mprintf(zText);
return SQLITE_ERROR;
}
if(l>0 && l <= max_len)
{
//pCur->subseq = sqlite3_mprintf("%s", (const char*)sqlite3_value_text(argv[0]));
//string s = sqlite3_mprintf("%s", (const char*)sqlite3_value_text(argv[0]));
generate_(pCur->st, utf8_split(s));
pCur->it = pCur->st.begin();
pCur->subseq = vect2str(*pCur->it);
pCur->nrows = pCur->st.size();
pCur->iRowid = 0;
} else
{
const string errmsg = "Length of INPUT1 argument for function subseq(INPUT1) must be between 1 and " \
+ to_string(max_len) + "\n";
pCur->base.pVtab->zErrMsg = sqlite3_mprintf(errmsg.c_str());
return SQLITE_ERROR;
}
return SQLITE_OK;
}
// critical, do not modify unless you are familiar in detail !!!!
int subseqBestIndex(
sqlite3_vtab *tab,
sqlite3_index_info *pIdxInfo
){
int i;
sqlite3_index_info::sqlite3_index_constraint *p;
p = pIdxInfo->aConstraint;
for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
if( p->iColumn!=1 ) continue;
if( p->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
if( !p->usable ) continue;
pIdxInfo->aConstraintUsage[i].argvIndex = 1;
pIdxInfo->aConstraintUsage[i].omit = 1;
pIdxInfo->estimatedCost = (double)10;
pIdxInfo->estimatedRows = 10;
return SQLITE_OK;
}
pIdxInfo->estimatedCost = (double)1000000000;
pIdxInfo->estimatedRows = 1000000000;
return SQLITE_OK;
}
/*
** This following structure defines all the methods for the
** generate_subseq virtual table.
*/
sqlite3_module subseqModule = {
0, /* iVersion */
0, /* xCreate */
subseqConnect, /* xConnect */
subseqBestIndex, /* xBestIndex */
subseqDisconnect, /* xDisconnect */
0, /* xDestroy */
subseqOpen, /* xOpen - open a cursor */
subseqClose, /* xClose - close a cursor */
subseqFilter, /* xFilter - configure scan constraints */
subseqNext, /* xNext - advance a cursor */
subseqEof, /* xEof - check for end of scan */
subseqColumn, /* xColumn - read data */
subseqRowid, /* xRowid - read data */
0, /* xUpdate */
0, /* xBegin */
0, /* xSync */
0, /* xCommit */
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
};
#endif /* SQLITE_OMIT_VIRTUALTABLE */
#ifdef __cplusplus
}
#endif