Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/librecsort/rs-verify.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1996-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* Glenn Fowler <[email protected]> *
19
* *
20
***********************************************************************/
21
/* Verify that records are sorted.
22
**
23
** Written by Kiem-Phong Vo (09/18/96).
24
*/
25
26
#include "rshdr.h"
27
28
typedef struct rsverify_s
29
{ Rsobj_t obj;
30
ulong o; /* obj's ordinal */
31
ulong n; /* number of objects */
32
} Rsverify_t;
33
34
#if __STD_C
35
static int verifyinsert(Rs_t* rs, reg Rsobj_t* obj)
36
#else
37
static int verifyinsert(rs, obj)
38
Rs_t* rs;
39
reg Rsobj_t* obj;
40
#endif
41
{
42
reg int cmp, n;
43
reg ulong oh, ph;
44
reg uchar* k;
45
reg Rsobj_t* p;
46
reg Rsverify_t* verify = (Rsverify_t*)rs->methdata;
47
48
p = &verify->obj; ph = p->order;
49
50
OBJHEAD(obj); oh = obj->order;
51
if(verify->n > 0) /* compare with current obj */
52
{ OBJCMP(obj,p,cmp);
53
54
if(cmp == 0 && (rs->type&RS_DATA) ) /* compare by obj->data */
55
{ k = obj->data; obj->data = obj->key; obj->key = k;
56
n = obj->datalen; obj->datalen = obj->keylen; obj->keylen = n;
57
OBJHEAD(obj);
58
k = p->data; p->data = p->key; p->key = k;
59
n = p->datalen; p->datalen = p->keylen; p->keylen = n;
60
OBJHEAD(p);
61
62
OBJCMP(obj,p,cmp);
63
64
k = obj->data; obj->data = obj->key; obj->key = k;
65
n = obj->datalen; obj->datalen = obj->keylen; obj->keylen = n;
66
k = p->data; p->data = p->key; p->key = k;
67
n = p->datalen; p->datalen = p->keylen; p->keylen = n;
68
obj->order = oh; p->order = ph;
69
}
70
if(rs->type&RS_REVERSE)
71
cmp = -cmp;
72
73
/* out of order */
74
if((cmp == 0 && (rs->type&RS_UNIQ)) || cmp < 0 )
75
{ if(!(rs->disc->events & RS_VERIFY))
76
return -1;
77
78
obj->equal = obj->left = obj->right = NIL(Rsobj_t*);
79
if(cmp == 0)
80
obj->equal = p;
81
else if(rs->type&RS_REVERSE)
82
obj->left = p;
83
else obj->right = p;
84
85
obj->order = verify->n;
86
p->order = verify->o;
87
88
n = RSNOTIFY(rs,RS_VERIFY,obj,0,rs->disc);
89
90
obj->order = oh; p->order = ph;
91
92
if(n < 0) /* abort now */
93
return -1;
94
else if(n == 0) /* pretend this didn't exist */
95
{ if(rs->disc->defkeyf && obj->key)
96
vmfree(rs->vm,obj->key);
97
goto done;
98
}
99
/* else if(n > 0); start comparing from this one */
100
}
101
}
102
103
if(p->left == p) /* p->data was our allocation */
104
{ if(p->data)
105
vmfree(rs->vm,p->data);
106
p->left = NIL(Rsobj_t*);
107
}
108
109
/* switch current object */
110
p->order = obj->order;
111
p->data = obj->data;
112
p->datalen = obj->datalen;
113
114
if(rs->disc->defkeyf && p->key)
115
vmfree(rs->vm,p->key);
116
p->key = obj->key;
117
p->keylen = obj->keylen;
118
119
verify->o = verify->n;
120
121
done: /* free obj */
122
obj->right = rs->free;
123
rs->free = obj;
124
125
verify->n += 1;
126
return 0;
127
}
128
129
#if __STD_C
130
static Rsobj_t* verifylist(Rs_t* rs)
131
#else
132
static Rsobj_t* verifylist(rs)
133
Rs_t* rs;
134
#endif
135
{
136
reg Rsverify_t* verify = (Rsverify_t*)rs->methdata;
137
reg Rsobj_t* p = &verify->obj;
138
reg uchar* k;
139
140
if(verify->n > 0 && p->data)
141
{ /* save data space for last object */
142
if(!(k = (uchar*)vmalloc(rs->vm,p->datalen)) )
143
return NIL(Rsobj_t*);
144
memcpy(k,p->data,p->datalen);
145
p->data = k;
146
147
if(!rs->disc->defkeyf)
148
p->key = p->data + rs->disc->key;
149
150
p->left = p; /* so we know that p->data is our space */
151
}
152
153
return NIL(Rsobj_t*);
154
}
155
156
/* public method */
157
static Rsmethod_t _Rsverify =
158
{ verifyinsert,
159
verifylist,
160
sizeof(Rsverify_t),
161
RS_MTVERIFY,
162
"verify",
163
"Verify that the input is sorted."
164
};
165
166
__DEFINE__(Rsmethod_t*, Rsverify, &_Rsverify);
167
168
#ifdef NoF
169
NoF(rsverify)
170
#endif
171
172