-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchvec.c
More file actions
74 lines (69 loc) · 1.92 KB
/
chvec.c
File metadata and controls
74 lines (69 loc) · 1.92 KB
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
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
// chvec.c ... functions on ChoiceVectors
// part of Multi-attribute Linear-hashed Files
// A ChVec is an array of MAXCHVEC ChVecItems
// Each ChVecItem is a pair (attr#,bit#)
// See chvec.c for details on functions
// Last modified by John Shepherd, July 2019
#include "defs.h"
#include "reln.h"
#include "chvec.h"
// convert a a,b:a,b:a,b:...:a,b" representation
// of a choice vector into a ChVec
// if string doesn't specify all 32 bits, then
// cycle through attributes until reach 32 bits
Status parseChVec(Reln r, char *str, ChVec cv)
{
Count i = 0, nattr = nattrs(r);
char *c = str, *c0 = str;
while (*c != '\0') {
while (*c != ':' && *c != '\0') c++;
Count a, b, n;
if (*c == '\0') {
n = sscanf(c0, "%d,%d", &a, &b);
// is the (attr,bit) pair valid?
// neither a nor b can be < 0 because they're unsigned
if (n != 2 || a >= nattr || b >= 32) {
printf("Invalid choice vector element: (att:%d,bit:%d)\n",a,b);
return ~OK;
}
}
else {
*c = '\0';
n = sscanf(c0, "%d,%d", &a, &b);
if (n != 2 || a >= nattr || b >= 32) {
printf("Invalid choice vector element: (att:%d,bit:%d)\n",a,b);
return ~OK;
}
*c = ':'; c++; c0 = c;
}
cv[i].att = a; cv[i].bit = b;
printf("cv[%d] is (%d,%d)\n", i, cv[i].att, cv[i].bit);
i++;
}
// get enough bits for a 32-bit choice vector
// take new bits from top end of each hash,
// so as to hopefully not conflict
Count x; Count next[MAXCHVEC];
for (x = 0; x < MAXCHVEC; x++) next[x] = 31;
x = 0;
while (i < MAXCHVEC) {
cv[i].att = x; cv[i].bit = next[x];
printf("cv[%d] is (%d,%d)\n", i, cv[i].att, cv[i].bit);
next[x]--;
i++; x = (x+1) % nattr;
}
return OK;
}
// print a choice vector (for debugging)
void printChVec(ChVec cv)
{
int i;
for (i = 0; i < MAXCHVEC; i++) {
printf("%d,%d",cv[i].att, cv[i].bit);
if (i < MAXCHVEC-1) putchar(':');
}
printf("\n");
}