Dist m4ri 0.16.1
Computing distance of a classical or quantum CSS code
Loading...
Searching...
No Matches
util_hash.h
Go to the documentation of this file.
1#ifndef UTIL_HASH_H
2#define UTIL_HASH_H
15#ifdef __cplusplus
16extern "C"{
17#endif
18#include "util_io.h"
19#include "util_m4ri.h"
20
21
23
25 typedef struct TWO_VEC_T {
27 int *err;
28 // int cnt; /** how many times this vector was encountered */
29 int w_e;
30 int w_s;
31 // int w_tot; /** `= w_e+w_s` */
32 // size_t len; /** `weight*sizeof(int)` (is this really needed?) */
33 int syn[0];
35
36 struct CW_VEC_T {
38 int weight;
39 int cnt;
40 int cnt_u;
41 int cnt_w;
42 int arr[0];
43 };
44
45 typedef struct ONE_VEC_T{
46 int wei;
47 // int max; /** allocated */
48 int vec[0];
50
51
56 void one_vec_print(const one_vec_t * const pvec);
57
67 static inline two_vec_t * two_vec_init(const one_vec_t * const syn, const one_vec_t * const err){
68 two_vec_t * ans = malloc(sizeof(two_vec_t)+sizeof(int)*(syn->wei + err->wei));
69 if(!ans)
70 ERROR("memory allocation fail");
71 for(int i=0; i < syn->wei; i++)
72 ans->syn[i] = syn->vec[i];
73 ans->w_e = err->wei;
74 ans->w_s = syn->wei;
75 ans->err = &( ans->syn[syn->wei] );
76 for(int j=0 ; j < err->wei; j++)
77 ans->err[j] = err->vec[j];
78
79 return ans;
80 }
81
86 static inline void two_vec_print(const two_vec_t * const it){
87 if(!it){
88 printf("two_vec_print(): null structure!\n");
89 return;
90 }
91 printf("# two_vec: w_e=%d e=[",it->w_e);
92 for(int i=0; i < it->w_e; i++)
93 printf(" %d",it->err[i]);
94 printf(" ]%s w_s=%d s=[",it->w_s>20?"\n#":" ",it->w_s);
95 for(int i=0; i < it->w_s; i++)
96 printf(" %d",it->syn[i]);
97 printf(" ]\n");
98 }
99
109 static inline int by_syndrome(void *a, void *b){
110 const two_vec_t * const pa = (two_vec_t *) a;
111 const two_vec_t * const pb = (two_vec_t *) b;
112 if (pa->w_s < pb->w_s)
113 return -1;
114 else if (pa->w_s > pb->w_s)
115 return +1;
116 else{
117 for(int i=0; i < pa->w_s; i++){
118 if (pa->syn[i] < pb->syn[i])
119 return -1;
120 else if (pa->syn[i] > pb->syn[i])
121 return +1;
122 }
123 }
124 return 0;
125}
126
136 static inline int by_error(void *a, void *b){
137 const two_vec_t * const pa = (two_vec_t *) a;
138 const two_vec_t * const pb = (two_vec_t *) b;
139 if (pa->w_e < pb->w_e)
140 return -1;
141 else if (pa->w_e > pb->w_e)
142 return +1;
143 else{
144 for(int i=0; i < pa->w_e; i++){
145 if (pa->err[i] < pb->err[i])
146 return -1;
147 else if (pa->err[i] > pb->err[i])
148 return +1;
149 }
150 }
151 return 0;
152 }
153
166 static inline two_vec_t * hash_add_maybe(one_vec_t * syn, const one_vec_t * const err,
167 two_vec_t * errors, int p_swei[],
168 __attribute__ ((unused)) const int debug){
169 two_vec_t *pvec, *entry;
170 const size_t keylen = syn->wei * sizeof(int);
171 if(p_swei[err->wei] > syn->wei){
172 //#ifndef NDEBUG
173 if(debug & 4096){ /* DBG_LEGACY in util_io.h */
174 printf("# swei[%d]=%d -> %d change\n# err: ",
175 err->wei,p_swei[err->wei],syn->wei);
176 one_vec_print(err);
177 printf("# syn: ");
178 one_vec_print(syn);
179 }
180 //#endif
181 p_swei[err->wei]=syn->wei;
182 }
183#ifndef NDEBUG
184 else{
185 if(debug & 4096){ /* DBG_LEGACY in util_io.h */
186 printf("p_swei[%d]=%d swei=%d not small enough\n",err->wei,p_swei[err->wei],syn->wei);
187 one_vec_print(err);
188 one_vec_print(syn);
189 }
190 }
191#endif
192
193 HASH_FIND(hh, errors, syn->vec, keylen, pvec);
194 if(!pvec){
195 entry = two_vec_init(syn, err);
196 HASH_ADD(hh, errors, syn, keylen, entry);
197 }
198 else{
200#ifndef NDEBUG
201 if(debug & 4096){ /* DBG_LEGACY in util_io.h */
202 printf("err: ");
203 one_vec_print(err);
204 printf("syn: ");
205 one_vec_print(syn);
206 printf("already in the hash:\n");
207 two_vec_print(pvec);
208 }
209#endif
210 }
211 return errors;
212 }
213
214
215#ifdef __cplusplus
216}
217#endif
218
219#endif /* UTIL_HASH_H */
220
two_vec_t * errors
Definition dist_cc.c:39
int arr[0]
Definition util_hash.h:42
int cnt_u
Definition util_hash.h:40
int cnt
Definition util_hash.h:39
UT_hash_handle hh
Definition util_hash.h:37
int cnt_w
Definition util_hash.h:41
int weight
Definition util_hash.h:38
int vec[0]
Definition util_hash.h:48
structure to hold two sparse vectors (syndrome,error) in a hash
Definition util_hash.h:25
UT_hash_handle hh
Definition util_hash.h:26
int syn[0]
Definition util_hash.h:33
int * err
Definition util_hash.h:27
#define HASH_ADD(hh, head, fieldname, keylen_in, add)
Definition uthash.h:427
#define HASH_FIND(hh, head, keyptr, keylen, out)
Definition uthash.h:165
struct TWO_VEC_T two_vec_t
structure to hold two sparse vectors (syndrome,error) in a hash
void one_vec_print(const one_vec_t *const pvec)
Print a one_vec_t structure (indices and weight) to stdout.
Definition dist_cc.c:32
struct ONE_VEC_T one_vec_t
#define ERROR(fmt,...)
Definition util_m4ri.h:13