harmonize-python 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+
2
+
3
+ #ifndef HARMONIZE_SYNC
4
+ #define HARMONIZE_SYNC
5
+
6
+ #include "barrier.h"
7
+
8
+ #endif
9
+
@@ -0,0 +1,510 @@
1
+
2
+ #ifndef HARMONIZE_UTIL_CLI
3
+ #define HARMONIZE_UTIL_CLI
4
+
5
+ #include <cstring>
6
+
7
+ namespace util {
8
+
9
+ namespace cli {
10
+
11
+
12
+
13
+ struct GraphStyle {
14
+
15
+ int rows;
16
+ int cols;
17
+ const char** tiles;
18
+
19
+ GraphStyle (unsigned int r, unsigned int c, const char** t)
20
+ : rows(r)
21
+ , cols(c)
22
+ , tiles(t)
23
+ {}
24
+
25
+ };
26
+
27
+
28
+ struct GraphShape {
29
+
30
+ int width;
31
+ int height;
32
+
33
+ float x_min;
34
+ float x_max;
35
+ float y_min;
36
+ float y_max;
37
+
38
+ };
39
+
40
+
41
+
42
+ static const char* BlockFillTiles[] = {
43
+ " ","▁","▂","▃","▄","▅","▅","▆","▇","█"
44
+ };
45
+ static const char* Block2x2FillTiles[] = {
46
+ " ","▗","▐",
47
+ "▖","▄","▟",
48
+ "▌","▙","█"
49
+ };
50
+ static const char* Block2x3FillTiles[] = {
51
+ " ","🬞","🬦","▐",
52
+ "🬏","🬭","🬵","🬷",
53
+ "🬓","🬱","🬹","🬻",
54
+ "▌","🬲","🬺","█"
55
+ };
56
+ static const char* AsciiFillTiles[] = {
57
+ " ",".",".","i","|",
58
+ ".","_","_","j","J",
59
+ ".","_","o","d","d",
60
+ "i","L","b",":","4",
61
+ "|","L","b","%","#"
62
+ };
63
+ static const char* BrailleFillTiles[25] = {
64
+ "⠀","⢀","⢠","⢰","⢸",
65
+ "⡀","⣀","⣠","⣰","⣸",
66
+ "⡄","⣄","⣤","⣴","⣼",
67
+ "⡆","⣆","⣦","⣶","⣾",
68
+ "⡇","⣇","⣧","⣷","⣿"
69
+ };
70
+ static const GraphStyle BlockFill = GraphStyle( 9, 1, BlockFillTiles );
71
+ static const GraphStyle Block2x2Fill = GraphStyle( 2, 2, Block2x2FillTiles );
72
+ static const GraphStyle Block2x3Fill = GraphStyle( 3, 2, Block2x3FillTiles );
73
+ static const GraphStyle AsciiFill = GraphStyle( 4, 2, AsciiFillTiles );
74
+ static const GraphStyle BrailleFill = GraphStyle( 4, 2, BrailleFillTiles);
75
+
76
+
77
+ static unsigned int lead_char_count(float val) {
78
+ unsigned int result = 1;
79
+ if( val >= 10.0 ){
80
+ result = ceil(log10(val)+0.00000000001);
81
+ } else if ( val < 0.0 ){
82
+ if( val <= -10.0 ){
83
+ result = ceil(log10(abs(val))) + 1;
84
+ } else {
85
+ result = 2;
86
+ }
87
+ }
88
+ return result;
89
+ }
90
+
91
+
92
+
93
+ static void cli_graph(float* data, int size, GraphShape shape, GraphStyle style){
94
+
95
+ // Local function for finding the width of a value's sign and leading digits
96
+ // Find upper and lower bounds
97
+ /*
98
+ float max = data[0];
99
+ float min = data[0];
100
+ for( int i=0; i<size; i++){
101
+ max = (data[i] > max) ? data[i] : max;
102
+ min = (data[i] < min) ? data[i] : min;
103
+ }
104
+ */
105
+
106
+ // Determine how many columns will be alloted for leading digits and signs
107
+ unsigned int min_lead_span = lead_char_count(shape.y_min);
108
+ unsigned int max_lead_span = lead_char_count(shape.y_max);
109
+ unsigned int lead_span = (min_lead_span > max_lead_span) ? min_lead_span : max_lead_span;
110
+
111
+ // Iterate over the rows of the graph
112
+ printf("%7.5f_\n",shape.y_max);
113
+ for(int i=0; i<shape.height; i++){
114
+
115
+ // Label tic marks of vertical axis
116
+ float vert_span = shape.y_max-shape.y_min;
117
+ float base = (shape.height-i-1)*vert_span/shape.height+shape.y_min;
118
+ int whole_digits = lead_char_count(base);
119
+ for(int i=whole_digits; i<lead_span; i++){
120
+ printf(" ");
121
+ }
122
+ printf("%7.5f_",base);
123
+
124
+ // Iterate over the elements of each row
125
+ unsigned int x_iter = 0;
126
+ float last = data[0];
127
+ for(int j=0; j<shape.width; j++){
128
+ unsigned int index = 0;
129
+ // Iterate over each sub-column in each element
130
+ for( int k=0; k<style.cols; k++){
131
+ float val = 0.0;
132
+ float count = 0.0;
133
+ unsigned int limit = ((j*style.cols+k)*size)/(shape.width*style.cols);
134
+ // Iterate over each value corresponding to each sub-column
135
+ for(; x_iter < limit; x_iter++){
136
+ val += data[x_iter];
137
+ count += 1.0;
138
+ }
139
+ // Use last column value if no values correspond to current column
140
+ if( count != 0.0 ) {
141
+ val /= count;
142
+ last = val;
143
+ } else {
144
+ val = last;
145
+ }
146
+ float rel_val = (val-base) / vert_span * shape.height * style.rows;
147
+ unsigned int col_idx;
148
+ col_idx = ( rel_val <= 0.0 ) ? 0 : rel_val;
149
+ col_idx = ( rel_val >= style.rows ) ? style.rows : col_idx;
150
+ index *= (style.rows + 1);
151
+ index += col_idx;
152
+ }
153
+ printf("%s",style.tiles[index]);
154
+ }
155
+
156
+
157
+ printf("\n");
158
+ }
159
+
160
+ int rule_size = 8*shape.width/2;
161
+ char* rule_vals = new char[rule_size];
162
+ std::memset(rule_vals,'\0',rule_size);
163
+
164
+ printf(" ");
165
+ for(int j=1; j<lead_span; j++){
166
+ printf(" ");
167
+ }
168
+ for(int j=0; j<shape.width; j+=2){
169
+ float tic_val = shape.x_min + ((shape.x_max-shape.x_min)/shape.width)*j;
170
+ sprintf(&rule_vals[(8*j/2)],"%7.3f",tic_val);
171
+ printf("\\ ");
172
+ }
173
+ printf("\n");
174
+ for(int i=0; i<7; i++){
175
+ printf(" ");
176
+ for(int j=1; j<lead_span; j++){
177
+ printf(" ");
178
+ }
179
+ for(int j=0; j<shape.width; j+=2){
180
+ printf(" %c",rule_vals[(8*j/2)+i]);
181
+ }
182
+ printf("\n");
183
+ }
184
+
185
+ free(rule_vals);
186
+
187
+ }
188
+
189
+
190
+
191
+
192
+ static void cli_graph(float* data, int size, int width, int height, float low, float high){
193
+
194
+
195
+ #if GRAPH_MODE == 4
196
+ const int cols = 5;
197
+ const int rows = 5;
198
+ const char* lookup[25] = {
199
+ "⠀","⡀","⡄","⡆","⡇",
200
+ "⢀","⣀","⣄","⣆","⣇",
201
+ "⢠","⣠","⣤","⣦","⣧",
202
+ "⢰","⣰","⣴","⣶","⣷",
203
+ "⢸","⣸","⣼","⣾","⣿"
204
+ };
205
+ #elif GRAPH_MODE == 1
206
+ const char* lookup[9] = {
207
+ " ","▖","▌",
208
+ "▗","▄","▙",
209
+ "▐","▟","█"
210
+ };
211
+ #else
212
+ const char* lookup[25] = {
213
+ " ",".",".","i","|",
214
+ ".","_","_","L","L",
215
+ ".","_","o","b","b",
216
+ "i","j","d",":","%",
217
+ "|","J","d","4","#"
218
+ };
219
+ #endif
220
+
221
+ float max = 0;
222
+ for( int i=0; i<size; i++){
223
+ if( data[i] > max ){
224
+ max = data[i];
225
+ }
226
+ }
227
+
228
+ printf("Max is %f\n",max);
229
+
230
+ int x_iter;
231
+ float l_val, r_val;
232
+ float last=0;
233
+
234
+ int whole_digit_space = (max < 1) ? 1 : ceil(log10(max));
235
+
236
+ printf("%7.5f_\n",max);
237
+ for(int i=0; i<height; i++){
238
+ float base = (height-i-1)*max/height;
239
+ int whole_digits = (base < 1) ? 1 : ceil(log10(base));
240
+ for(int i=whole_digits; i<whole_digit_space; i++){
241
+ printf(" ");
242
+ }
243
+ printf("%7.5f_",base);
244
+ x_iter = 0;
245
+ for(int j=0; j<width; j++){
246
+ l_val = 0;
247
+ r_val = 0;
248
+ int l_limit = (j*2*size)/(width*2);
249
+ int r_limit = ((j*2+1)*size)/(width*2);
250
+ float count = 0.0;
251
+ for(; x_iter < l_limit; x_iter++){
252
+ l_val += data[x_iter];
253
+ //printf("%f,",data[x_iter]);
254
+ count += 1.0;
255
+ }
256
+ l_val = ( count == 0.0 ) ? last : l_val / count;
257
+ last = l_val;
258
+ count = 0.0;
259
+ for(; x_iter < r_limit; x_iter++){
260
+ r_val += data[x_iter];
261
+ count += 1.0;
262
+ }
263
+ r_val = ( count == 0.0 ) ? last : r_val / count;
264
+ last = r_val;
265
+ l_val = ( l_val - base )/max*height*4;
266
+ r_val = ( r_val - base )/max*height*4;
267
+ int l_idx = (l_val <= 0.0) ? 0 : ( (l_val >= 4.0) ? 4 : l_val );
268
+ int r_idx = (r_val <= 0.0) ? 0 : ( (r_val >= 4.0) ? 4 : r_val );
269
+ int str_idx = r_idx*5+l_idx;
270
+ /*
271
+ if( (str_idx < 0) || (str_idx >= 25) ){
272
+ printf("BAD! [%d](%f:%d,%f:%d) -> (%d)",j,l_val,l_idx,r_val,r_idx,str_idx);
273
+ }
274
+ */
275
+ printf("%s",lookup[str_idx]);
276
+ }
277
+ printf("\n");
278
+ }
279
+
280
+ int rule_size = 8*width/2;
281
+ char* rule_vals = new char[rule_size];
282
+ memset(rule_vals,'\0',rule_size);
283
+
284
+ printf(" ");
285
+ for(int j=1; j<whole_digit_space; j++){
286
+ printf(" ");
287
+ }
288
+ for(int j=0; j<width; j+=2){
289
+ float l_limit = low + ((high-low)/width)*j;
290
+ sprintf(&rule_vals[(8*j/2)],"%7.3f",l_limit);
291
+ printf("\\ ");
292
+ }
293
+ printf("\n");
294
+ for(int i=0; i<7; i++){
295
+ printf(" ");
296
+ for(int j=1; j<whole_digit_space; j++){
297
+ printf(" ");
298
+ }
299
+ for(int j=0; j<width; j+=2){
300
+ printf(" %c",rule_vals[(8*j/2)+i]);
301
+ }
302
+ printf("\n");
303
+ }
304
+
305
+ free(rule_vals);
306
+
307
+ }
308
+
309
+
310
+
311
+
312
+
313
+
314
+ struct ArgSet
315
+ {
316
+
317
+ int argc;
318
+ char const ** argv;
319
+
320
+ int get_flag_idx(char const *flag){
321
+ for(int i=0; i<argc; i++){
322
+ char const* str = argv[i];
323
+ if( (str != NULL )
324
+ && (str[0] == '-' )
325
+ && (strcmp(str+1,flag) == 0)
326
+ ){
327
+ return i;
328
+ }
329
+ }
330
+ return -1;
331
+ }
332
+
333
+
334
+ char const *get_flag_str(char const *flag){
335
+ int idx = get_flag_idx(flag);
336
+ if( idx == -1 ) {
337
+ return NULL;
338
+ } else if (idx == (argc-1) ) {
339
+ return (char*) "";
340
+ }
341
+ return argv[idx+1];
342
+ }
343
+
344
+
345
+ template<typename T>
346
+ struct ArgVal {
347
+
348
+ T value;
349
+
350
+ ArgVal(T val) : value(val) {}
351
+
352
+ ArgVal<T> operator| (T other) {
353
+ return *this;
354
+ }
355
+
356
+ operator T() const {
357
+ return value;
358
+ }
359
+
360
+ };
361
+
362
+
363
+ struct ArgQuery {
364
+
365
+ char const *flag_str;
366
+ char const *value_str;
367
+
368
+
369
+ template<typename T>
370
+ bool scan_arg(char const *str, T &dest) const {
371
+ return false;
372
+ }
373
+
374
+
375
+ bool scan_arg(char const *str, unsigned char &dest) const {
376
+ return ( 0 < sscanf(str,"%hhu",&dest) );
377
+ }
378
+
379
+ bool scan_arg(char const *str, unsigned short int &dest) const {
380
+ return ( 0 < sscanf(str,"%hu",&dest) );
381
+ }
382
+
383
+ bool scan_arg(char const *str, unsigned int &dest) const {
384
+ return ( 0 < sscanf(str,"%u",&dest) );
385
+ }
386
+
387
+ bool scan_arg(char const *str, unsigned long int &dest) const {
388
+ return ( 0 < sscanf(str,"%lu",&dest) );
389
+ }
390
+
391
+ bool scan_arg(char const *str, unsigned long long int &dest) const {
392
+ return ( 0 < sscanf(str,"%llu",&dest) );
393
+ }
394
+
395
+
396
+ bool scan_arg(char const *str, signed char &dest) const {
397
+ return ( 0 < sscanf(str,"%hhd",&dest) );
398
+ }
399
+
400
+ bool scan_arg(char const *str, signed short int &dest) const {
401
+ return ( 0 < sscanf(str,"%hd",&dest) );
402
+ }
403
+
404
+ bool scan_arg(char const *str, signed int &dest) const {
405
+ return ( 0 < sscanf(str,"%d",&dest) );
406
+ }
407
+
408
+ bool scan_arg(char const *str, signed long int &dest) const {
409
+ return ( 0 < sscanf(str,"%ld",&dest) );
410
+ }
411
+
412
+ bool scan_arg(char const *str, signed long long int &dest) const {
413
+ return ( 0 < sscanf(str,"%lld",&dest) );
414
+ }
415
+
416
+
417
+
418
+ bool scan_arg(char const *str, float &dest) const {
419
+ return ( 0 < sscanf(str,"%f",&dest) );
420
+ }
421
+
422
+ bool scan_arg(char const *str, double &dest) const {
423
+ return ( 0 < sscanf(str,"%lf",&dest) );
424
+ }
425
+
426
+
427
+ bool scan_arg(char const *str, bool &dest) const{
428
+ if ( strcmp(str,"false") == 0 ){
429
+ dest = false;
430
+ } else if ( strcmp(str,"true" ) == 0 ){
431
+ dest = true;
432
+ } else {
433
+ return false;
434
+ }
435
+ return true;
436
+ }
437
+
438
+
439
+
440
+ ArgQuery(char const* f, char const* v) : flag_str(f), value_str(v) {}
441
+
442
+ template<typename T>
443
+ void scan_or_fail(T& dest) const{
444
+ if(value_str == NULL) {
445
+ printf("No value provided for flag '-%s'\n", flag_str);
446
+ std::exit(1);
447
+ }
448
+ if( !scan_arg(value_str,dest) ){
449
+ printf("Value string '%s' provided for flag '-%s' "
450
+ "could not be parsed\n",
451
+ value_str,flag_str
452
+ );
453
+ std::exit(1);
454
+ }
455
+ }
456
+
457
+ void scan_or_fail(bool& dest) const{
458
+ dest = (value_str != NULL);
459
+ }
460
+
461
+ template<typename T>
462
+ ArgVal<T> operator| (T other) {
463
+ if(value_str == NULL){
464
+ return ArgVal<T>(other);
465
+ } else {
466
+ T value;
467
+ scan_or_fail(value);
468
+ return ArgVal<T>(value);
469
+ }
470
+ }
471
+
472
+ ArgQuery operator| (ArgQuery other) {
473
+ if(value_str == NULL){
474
+ return other;
475
+ } else {
476
+ return *this;
477
+ }
478
+ }
479
+
480
+
481
+ template<typename T>
482
+ operator T() const {
483
+ T value;
484
+ scan_or_fail(value);
485
+ return value;
486
+ }
487
+
488
+ };
489
+
490
+
491
+ ArgQuery operator[] (char const* flag_str) {
492
+ char const *val_str = get_flag_str(flag_str);
493
+ return ArgQuery(flag_str,val_str);
494
+ }
495
+
496
+ ArgSet(int c, char const** v) : argc(c), argv(v) {}
497
+
498
+ };
499
+
500
+
501
+
502
+ }
503
+
504
+ }
505
+
506
+
507
+ #endif
508
+
509
+
510
+