| 1 | /* The MIT License |
| 2 | |
| 3 | Copyright (c) 2016-2020 Genome Research Ltd. |
| 4 | |
| 5 | Author: Petr Danecek <[email protected]> |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | of this software and associated documentation files (the "Software"), to deal |
| 9 | in the Software without restriction, including without limitation the rights |
| 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | copies of the Software, and to permit persons to whom the Software is |
| 12 | furnished to do so, subject to the following conditions: |
| 13 | |
| 14 | The above copyright notice and this permission notice shall be included in |
| 15 | all copies or substantial portions of the Software. |
| 16 | |
| 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | THE SOFTWARE. |
| 24 | |
| 25 | */ |
| 26 | /* |
| 27 | Logarithmic binning |
| 28 | |
| 29 | Example of usage: |
| 30 | |
| 31 | // Initialize, make the binning exact up to 10^4, then add a log-step |
| 32 | dist_t *dist = dist_init(4); |
| 33 | |
| 34 | // Insert values |
| 35 | int i; |
| 36 | for (i=0; i<1e6; i++) |
| 37 | dist_insert(dist, i); |
| 38 | |
| 39 | // Number of bins used |
| 40 | int n = dist_n(dist); |
| 41 | |
| 42 | // Now print the distribution |
| 43 | uint32_t beg, end; |
| 44 | for (i=0; i<n; i++) |
| 45 | { |
| 46 | // Raw count in the bin. The boundaries beg,end are optional, |
| 47 | // and can be used to plot correctly the density |
| 48 | uint64_t cnt = dist_get(dist, i, &beg, &end); |
| 49 | if ( !cnt ) continue; |
| 50 | |
| 51 | // Print the interval, count and density |
| 52 | printf("%u\t%u\t%"PRIu64"\t%f\n", beg, end, cnt, (double)cnt/(end-beg)); |
| 53 | } |
| 54 | |
| 55 | // Clean up |
| 56 | dist_destroy(dist); |
| 57 | */ |
| 58 | |
| 59 | #ifndef __DIST_H__ |
| 60 | #define __DIST_H__ |
| 61 | |
| 62 | #include <stdio.h> |
| 63 | #include <inttypes.h> |
| 64 | |
| 65 | typedef struct _dist_t dist_t; |
| 66 | |
| 67 | /* |
| 68 | * dist_init() - init bins |
| 69 | */ |
| 70 | dist_t *dist_init(int npow); |
| 71 | void dist_destroy(dist_t *dist); |
| 72 | |
| 73 | /* |
| 74 | dist_nbins() - get the number of bins |
| 75 | */ |
| 76 | int dist_nbins(dist_t *dist); |
| 77 | |
| 78 | /* |
| 79 | dist_nvalues() - get the total number of values inserted |
| 80 | */ |
| 81 | int dist_nvalues(dist_t *dist); |
| 82 | |
| 83 | /* |
| 84 | dist_insert() - insert new value |
| 85 | dist_insert_n() - insert new value n times |
| 86 | */ |
| 87 | uint32_t dist_insert(dist_t *dist, uint32_t value); |
| 88 | uint32_t dist_insert_n(dist_t *dist, uint32_t value, uint32_t cnt); |
| 89 | |
| 90 | /* |
| 91 | dist_get() |
| 92 | @idx: from the interval [0,dist_n-1] |
| 93 | @beg,end: [beg,end) |
| 94 | */ |
| 95 | uint64_t dist_get(dist_t *dist, uint32_t idx, uint32_t *beg, uint32_t *end); |
| 96 | |
| 97 | #endif |
| 98 |