BitPackage.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is Mozilla Universal charset detector code.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 2001
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. * Kohei TAKETA <k-tak@void.in> (Java port)
  23. * Rudi Pettazzi <rudi.pettazzi@gmail.com> (C# port)
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either the GNU General Public License Version 2 or later (the "GPL"), or
  27. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. namespace UniversalDetector.Core
  39. {
  40. public class BitPackage
  41. {
  42. public static int INDEX_SHIFT_4BITS = 3;
  43. public static int INDEX_SHIFT_8BITS = 2;
  44. public static int INDEX_SHIFT_16BITS = 1;
  45. public static int SHIFT_MASK_4BITS = 7;
  46. public static int SHIFT_MASK_8BITS = 3;
  47. public static int SHIFT_MASK_16BITS = 1;
  48. public static int BIT_SHIFT_4BITS = 2;
  49. public static int BIT_SHIFT_8BITS = 3;
  50. public static int BIT_SHIFT_16BITS = 4;
  51. public static int UNIT_MASK_4BITS = 0x0000000F;
  52. public static int UNIT_MASK_8BITS = 0x000000FF;
  53. public static int UNIT_MASK_16BITS = 0x0000FFFF;
  54. private int indexShift;
  55. private int shiftMask;
  56. private int bitShift;
  57. private int unitMask;
  58. private int[] data;
  59. public BitPackage(int indexShift, int shiftMask,
  60. int bitShift, int unitMask, int[] data)
  61. {
  62. this.indexShift = indexShift;
  63. this.shiftMask = shiftMask;
  64. this.bitShift = bitShift;
  65. this.unitMask = unitMask;
  66. this.data = data;
  67. }
  68. public static int Pack16bits(int a, int b)
  69. {
  70. return ((b << 16) | a);
  71. }
  72. public static int Pack8bits(int a, int b, int c, int d)
  73. {
  74. return Pack16bits((b << 8) | a, (d << 8) | c);
  75. }
  76. public static int Pack4bits(int a, int b, int c, int d,
  77. int e, int f, int g, int h)
  78. {
  79. return Pack8bits((b << 4) | a, (d << 4) | c,
  80. (f << 4) | e, (h << 4) | g);
  81. }
  82. public int Unpack(int i)
  83. {
  84. return (data[i >> indexShift] >>
  85. ((i & shiftMask) << bitShift)) & unitMask;
  86. }
  87. }
  88. }