-
Notifications
You must be signed in to change notification settings - Fork 0
/
CStepMan.h
165 lines (143 loc) · 3.1 KB
/
CStepMan.h
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#pragma once
#include "framework.h"
#include <assert.h>
class CStepMan
{
public:
CStepMan(size_t sz) : szCount{ sz }
{
pSteps = new int[sz];
}
~CStepMan()
{
if (pSteps)
delete[] pSteps;
}
/*
1 2 4 8 16 31 2 ^ 5 - 1
1 2 4 2 1 10 (2 ^ 3 - 1) * 2 - 2 ^ 2
1 2 4 8 16 32 63 2 ^ 6 - 1
*/
void SetSpan_Inc(int ns)
{
BasicSplitSpan(ns, szCount, pSteps);
}
/*
1 2 4 8 16 31 2 ^ 5 - 1
1 2 4 2 1 10 (2 ^ 3 - 1) * 2 - 2 ^ 2
1 2 4 8 16 32 63 2 ^ 6 - 1
1 2 4 4 2 1 14 (2 ^ 3 - 1) * 2
1 2 4 8 4 2 1 22 (2 ^ 4 - 1) * 2 - 2 ^ 3 = 22
1 2 4 8 8 4 2 1 30 (2 ^ 4 - 1) * 2 = 30
*/
void SetSpan_IncDec(int ns)
{
assert(ns != 0);
assert(szCount > 2);
size_t szHalf = szCount / 2;
if (szCount % 2) // odd
{
int nSig = ns < 0 ? -1 : 1;
ns = ns < 0 ? -ns : ns;
int nFrac = (((2 << szHalf) - 1) << 1) - (2 << (szHalf - 1));
int nFracThird = (2 << (szHalf - 1)) - 1;
float f = float(nFracThird) / float(nFrac);
int nSpanThird = int(f * float(ns) + .5f);
if (nSpanThird < 1)
BasicSplitSpan(ns * nSig, szCount, pSteps);
else
{
size_t szPos = BasicSplitSpan(nSpanThird * nSig, szHalf, pSteps);
ns -= (nSpanThird << 1);
if (ns > 0)
{
pSteps[szPos] = ns * nSig;
for (size_t i = 1; i <= szPos; i++)
{
pSteps[szPos + i] = pSteps[szPos - i];
}
for (size_t i = szPos * 2 + 1; i < szCount; i++)
pSteps[i] = 0;
}
else
{
for (size_t i = 0; i < szPos; i++)
{
pSteps[szPos + i] = pSteps[szPos - i - 1];
}
for (size_t i = szPos * 2; i < szCount; i++)
pSteps[i] = 0;
}
}
}
else // even
{
int nSpanHalf = ns / 2;
if (nSpanHalf == 0)
{
BasicSplitSpan(ns, szCount, pSteps);
}
else
{
size_t szPos = BasicSplitSpan(nSpanHalf, szHalf, pSteps);
for (size_t i = 0; i < szPos; i++)
{
pSteps[szPos + i] = pSteps[szPos - 1 - i];
}
for (size_t i = szPos * 2; i < szCount; i++)
pSteps[i] = 0;
if (szPos >= szHalf)
pSteps[szPos] += (ns - nSpanHalf * 2);
else
pSteps[szPos * 2] = (ns - nSpanHalf * 2);
}
}
}
int Get(size_t at, bool bReverse = false) const
{
assert(at < szCount);
if (bReverse)
return pSteps[szCount - at - 1];
else
return pSteps[at];
}
int operator[](size_t at) const
{
return Get(at);
}
size_t Count(void) const { return szCount; }
protected:
size_t szCount;
int* pSteps;
static size_t BasicSplitSpan(int ns, size_t sz, int* pRes)
{
assert(ns != 0);
assert(sz > 0);
assert(pRes);
int nFraction = (1 << sz) - 1;
int nMulti = ns > 0 ? 1 : -1;
ns = ns < 0 ? -ns : ns;
float fbs = float(ns) / float(nFraction);
int nTotal = 0;
size_t szLast = sz - 1, szFirst = 0;
for (size_t i = 0; i < sz; i++)
{
if (nTotal >= ns)
break;
int n = int(fbs * float(1 << i) + .5f);
if (n == 0)
szFirst++;
else
{
nTotal += n;
pRes[i - szFirst] = n * nMulti;
if (nTotal >= ns)
szLast = i - szFirst;
}
}
pRes[szLast] += (ns - nTotal) * nMulti;
for (size_t i = szLast + 1; i < sz; i++)
pRes[i] = 0;
return szLast + 1;
}
};