@@ -18,63 +18,85 @@ extern "C" {
18
18
19
19
#define FL_LOADED 0x01
20
20
21
- extern " C" int di_compare (const void *op1, const void *op2) {
22
- d_idx_t *ie1 = (d_idx_t *)op1,
23
- *ie2 = (d_idx_t *)op2;
24
-
25
- return strcasecmp (ie1->name , ie2->name );
26
- }
21
+ struct TDist : public dist_t {
22
+ TVector<int > TypeVector;
23
+ TVector<int > WeightSets;
24
+ TVector<int *> WeightSetIndex;
25
+ TVector<int > Maximums;
26
+ TVector<int > ValueSets;
27
+ TVector<int *> ValueSetIndex;
28
+ TString Strings;
29
+ TString Names;
30
+ };
27
31
28
- int load_dist (d_idx_t *di) {
29
- int res = 0 ;
30
- int32_t temp;
32
+ struct TDIdx : public d_idx_t {
33
+ explicit TDIdx (IInputStream& mi) {
34
+ memset (this , 0 , sizeof (d_idx_t ));
35
+ mi.Read (name, D_NAME_LEN);
36
+ name[D_NAME_LEN] = ' \0 ' ;
37
+ index = ReadInt (mi);
38
+ offset = ReadInt (mi);
39
+ str_space = ReadInt (mi);
40
+ length = ReadInt (mi);
41
+ w_width = ReadInt (mi);
42
+ v_width = ReadInt (mi);
43
+ name_space = ReadInt (mi);
44
+ dist = nullptr ;
45
+ }
31
46
32
- if (di->flags != FL_LOADED) {
47
+ void Load () {
48
+ dist = &Dist;
33
49
auto resource = NResource::Find (" tpcds.idx" );
34
50
TStringInput mi (resource);
35
- mi.Skip (di->offset );
36
- di->dist = (dist_t *)malloc (sizeof (struct DIST_T ));
37
- auto d = di->dist ;
38
- d->type_vector = (int *)malloc (sizeof (int32_t ) * di->v_width );
39
- for (int i = 0 ; i < di->v_width ; i++) {
40
- mi.Read (&temp, sizeof (int32_t ));
41
- d->type_vector [i] = ntohl (temp);
51
+ mi.Skip (offset);
52
+ Dist.TypeVector .resize (v_width);
53
+ Dist.type_vector = Dist.TypeVector .data ();
54
+ for (int i = 0 ; i < v_width; i++) {
55
+ Dist.TypeVector [i] = ReadInt (mi);
42
56
}
43
-
44
- d-> weight_sets = ( int **) malloc ( sizeof ( int *) * di-> w_width );
45
- d-> maximums = ( int *) malloc ( sizeof ( int32_t ) * di-> w_width );
46
- for ( int i = 0 ; i < di-> w_width ; i++) {
47
- *(d-> weight_sets + i) = ( int *) malloc (di-> length * sizeof ( int32_t ) );
48
- d-> maximums [i] = 0 ;
49
- for ( int j = 0 ; j < di-> length ; j++) {
50
- mi. Read (&temp, sizeof ( int32_t )) ;
51
- *(*(d-> weight_sets + i) + j) = ntohl (temp);
52
- d-> maximums [i] += d-> weight_sets [i][j] ;
53
- d-> weight_sets [i][j] = d-> maximums [i];
57
+ Dist. WeightSetIndex . resize (w_width);
58
+ Dist. WeightSets . resize (w_width * length );
59
+ Dist. weight_sets = Dist. WeightSetIndex . data ( );
60
+ Dist. Maximums . resize ( w_width);
61
+ Dist. maximums = Dist. Maximums . data ( );
62
+ for ( int i = 0 ; i < w_width; i++) {
63
+ Dist. Maximums [i] = 0 ;
64
+ Dist. WeightSetIndex [i] = Dist. WeightSets . data () + i * length ;
65
+ for ( int j = 0 ; j < length; j++) {
66
+ Dist. maximums [i] += ReadInt (mi) ;
67
+ Dist. weight_sets [i][j] = Dist. maximums [i];
54
68
}
55
69
}
56
-
57
- d-> value_sets = ( int **) malloc ( sizeof ( int *) * di-> v_width );
58
- MALLOC_CHECK (d-> value_sets );
59
- for ( int i = 0 ; i < di-> v_width ; i++) {
60
- *(d-> value_sets + i) = ( int *) malloc (di-> length * sizeof ( int32_t ));
61
- for ( int j = 0 ; j < di-> length ; j++) {
62
- mi. Read (&temp, sizeof ( int32_t ));
63
- *(*(d-> value_sets + i) + j) = ntohl (temp );
70
+
71
+ Dist. ValueSetIndex . resize ( v_width);
72
+ Dist. ValueSets . resize (v_width * length );
73
+ Dist. value_sets = Dist. ValueSetIndex . data ();
74
+ for ( int i = 0 ; i < v_width; i++) {
75
+ Dist. ValueSetIndex [i] = Dist. ValueSets . data () + i * length;
76
+ for ( int j = 0 ; j < length; j++) {
77
+ Dist. value_sets [i][j] = ReadInt (mi );
64
78
}
65
79
}
66
80
67
- if (di->name_space ) {
68
- d->names = (char *)malloc (di->name_space );
69
- mi.Read (d->names , di->name_space * sizeof (char ));
81
+ if (name_space) {
82
+ Dist.Names .resize (name_space);
83
+ Dist.names = Dist.Names .begin ();
84
+ mi.Read (Dist.names , name_space * sizeof (char ));
70
85
}
71
86
72
- d->strings = (char *)malloc (sizeof (char ) * di->str_space );
73
- mi.Read (d->strings , di->str_space * sizeof (char ));
74
- di->flags = FL_LOADED;
87
+ Dist.Strings .resize (str_space);
88
+ Dist.strings = Dist.Strings .begin ();
89
+ mi.Read (Dist.strings , str_space * sizeof (char ));
90
+ flags = FL_LOADED;
75
91
}
76
- return (res);
77
- }
92
+
93
+ static inline int ReadInt (IInputStream& mi) {
94
+ int temp;
95
+ mi.Read (&temp, sizeof (int32_t ));
96
+ return ntohl (temp);
97
+ };
98
+ TDist Dist;
99
+ };
78
100
79
101
80
102
class TDists {
@@ -85,44 +107,25 @@ class TDists {
85
107
memcpy (&temp, resource.data (), sizeof (int32_t ));
86
108
int entry_count = ntohl (temp);
87
109
TMemoryInput mi (resource.end () - entry_count * IDX_SIZE, entry_count * IDX_SIZE);
88
- Idxs.resize (entry_count);
89
- for (auto & current_idx: Idxs) {
90
- memset (¤t_idx, 0 , sizeof (d_idx_t ));
91
- mi.Read (current_idx.name , D_NAME_LEN);
92
- current_idx.name [D_NAME_LEN] = ' \0 ' ;
93
- mi.Read (&temp, sizeof (int32_t ));
94
- current_idx.index = ntohl (temp);
95
- mi.Read (&temp, sizeof (int32_t ));
96
- current_idx.offset = ntohl (temp);
97
- mi.Read (&temp, sizeof (int32_t ));
98
- current_idx.str_space = ntohl (temp);
99
- mi.Read (&temp, sizeof (int32_t ));
100
- current_idx.length = ntohl (temp);
101
- mi.Read (&temp, sizeof (int32_t ));
102
- current_idx.w_width = ntohl (temp);
103
- mi.Read (&temp, sizeof (int32_t ));
104
- current_idx.v_width = ntohl (temp);
105
- mi.Read (&temp, sizeof (int32_t ));
106
- current_idx.name_space = ntohl (temp);
107
- current_idx.dist = NULL ;
110
+ for (auto i = 0 ; i < entry_count; ++i) {
111
+ TDIdx current_idx (mi);
112
+ TString name (current_idx.name );
113
+ name.to_lower ();
114
+ Idxs.emplace (name, std::move (current_idx));
108
115
}
109
- qsort (Idxs.begin (), entry_count, sizeof (d_idx_t ), di_compare);
110
116
}
111
117
112
- d_idx_t * operator [](char * name) {
113
- d_idx_t key;
114
- strcpy (key.name , name);
115
- auto id = (d_idx_t *)bsearch (&key, Idxs.begin (), Idxs.size (), sizeof (d_idx_t ), di_compare);
116
- if (id != NULL ) {
117
- if (id->flags != FL_LOADED) {
118
- load_dist (id);
119
- }
118
+ d_idx_t * operator [](TString name) {
119
+ name.to_lower ();
120
+ auto id = MapFindPtr (Idxs, name);
121
+ if (id != NULL && id->flags != FL_LOADED) {
122
+ id->Load ();
120
123
}
121
124
return id;
122
125
}
123
126
124
127
private:
125
- TVector< d_idx_t > Idxs;
128
+ TMap<TString, TDIdx > Idxs;
126
129
};
127
130
128
131
extern " C" d_idx_t * find_dist (char *name) {
0 commit comments