DS_HuffmanEncodingTree.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file DS_HuffmanEncodingTree.h
  11. /// \brief \b [Internal] Generates a huffman encoding tree, used for string and global compression.
  12. ///
  13. #ifndef __HUFFMAN_ENCODING_TREE
  14. #define __HUFFMAN_ENCODING_TREE
  15. #include "RakMemoryOverride.h"
  16. #include "DS_HuffmanEncodingTreeNode.h"
  17. #include "BitStream.h"
  18. #include "Export.h"
  19. #include "DS_LinkedList.h"
  20. namespace RakNet
  21. {
  22. /// This generates special cases of the huffman encoding tree using 8 bit keys with the additional condition that unused combinations of 8 bits are treated as a frequency of 1
  23. class RAK_DLL_EXPORT HuffmanEncodingTree
  24. {
  25. public:
  26. HuffmanEncodingTree();
  27. ~HuffmanEncodingTree();
  28. /// \brief Pass an array of bytes to array and a preallocated BitStream to receive the output.
  29. /// \param [in] input Array of bytes to encode
  30. /// \param [in] sizeInBytes size of \a input
  31. /// \param [out] output The bitstream to write to
  32. void EncodeArray( unsigned char *input, size_t sizeInBytes, RakNet::BitStream * output );
  33. // \brief Decodes an array encoded by EncodeArray().
  34. unsigned DecodeArray( RakNet::BitStream * input, BitSize_t sizeInBits, size_t maxCharsToWrite, unsigned char *output );
  35. void DecodeArray( unsigned char *input, BitSize_t sizeInBits, RakNet::BitStream * output );
  36. /// \brief Given a frequency table of 256 elements, all with a frequency of 1 or more, generate the tree.
  37. void GenerateFromFrequencyTable( unsigned int frequencyTable[ 256 ] );
  38. /// \brief Free the memory used by the tree.
  39. void FreeMemory( void );
  40. private:
  41. /// The root node of the tree
  42. HuffmanEncodingTreeNode *root;
  43. /// Used to hold bit encoding for one character
  44. struct CharacterEncoding
  45. {
  46. unsigned char* encoding;
  47. unsigned short bitLength;
  48. };
  49. CharacterEncoding encodingTable[ 256 ];
  50. void InsertNodeIntoSortedList( HuffmanEncodingTreeNode * node, DataStructures::LinkedList<HuffmanEncodingTreeNode *> *huffmanEncodingTreeNodeList ) const;
  51. };
  52. } // namespace RakNet
  53. #endif
粤ICP备19079148号