@@ -29,7 +29,7 @@ public class Huffman {
29
29
/* The tree structure. The first 256 entries are for the leaf nodes (not all
30
30
of those may be used, depending on the input). We add additional nodes as
31
31
we build the tree. */
32
- var tree = [ Node] ( count : 256 , repeatedValue : Node ( ) )
32
+ var tree = [ Node] ( repeating : Node ( ) , count : 256 )
33
33
34
34
/* This is the last node we add to the tree. */
35
35
var root : NodeIndex = - 1
@@ -50,10 +50,10 @@ extension Huffman {
50
50
occurs. These counts are stored in the first 256 nodes in the tree, i.e.
51
51
the leaf nodes. The frequency table used by decompression is derived from
52
52
this. */
53
- private func countByteFrequency( data: NSData ) {
54
- var ptr = UnsafePointer < UInt8 > ( data. bytes)
53
+ fileprivate func countByteFrequency( inData data: NSData ) {
54
+ var ptr = data. bytes. assumingMemoryBound ( to : UInt8 . self )
55
55
for _ in 0 ..< data. length {
56
- let i = Int ( ptr. memory )
56
+ let i = Int ( ptr. pointee )
57
57
tree [ i] . count += 1
58
58
tree [ i] . index = i
59
59
ptr = ptr. successor ( )
@@ -62,7 +62,7 @@ extension Huffman {
62
62
63
63
/* Takes a frequency table and rebuilds the tree. This is the first step of
64
64
decompression. */
65
- private func restoreTree( frequencyTable: [ Freq ] ) {
65
+ fileprivate func restoreTree( fromTable frequencyTable: [ Freq ] ) {
66
66
for freq in frequencyTable {
67
67
let i = Int ( freq. byte)
68
68
tree [ i] . count = freq. count
@@ -85,7 +85,7 @@ extension Huffman {
85
85
86
86
extension Huffman {
87
87
/* Builds a Huffman tree from a frequency table. */
88
- private func buildTree( ) {
88
+ fileprivate func buildTree( ) {
89
89
// Create a min-priority queue and enqueue all used nodes.
90
90
var queue = PriorityQueue < Node > ( sort: { $0. count < $1. count } )
91
91
for node in tree where node. count > 0 {
@@ -123,13 +123,13 @@ extension Huffman {
123
123
extension Huffman {
124
124
/* Compresses the contents of an NSData object. */
125
125
public func compressData( data: NSData ) -> NSData {
126
- countByteFrequency ( data)
126
+ countByteFrequency ( inData : data)
127
127
buildTree ( )
128
128
129
129
let writer = BitWriter ( )
130
- var ptr = UnsafePointer < UInt8 > ( data. bytes)
130
+ var ptr = data. bytes. assumingMemoryBound ( to : UInt8 . self )
131
131
for _ in 0 ..< data. length {
132
- let c = ptr. memory
132
+ let c = ptr. pointee
133
133
let i = Int ( c)
134
134
traverseTree ( writer: writer, nodeIndex: i, childIndex: - 1 )
135
135
ptr = ptr. successor ( )
@@ -141,15 +141,15 @@ extension Huffman {
141
141
/* Recursively walks the tree from a leaf node up to the root, and then back
142
142
again. If a child is the right node, we emit a 0 bit; if it's the left node,
143
143
we emit a 1 bit. */
144
- private func traverseTree( writer writer : BitWriter , nodeIndex h: Int , childIndex child: Int ) {
144
+ private func traverseTree( writer: BitWriter , nodeIndex h: Int , childIndex child: Int ) {
145
145
if tree [ h] . parent != - 1 {
146
146
traverseTree ( writer: writer, nodeIndex: tree [ h] . parent, childIndex: h)
147
147
}
148
148
if child != - 1 {
149
149
if child == tree [ h] . left {
150
- writer. writeBit ( true )
150
+ writer. writeBit ( bit : true )
151
151
} else if child == tree [ h] . right {
152
- writer. writeBit ( false )
152
+ writer. writeBit ( bit : false )
153
153
}
154
154
}
155
155
}
@@ -158,7 +158,7 @@ extension Huffman {
158
158
extension Huffman {
159
159
/* Takes a Huffman-compressed NSData object and outputs the uncompressed data. */
160
160
public func decompressData( data: NSData , frequencyTable: [ Freq ] ) -> NSData {
161
- restoreTree ( frequencyTable)
161
+ restoreTree ( fromTable : frequencyTable)
162
162
163
163
let reader = BitReader ( data: data)
164
164
let outData = NSMutableData ( )
@@ -167,7 +167,7 @@ extension Huffman {
167
167
var i = 0
168
168
while i < byteCount {
169
169
var b = findLeafNode ( reader: reader, nodeIndex: root)
170
- outData. appendBytes ( & b, length: 1 )
170
+ outData. append ( & b, length: 1 )
171
171
i += 1
172
172
}
173
173
return outData
@@ -177,7 +177,7 @@ extension Huffman {
177
177
next bit and use that to determine whether to step to the left or right.
178
178
When we get to the leaf node, we simply return its index, which is equal to
179
179
the original byte value. */
180
- private func findLeafNode( reader reader : BitReader , nodeIndex: Int ) -> UInt8 {
180
+ private func findLeafNode( reader: BitReader , nodeIndex: Int ) -> UInt8 {
181
181
var h = nodeIndex
182
182
while tree [ h] . right != - 1 {
183
183
if reader. readBit ( ) {
0 commit comments