diff options
Diffstat (limited to 'songdbj/de/jarnbjo/util/io/HuffmanNode.java')
-rw-r--r-- | songdbj/de/jarnbjo/util/io/HuffmanNode.java | 144 |
1 files changed, 0 insertions, 144 deletions
diff --git a/songdbj/de/jarnbjo/util/io/HuffmanNode.java b/songdbj/de/jarnbjo/util/io/HuffmanNode.java deleted file mode 100644 index 88600a4ddd..0000000000 --- a/songdbj/de/jarnbjo/util/io/HuffmanNode.java +++ /dev/null | |||
@@ -1,144 +0,0 @@ | |||
1 | /* | ||
2 | * $ProjectName$ | ||
3 | * $ProjectRevision$ | ||
4 | * ----------------------------------------------------------- | ||
5 | * $Id$ | ||
6 | * ----------------------------------------------------------- | ||
7 | * | ||
8 | * $Author$ | ||
9 | * | ||
10 | * Description: | ||
11 | * | ||
12 | * Copyright 2002-2003 Tor-Einar Jarnbjo | ||
13 | * ----------------------------------------------------------- | ||
14 | * | ||
15 | * Change History | ||
16 | * ----------------------------------------------------------- | ||
17 | * $Log$ | ||
18 | * Revision 1.1 2005/07/11 15:42:36 hcl | ||
19 | * Songdb java version, source. only 1.5 compatible | ||
20 | * | ||
21 | * Revision 1.1.1.1 2004/04/04 22:09:12 shred | ||
22 | * First Import | ||
23 | * | ||
24 | * Revision 1.2 2003/04/10 19:48:31 jarnbjo | ||
25 | * no message | ||
26 | * | ||
27 | */ | ||
28 | |||
29 | package de.jarnbjo.util.io; | ||
30 | |||
31 | import java.io.IOException; | ||
32 | import de.jarnbjo.util.io.BitInputStream; | ||
33 | |||
34 | /** | ||
35 | * Representation of a node in a Huffman tree, used to read | ||
36 | * Huffman compressed codewords from e.g. a Vorbis stream. | ||
37 | */ | ||
38 | |||
39 | final public class HuffmanNode { | ||
40 | |||
41 | private HuffmanNode parent; | ||
42 | private int depth=0; | ||
43 | protected HuffmanNode o0, o1; | ||
44 | protected Integer value; | ||
45 | private boolean full=false; | ||
46 | |||
47 | /** | ||
48 | * creates a new Huffman tree root node | ||
49 | */ | ||
50 | |||
51 | public HuffmanNode() { | ||
52 | this(null); | ||
53 | } | ||
54 | |||
55 | protected HuffmanNode(HuffmanNode parent) { | ||
56 | this.parent=parent; | ||
57 | if(parent!=null) { | ||
58 | depth=parent.getDepth()+1; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | protected HuffmanNode(HuffmanNode parent, int value) { | ||
63 | this(parent); | ||
64 | this.value=new Integer(value); | ||
65 | full=true; | ||
66 | } | ||
67 | |||
68 | protected int read(BitInputStream bis) throws IOException { | ||
69 | HuffmanNode iter=this; | ||
70 | while(iter.value==null) { | ||
71 | iter=bis.getBit()?iter.o1:iter.o0; | ||
72 | } | ||
73 | return iter.value.intValue(); | ||
74 | } | ||
75 | |||
76 | protected HuffmanNode get0() { | ||
77 | return o0==null?set0(new HuffmanNode(this)):o0; | ||
78 | } | ||
79 | |||
80 | protected HuffmanNode get1() { | ||
81 | return o1==null?set1(new HuffmanNode(this)):o1; | ||
82 | } | ||
83 | |||
84 | protected Integer getValue() { | ||
85 | return value; | ||
86 | } | ||
87 | |||
88 | private HuffmanNode getParent() { | ||
89 | return parent; | ||
90 | } | ||
91 | |||
92 | protected int getDepth() { | ||
93 | return depth; | ||
94 | } | ||
95 | |||
96 | private boolean isFull() { | ||
97 | return full?true:(full=o0!=null&&o0.isFull()&&o1!=null&&o1.isFull()); | ||
98 | } | ||
99 | |||
100 | private HuffmanNode set0(HuffmanNode value) { | ||
101 | return o0=value; | ||
102 | } | ||
103 | |||
104 | private HuffmanNode set1(HuffmanNode value) { | ||
105 | return o1=value; | ||
106 | } | ||
107 | |||
108 | private void setValue(Integer value) { | ||
109 | full=true; | ||
110 | this.value=value; | ||
111 | } | ||
112 | |||
113 | /** | ||
114 | * creates a new tree node at the first free location at the given | ||
115 | * depth, and assigns the value to it | ||
116 | * | ||
117 | * @param depth the tree depth of the new node (codeword length in bits) | ||
118 | * @param value the node's new value | ||
119 | */ | ||
120 | |||
121 | public boolean setNewValue(int depth, int value) { | ||
122 | if(isFull()) { | ||
123 | return false; | ||
124 | } | ||
125 | if(depth==1) { | ||
126 | if(o0==null) { | ||
127 | set0(new HuffmanNode(this, value)); | ||
128 | return true; | ||
129 | } | ||
130 | else if(o1==null) { | ||
131 | set1(new HuffmanNode(this, value)); | ||
132 | return true; | ||
133 | } | ||
134 | else { | ||
135 | return false; | ||
136 | } | ||
137 | } | ||
138 | else { | ||
139 | return get0().setNewValue(depth-1, value)? | ||
140 | true: | ||
141 | get1().setNewValue(depth-1, value); | ||
142 | } | ||
143 | } | ||
144 | } | ||