File: nc_utils.h

package info (click to toggle)
kmc 2.3%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,412 kB
  • sloc: cpp: 17,316; perl: 178; makefile: 90; sh: 16
file content (139 lines) | stat: -rw-r--r-- 3,555 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
  This file is a part of KMC software distributed under GNU GPL 3 licence.
  The homepage of the KMC project is http://sun.aei.polsl.pl/kmc

  This file demonstrates the example usage of kmc_api software. 
  It reads kmer_counter's output and prints kmers to an output file.

  Authors: Sebastian Deorowicz, Agnieszka Debudaj-Grabysz, Marek Kokot

  Version: 2.3.0
  Date   : 2015-08-21
*/

#include <cmath>
#include <string>
#include "../kmc_api/kmer_defs.h"

#ifndef _NC_UTILS_H
#define _NC_UTILS_H
class CNumericConversions {
public:
    static uchar digits[100000*5];
	static int powOf10[30];
    struct _si {
        _si()
        {
            for(int i = 0; i < 100000; ++i)
            {
                int dig = i;

                digits[i*5+4] = '0' + (dig % 10);
                dig /= 10;
                digits[i*5+3] = '0' + (dig % 10);
                dig /= 10;
                digits[i*5+2] = '0' + (dig % 10);
                dig /= 10;
                digits[i*5+1] = '0' + (dig % 10);
                dig /= 10;
                digits[i*5+0] = '0' + dig;
            }
			powOf10[0] = 1;
			for(int i = 1 ; i < 30 ; ++i)
			{
				powOf10[i] = powOf10[i-1]*10;
			}
        }
    } static _init;

    static int NDigits(uint64 val)
    {
        if(val >= 10000)
            return 5;
        else if(val >= 1000)
            return 4;
        else if(val >= 100)
            return 3;
        else if(val >= 10)
            return 2;
        else
            return 1;
    }

    static int Int2PChar(uint64 val, uchar *str)
    {
        if(val >= 1000000000000000ull)
        {
            uint64 dig1 = val / 1000000000000000ull;
            val -= dig1 * 1000000000000000ull;
            uint64 dig2 = val / 10000000000ull;
            val -= dig2 * 10000000000ull;
            uint64 dig3 = val / 100000ull;
            uint64 dig4 = val - dig3 * 100000ull;

            int ndig = NDigits(dig1);

            memcpy(str, digits+dig1*5+(5-ndig), ndig);
            memcpy(str+ndig, digits+dig2*5, 5);
            memcpy(str+ndig+5, digits+dig3*5, 5);
            memcpy(str+ndig+10, digits+dig4*5, 5);

            return ndig+15;
        }
        else if(val >= 10000000000ull)
        {
            uint64 dig1 = val / 10000000000ull;
            val -= dig1 * 10000000000ull;
            uint64 dig2 = val / 100000ull;
            uint64 dig3 = val - dig2 * 100000ull;

            int ndig = NDigits(dig1);

            memcpy(str, digits+dig1*5+(5-ndig), ndig);
            memcpy(str+ndig, digits+dig2*5, 5);
            memcpy(str+ndig+5, digits+dig3*5, 5);

            return ndig+10;
        }
        else if(val >= 100000ull)
        {
            uint64 dig1 = val / 100000ull;
            uint64 dig2 = val - dig1 * 100000ull;

            int ndig = NDigits(dig1);

            memcpy(str, digits+dig1*5+(5-ndig), ndig);
            memcpy(str+ndig, digits+dig2*5, 5);

            return ndig+5;
        }
        else
        {
            int ndig = NDigits(val);

            memcpy(str, digits+val*5+(5-ndig), ndig);

            return ndig;
        }
	}

	static int Double2PChar(double val, int prec, uchar *str)
	{
		double corrector = .5 / powOf10[prec];
		val += corrector;
		double ipart;
		double fractPart = std::modf(val, &ipart);
		uint32 intPart = (uint32)ipart;
		uint32 len = Int2PChar(intPart, str);
		uint32 pos = len;
		str[pos++] = '.';
		for(int i = 0 ; i < prec ; ++i)
		{
			fractPart *= 10;
			str[pos++] = '0' + (uint32)fractPart  % 10 ;
		}
		return len + prec + 1;
	}
};

#endif