<commit_before>/*
Copyright 2015 Esri

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

A local copy of the license and additional notices are located with the
source distribution at:

http://github.com/Esri/lerc/

Contributors:  <NAME>
*/

#ifndef HUFFMAN_H
#define HUFFMAN_H
// ---- includes ------------------------------------------------------------ ;

#include "Defines.h"
#include <vector>

NAMESPACE_LERC_START

// ---- related classes ----------------------------------------------------- ;

// -------------------------------------------------------------------------- ;

class Huffman
{
public:
  Huffman() : m_maxHistoSize(1 << 15), m_maxNumBitsLUT(12), m_root(NULL) {};
  ~Huffman() { Clear(); };

  // Limitation: We limit the max Huffman code length to 32 bit. If this happens, the function ComputeCodes() 
  // returns false. In that case don't use Huffman coding but Lerc only instead. 
  // This won't happen easily. For the worst case input maximizing the Huffman code length the counts in the 
  // histogram have to follow the Fibonacci sequence. Even then, for < 9,227,465 data values, 32 bit is
  // the max Huffman code length possible. 

  bool ComputeCodes(const std::vector<int>& histo);    // input histogram, size < 2^15

  bool ComputeCompressedSize(const std::vector<int>& histo, int& numBytes, double& avgBpp) const;

  // LUT of same size as histogram, each entry has length of Huffman bit code, and the bit code
  const std::vector<std::pair<short, unsigned int> >& GetCodes() const  { return m_codeTable; }
  bool SetCodes(const std::vector<std::pair<short, unsigned int> >& codeTable);

  bool WriteCodeTable(Byte** ppByte) const;
  bool ReadCodeTable(const Byte** ppByte);

  bool BuildTreeFromCodes(int& numBitsLUT);
  bool DecodeOneValue(const unsigned int** srcPtr, int& bitPos, int numBitsLUT, int& value) const;
  void Clear();

private:

  struct Node
  {
    int weight;<commit_msg>Uninitialized member variable, found by Coverity<commit_after>/*
Copyright 2015 Esri

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

A local copy of the license and additional notices are located with the
source distribution at:

http://github.com/Esri/lerc/

Contributors:  <NAME>
*/

#ifndef HUFFMAN_H
#define HUFFMAN_H
// ---- includes ------------------------------------------------------------ ;

#include "Defines.h"
#include <vector>

NAMESPACE_LERC_START

// ---- related classes ----------------------------------------------------- ;

// -------------------------------------------------------------------------- ;

class Huffman
{
public:
  Huffman() : m_maxHistoSize(1 << 15), m_maxNumBitsLUT(12), m_numBitsToSkipInTree(0), m_root(NULL) {};
  ~Huffman() { Clear(); };

  // Limitation: We limit the max Huffman code length to 32 bit. If this happens, the function ComputeCodes() 
  // returns false. In that case don't use Huffman coding but Lerc only instead. 
  // This won't happen easily. For the worst case input maximizing the Huffman code length the counts in the 
  // histogram have to follow the Fibonacci sequence. Even then, for < 9,227,465 data values, 32 bit is
  // the max Huffman code length possible. 

  bool ComputeCodes(const std::vector<int>& histo);    // input histogram, size < 2^15

  bool ComputeCompressedSize(const std::vector<int>& histo, int& numBytes, double& avgBpp) const;

  // LUT of same size as histogram, each entry has length of Huffman bit code, and the bit code
  const std::vector<std::pair<short, unsigned int> >& GetCodes() const  { return m_codeTable; }
  bool SetCodes(const std::vector<std::pair<short, unsigned int> >& codeTable);

  bool WriteCodeTable(Byte** ppByte) const;
  bool ReadCodeTable(const Byte** ppByte);

  bool BuildTreeFromCodes(int& numBitsLUT);
  bool DecodeOneValue(const unsigned int** srcPtr, int& bitPos, int numBitsLUT, int& value) const;
  void Clear();

private:

  struct Node
  {
    int weight;
{'fraction_non_alphanumeric': 0.16388717315215154, 'fraction_numerical': 0.011323862466543133, 'mean_word_length': 2.92090395480226, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4319856', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1317, 'n_words': 549}