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#include <cstddef>
24
25using namespace U3D;
26
28 m_symbolCount( CONSTANTS::StaticFull ),
29 m_cumulativeCount( CONSTANTS::StaticFull ),
30 m_elephant( 0x00001fff ),
31 m_maximumSymbolInHistogram( 0x0000ffff ),
32 m_arraySizeIncr( 32 )
33{
34}
35
36
37void CONTEXT_MANAGER::AddSymbol( uint32_t aContext, uint32_t aSymbol )
38{
39 if( aContext < CONSTANTS::StaticFull && aContext != CONSTANTS::Context8
40 && aSymbol < m_maximumSymbolInHistogram )
41 {
42 std::vector<uint32_t>& cumulativeCountRef = m_cumulativeCount[aContext];
43 std::vector<uint32_t>& symbolCountRef = m_symbolCount[aContext];
44
45 if( cumulativeCountRef.empty() || cumulativeCountRef.size() <= aSymbol )
46 {
47 std::vector<uint32_t> newCumulativeCount( aSymbol + m_arraySizeIncr, 0 );
48 std::vector<uint32_t> newSymbolCount( aSymbol + m_arraySizeIncr, 0 );
49
50 if( cumulativeCountRef.empty() )
51 {
52 cumulativeCountRef = newCumulativeCount;
53 cumulativeCountRef[0] = 1;
54
55 symbolCountRef = newSymbolCount;
56 symbolCountRef[0] = 1;
57 }
58 else
59 {
60 std::copy( cumulativeCountRef.begin(), cumulativeCountRef.end(),
61 newCumulativeCount.begin() );
62 std::copy( symbolCountRef.begin(), symbolCountRef.end(),
63 newSymbolCount.begin() );
64 cumulativeCountRef = newCumulativeCount;
65 symbolCountRef = newSymbolCount;
66 }
67 }
68
69 if( cumulativeCountRef[0] >= m_elephant )
70 {
71 size_t len = cumulativeCountRef.size();
72 uint32_t tempAccum = 0;
73 for( int i = (int32_t)len - 1; i >= 0; i-- )
74 {
75 symbolCountRef[i] >>= 1;
76 tempAccum += symbolCountRef[i];
77 cumulativeCountRef[i] = tempAccum;
78 }
79 symbolCountRef[0]++;
80 cumulativeCountRef[0]++;
81 }
82
83 symbolCountRef[aSymbol]++;
84 for( uint32_t i = 0; i <= aSymbol; i++ )
85 {
86 cumulativeCountRef[i]++;
87 }
88 }
89}
90
91
92uint32_t CONTEXT_MANAGER::GetSymbolFrequency( uint32_t aContext, uint32_t aSymbol )
93{
94 uint32_t rValue = 1;
95 if( aContext < CONSTANTS::StaticFull && aContext != CONSTANTS::Context8 )
96 {
97 rValue = 0;
98 if( !m_symbolCount[aContext].empty() && aSymbol < m_symbolCount[aContext].size() )
99 {
100 rValue = m_symbolCount[aContext][aSymbol];
101 }
102 else if( aSymbol == 0 )
103 {
104 rValue = 1;
105 }
106 }
107 return rValue;
108}
109
110
111uint32_t CONTEXT_MANAGER::GetCumulativeSymbolFrequency( uint32_t aContext, uint32_t aSymbol )
112{
113 uint32_t rValue = aSymbol - 1;
114 if( aContext < CONSTANTS::StaticFull && aContext != CONSTANTS::Context8 )
115 {
116 rValue = 0;
117 if( !m_cumulativeCount[aContext].empty() )
118 {
119 if( aSymbol < m_cumulativeCount[aContext].size() )
120 {
121 rValue = m_cumulativeCount[aContext][0] - m_cumulativeCount[aContext][aSymbol];
122 }
123 else
124 {
125 rValue = m_cumulativeCount[aContext][0];
126 }
127 }
128 }
129 return rValue;
130}
131
132
133uint32_t CONTEXT_MANAGER::GetTotalSymbolFrequency( uint32_t aContext )
134{
135 if( aContext < CONSTANTS::StaticFull && aContext != CONSTANTS::Context8 )
136 {
137 uint32_t rValue = 1;
138 if( !m_cumulativeCount[aContext].empty() )
139 {
140 rValue = m_cumulativeCount[aContext][0];
141 }
142 return rValue;
143 }
144
145 if( aContext == CONSTANTS::Context8 )
146 {
147 return 256;
148 }
149
150 return aContext - CONSTANTS::StaticFull;
151}
152
153
154uint32_t CONTEXT_MANAGER::GetSymbolFromFrequency( uint32_t aContext, uint32_t aSymbolFrequency )
155{
156 uint32_t rValue = 0;
157 if( aContext < CONSTANTS::StaticFull && aContext != CONSTANTS::Context8 )
158 {
159 rValue = 0;
160 if( !m_cumulativeCount[aContext].empty() && aSymbolFrequency != 0
161 && m_cumulativeCount[aContext][0] >= aSymbolFrequency )
162 {
163 for( size_t i = 0; i < m_cumulativeCount[aContext].size(); i++ )
164 {
165 if( GetCumulativeSymbolFrequency( aContext, (uint32_t) i ) <= aSymbolFrequency )
166 {
167 rValue = (uint32_t) i;
168 }
169 else
170 {
171 break;
172 }
173 }
174 }
175 }
176 else
177 {
178 rValue = aSymbolFrequency + 1;
179 }
180 return rValue;
181}
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)