KiCad PCB EDA Suite
Loading...
Searching...
No Matches
context_manager.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 Mark Roszko <[email protected]>
3 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
4 * Copyright (C) 2007 Ecma International (original Java source)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * License info found here:
19 * https://www.loc.gov/preservation/digital/formats/fdd/fdd000491.shtml
20 */
21
22#include "context_manager.h"
23
24using namespace U3D;
25
27 m_symbolCount( CONSTANTS::StaticFull ),
28 m_cumulativeCount( CONSTANTS::StaticFull ),
29 m_elephant( 0x00001fff ),
30 m_maximumSymbolInHistogram( 0x0000ffff ),
31 m_arraySizeIncr( 32 )
32{
33}
34
35
36void CONTEXT_MANAGER::AddSymbol( uint32_t aContext, uint32_t aSymbol )
37{
38 if( aContext < CONSTANTS::StaticFull && aContext != CONSTANTS::Context8
39 && aSymbol < m_maximumSymbolInHistogram )
40 {
41 std::vector<uint32_t>& cumulativeCountRef = m_cumulativeCount[aContext];
42 std::vector<uint32_t>& symbolCountRef = m_symbolCount[aContext];
43
44 if( cumulativeCountRef.empty() || cumulativeCountRef.size() <= aSymbol )
45 {
46 std::vector<uint32_t> newCumulativeCount( aSymbol + m_arraySizeIncr, 0 );
47 std::vector<uint32_t> newSymbolCount( aSymbol + m_arraySizeIncr, 0 );
48
49 if( cumulativeCountRef.empty() )
50 {
51 cumulativeCountRef = newCumulativeCount;
52 cumulativeCountRef[0] = 1;
53
54 symbolCountRef = newSymbolCount;
55 symbolCountRef[0] = 1;
56 }
57 else
58 {
59 std::copy( cumulativeCountRef.begin(), cumulativeCountRef.end(),
60 newCumulativeCount.begin() );
61 std::copy( symbolCountRef.begin(), symbolCountRef.end(),
62 newSymbolCount.begin() );
63 cumulativeCountRef = newCumulativeCount;
64 symbolCountRef = newSymbolCount;
65 }
66 }
67
68 if( cumulativeCountRef[0] >= m_elephant )
69 {
70 size_t len = cumulativeCountRef.size();
71 uint32_t tempAccum = 0;
72 for( int i = (int32_t)len - 1; i >= 0; i-- )
73 {
74 symbolCountRef[i] >>= 1;
75 tempAccum += symbolCountRef[i];
76 cumulativeCountRef[i] = tempAccum;
77 }
78 symbolCountRef[0]++;
79 cumulativeCountRef[0]++;
80 }
81
82 symbolCountRef[aSymbol]++;
83 for( uint32_t i = 0; i <= aSymbol; i++ )
84 {
85 cumulativeCountRef[i]++;
86 }
87 }
88}
89
90
91uint32_t CONTEXT_MANAGER::GetSymbolFrequency( uint32_t aContext, uint32_t aSymbol )
92{
93 uint32_t rValue = 1;
94 if( aContext < CONSTANTS::StaticFull && aContext != CONSTANTS::Context8 )
95 {
96 rValue = 0;
97 if( !m_symbolCount[aContext].empty() && aSymbol < m_symbolCount[aContext].size() )
98 {
99 rValue = m_symbolCount[aContext][aSymbol];
100 }
101 else if( aSymbol == 0 )
102 {
103 rValue = 1;
104 }
105 }
106 return rValue;
107}
108
109
110uint32_t CONTEXT_MANAGER::GetCumulativeSymbolFrequency( uint32_t aContext, uint32_t aSymbol )
111{
112 uint32_t rValue = aSymbol - 1;
113 if( aContext < CONSTANTS::StaticFull && aContext != CONSTANTS::Context8 )
114 {
115 rValue = 0;
116 if( !m_cumulativeCount[aContext].empty() )
117 {
118 if( aSymbol < m_cumulativeCount[aContext].size() )
119 {
120 rValue = m_cumulativeCount[aContext][0] - m_cumulativeCount[aContext][aSymbol];
121 }
122 else
123 {
124 rValue = m_cumulativeCount[aContext][0];
125 }
126 }
127 }
128 return rValue;
129}
130
131
132uint32_t CONTEXT_MANAGER::GetTotalSymbolFrequency( uint32_t aContext )
133{
134 if( aContext < CONSTANTS::StaticFull && aContext != CONSTANTS::Context8 )
135 {
136 uint32_t rValue = 1;
137 if( !m_cumulativeCount[aContext].empty() )
138 {
139 rValue = m_cumulativeCount[aContext][0];
140 }
141 return rValue;
142 }
143
144 if( aContext == CONSTANTS::Context8 )
145 {
146 return 256;
147 }
148
149 return aContext - CONSTANTS::StaticFull;
150}
151
152
153uint32_t CONTEXT_MANAGER::GetSymbolFromFrequency( uint32_t aContext, uint32_t aSymbolFrequency )
154{
155 uint32_t rValue = 0;
156 if( aContext < CONSTANTS::StaticFull && aContext != CONSTANTS::Context8 )
157 {
158 rValue = 0;
159 if( !m_cumulativeCount[aContext].empty() && aSymbolFrequency != 0
160 && m_cumulativeCount[aContext][0] >= aSymbolFrequency )
161 {
162 for( size_t i = 0; i < m_cumulativeCount[aContext].size(); i++ )
163 {
164 if( GetCumulativeSymbolFrequency( aContext, (uint32_t) i ) <= aSymbolFrequency )
165 {
166 rValue = (uint32_t) i;
167 }
168 else
169 {
170 break;
171 }
172 }
173 }
174 }
175 else
176 {
177 rValue = aSymbolFrequency + 1;
178 }
179 return rValue;
180}
static const uint32_t StaticFull
Contexts greater than this are static contexts.
Definition constants.h:57
static const uint32_t Context8
Uncompressed U8 Context.
Definition constants.h:35
uint32_t GetSymbolFrequency(uint32_t aContext, uint32_t aSymbol)
Get the number of occurrences of the given symbol in the context.
const uint32_t m_arraySizeIncr
the ammount to increase the size of an array when reallocating an array.
uint32_t GetSymbolFromFrequency(uint32_t aContext, uint32_t symbolFrequency)
Find the symbol in a histogram that has the specified cumulative frequency.
uint32_t GetTotalSymbolFrequency(uint32_t aContext)
Get the total occurrences of all the symbols in this context.
std::vector< std::vector< std::uint32_t > > m_cumulativeCount
uint32_t GetCumulativeSymbolFrequency(uint32_t aContext, uint32_t aSymbol)
Get the total number of occurrences for all symbols less than the given symbol in the context.
std::vector< std::vector< std::uint32_t > > m_symbolCount
void AddSymbol(uint32_t aContext, uint32_t aSymbol)
Add an occurrence of the symbol to the specified context.
const uint32_t m_maximumSymbolInHistogram
the maximum value that is stored in a histogram
const uint32_t m_elephant
Elephant determines the number of symbol occurences that are stored in each histogram.
static bool empty(const wxTextEntryBase *aCtrl)