Dist m4ri 0.0.1.alpha
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
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 arr[0];
41 };
42
43 typedef struct ONE_VEC_T{
44 int wei;
45 // int max; /** allocated */
46 int vec[0];
48
49
54 void one_vec_print(const one_vec_t * const pvec);
55
65 static inline two_vec_t * two_vec_init(const one_vec_t * const syn, const one_vec_t * const err){
66 two_vec_t * ans = malloc(sizeof(two_vec_t)+sizeof(int)*(syn->wei + err->wei));
67 if(!ans)
68 ERROR("memory allocation fail");
69 for(int i=0; i < syn->wei; i++)
70 ans->syn[i] = syn->vec[i];
71 ans->w_e = err->wei;
72 ans->w_s = syn->wei;
73 ans->err = &( ans->syn[syn->wei] );
74 for(int j=0 ; j < err->wei; j++)
75 ans->err[j] = err->vec[j];
76
77 return ans;
78 }
79
84 static inline void two_vec_print(const two_vec_t * const it){
85 if(!it){
86 printf("two_vec_print(): null structure!\n");
87 return;
88 }
89 printf("# two_vec: w_e=%d e=[",it->w_e);
90 for(int i=0; i < it->w_e; i++)
91 printf(" %d",it->err[i]);
92 printf(" ]%s w_s=%d s=[",it->w_s>20?"\n#":" ",it->w_s);
93 for(int i=0; i < it->w_s; i++)
94 printf(" %d",it->syn[i]);
95 printf(" ]\n");
96 }
97
107 static inline int by_syndrome(void *a, void *b){
108 const two_vec_t * const pa = (two_vec_t *) a;
109 const two_vec_t * const pb = (two_vec_t *) b;
110 if (pa->w_s < pb->w_s)
111 return -1;
112 else if (pa->w_s > pb->w_s)
113 return +1;
114 else{
115 for(int i=0; i < pa->w_s; i++){
116 if (pa->syn[i] < pb->syn[i])
117 return -1;
118 else if (pa->syn[i] > pb->syn[i])
119 return +1;
120 }
121 }
122 return 0;
123}
124
134 static inline int by_error(void *a, void *b){
135 const two_vec_t * const pa = (two_vec_t *) a;
136 const two_vec_t * const pb = (two_vec_t *) b;
137 if (pa->w_e < pb->w_e)
138 return -1;
139 else if (pa->w_e > pb->w_e)
140 return +1;
141 else{
142 for(int i=0; i < pa->w_e; i++){
143 if (pa->err[i] < pb->err[i])
144 return -1;
145 else if (pa->err[i] > pb->err[i])
146 return +1;
147 }
148 }
149 return 0;
150 }
151
164 static inline two_vec_t * hash_add_maybe(one_vec_t * syn, const one_vec_t * const err,
165 two_vec_t * errors, int p_swei[],
166 __attribute__ ((unused)) const int debug){
167 two_vec_t *pvec, *entry;
168 const size_t keylen = syn->wei * sizeof(int);
169 if(p_swei[err->wei] > syn->wei){
170 //#ifndef NDEBUG
171 if(debug&64){
172 printf("# swei[%d]=%d -> %d change\n# err: ",
173 err->wei,p_swei[err->wei],syn->wei);
174 one_vec_print(err);
175 printf("# syn: ");
176 one_vec_print(syn);
177 }
178 //#endif
179 p_swei[err->wei]=syn->wei;
180 }
181#ifndef NDEBUG
182 else{
183 if(debug&64){
184 printf("p_swei[%d]=%d swei=%d not small enough\n",err->wei,p_swei[err->wei],syn->wei);
185 one_vec_print(err);
186 one_vec_print(syn);
187 }
188 }
189#endif
190
191 HASH_FIND(hh, errors, syn->vec, keylen, pvec);
192 if(!pvec){
193 entry = two_vec_init(syn, err);
194 HASH_ADD(hh, errors, syn, keylen, entry);
195 }
196 else{
198#ifndef NDEBUG
199 if(debug&128){
200 printf("err: ");
201 one_vec_print(err);
202 printf("syn: ");
203 one_vec_print(syn);
204 printf("already in the hash:\n");
205 two_vec_print(pvec);
206 }
207#endif
208 }
209 return errors;
210 }
211
212
213#ifdef __cplusplus
214}
215#endif
216
217#endif /* UTIL_HASH_H */
218
two_vec_t * errors
Definition dist_cc.c:39
int arr[0]
Definition util_hash.h:40
int cnt
Definition util_hash.h:39
UT_hash_handle hh
Definition util_hash.h:37
int weight
Definition util_hash.h:38
int vec[0]
Definition util_hash.h:46
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