1 #include "bcftools.pysam.h"
2 
3 /* The MIT License
4 
5    Copyright (c) 2016-2020 Genome Research Ltd.
6 
7    Author: Petr Danecek <pd3@sanger.ac.uk>
8 
9    Permission is hereby granted, free of charge, to any person obtaining a copy
10    of this software and associated documentation files (the "Software"), to deal
11    in the Software without restriction, including without limitation the rights
12    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13    copies of the Software, and to permit persons to whom the Software is
14    furnished to do so, subject to the following conditions:
15 
16    The above copyright notice and this permission notice shall be included in
17    all copies or substantial portions of the Software.
18 
19    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25    THE SOFTWARE.
26 
27  */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include <string.h>
33 #include <assert.h>
34 #include "dist.h"
35 
36 extern void error(const char *format, ...);
37 
38 struct _dist_t
39 {
40     uint64_t *bins, nvalues;
41     int nbins;
42     int npow;   // the number of orders of magnitude to represent exactly
43     int nexact; // pow(10,npow)
44     int nlevel;
45 };
46 
dist_init(int npow)47 dist_t *dist_init(int npow)
48 {
49     dist_t *dist = (dist_t*) calloc(1,sizeof(dist_t));
50     dist->npow   = npow;
51     dist->nexact = pow(10,npow);
52     dist->nlevel = dist->nexact - pow(10,npow-1);
53     return dist;
54 }
55 
dist_destroy(dist_t * dist)56 void dist_destroy(dist_t *dist)
57 {
58     if ( !dist ) return;
59     free(dist->bins);
60     free(dist);
61 }
62 
dist_nbins(dist_t * dist)63 int dist_nbins(dist_t *dist)
64 {
65     return dist->nbins;
66 }
67 
dist_nvalues(dist_t * dist)68 int dist_nvalues(dist_t *dist)
69 {
70     return dist->nvalues;
71 }
72 
dist_insert(dist_t * dist,uint32_t value)73 uint32_t dist_insert(dist_t *dist, uint32_t value)
74 {
75     int ibin;
76 
77     if ( value <= dist->nexact )
78         ibin = value;
79     else
80     {
81         int npow  = (int) log10(value);
82         int level = npow - dist->npow + 1;
83         uint32_t step = pow(10, level);
84         ibin = dist->nexact + dist->nlevel*(level-1) + (value - pow(10,npow)) / step;
85     }
86 
87     if ( ibin >= dist->nbins )
88     {
89         dist->bins = (uint64_t*) realloc(dist->bins, sizeof(*dist->bins)*(ibin+1));
90         memset(dist->bins + dist->nbins, 0, (ibin+1 - dist->nbins)*sizeof(*dist->bins));
91         dist->nbins = ibin+1;
92     }
93     dist->bins[ibin]++;
94     dist->nvalues++;
95     return ibin;
96 }
dist_insert_n(dist_t * dist,uint32_t value,uint32_t cnt)97 uint32_t dist_insert_n(dist_t *dist, uint32_t value, uint32_t cnt)
98 {
99     if ( !cnt ) return 0;
100     int ibin = dist_insert(dist, value);
101     dist->bins[ibin] += cnt - 1;
102     dist->nvalues += cnt;
103     return ibin;
104 }
105 
dist_get(dist_t * dist,uint32_t idx,uint32_t * beg,uint32_t * end)106 uint64_t dist_get(dist_t *dist, uint32_t idx, uint32_t *beg, uint32_t *end)
107 {
108     if ( idx < dist->nexact )
109     {
110         if ( beg ) *beg = idx;
111         if ( end ) *end = idx + 1;
112     }
113     else
114     {
115         int level = (idx - dist->nexact) / dist->nlevel + 1;
116         int bin   = idx - dist->nexact - dist->nlevel*(level-1);
117 
118         uint32_t step  = pow(10, level);
119         uint32_t value = pow(10, level + dist->npow - 1) + step*bin;
120 
121         if ( beg ) *beg = value;
122         if ( end ) *end = value + step;
123     }
124     return dist->bins[idx];
125 }
126 
127