コード例 #1
0
 def test_build_codes(self):
     test3=Node(71,20,Leaf(71,10),Leaf(80,10))
     test4=array_list.List([None]*256,256,256)
     test5=None
     test6=array_list.List([None]*256,256,256)
     build_codes(test3, test4)
     build_codes(test5, test6)
     self.assertEqual(test4,array_list.List([None]*71+["0"]+[None]*8+["1"]+[None]*175,256,256))
     self.assertEqual(test6,array_list.List([None]*256,256,256))
コード例 #2
0
def header_reader(bitreader):
    alist= array_list.List([0]*256,256,256)
    count=bitreader.read_byte()
    for i in range(count):
        char=bitreader.read_byte()
        num=bitreader.read_int()
        alist=array_list.set(alist,char,num)
    return alist
コード例 #3
0
def huffman_encode(infile,outfile):
    chrcount=count_occurances(infile)                   #Arraylist containing the counts of each character
    htree=build_Hufftree(chrcount)                      #HuffmanTree of all the characters
    codelist=array_list.List([None]*256,256,256)        #Arraylist containing the codes of each character
    build_codes(htree, codelist)
    bitwriter=HuffmanBitsWriter(outfile)                #HuffmanBitsWriter
    header_writer(bitwriter,chrcount)                   #Writes the header
    file_encoder(bitwriter, infile, codelist)           #Encodes the file
    bitwriter.close()                                   #Closes the HuffmanBitsWriter
    return huff_tostring(htree)                         
コード例 #4
0
def count_occurances(path):
    file=open_file(path)
    if(file==None):
        return None
    else:
        huffcount=array_list.List([0]*256,256,256)
        for line in file:
            for ch in line:
                val=ord(ch)
                huffcount=array_list.set(huffcount, val , array_list.get(huffcount,val)+1)
        file.close()
        return huffcount
コード例 #5
0
 def test_build_Hufftree(self):
     self.assertEqual(build_Hufftree(array_list.List([0]*256,256,256)),None)
     self.assertEqual(build_Hufftree(count_occurances("test1.txt")),Node(10, 104, Node(10, 40, Node(103, 16, Node(103, 8, Node(103, 4, Leaf(103, 2), Node(105, 2, Leaf(105, 1), Leaf(106, 1))), Node(107, 4, Node(107, 2, Leaf(107, 1), Leaf(108, 1)), Node(109, 2, Leaf(109, 1), Leaf(110, 1)))), Node(111, 8, Node(111, 4, Node(111, 2, Leaf(111, 1), Leaf(112, 1)), Node(113, 2, Leaf(113, 1), Leaf(114, 1))), Node(115, 4, Node(115, 2, Leaf(115, 1), Leaf(116, 1)), Node(117, 2, Leaf(117, 1), Leaf(118, 1))))), Node(10, 24, Node(119, 8, Node(119, 4, Node(119, 2, Leaf(119, 1), Leaf(120, 1)), Node(121, 2, Leaf(121, 1), Leaf(122, 1))), Node(123, 4, Node(123, 2, Leaf(123, 1), Leaf(124, 1)), Node(125, 2, Leaf(125, 1), Leaf(126, 1)))), Node(10, 16, Node(10, 8, Node(10, 4, Node(10, 2, Leaf(10, 1), Leaf(33, 1)), Leaf(32, 2)), Node(34, 4, Node(34, 2, Leaf(34, 1), Leaf(35, 1)), Node(36, 2, Leaf(36, 1), Leaf(37, 1)))), Node(38, 8, Node(38, 4, Node(38, 2, Leaf(38, 1), Leaf(39, 1)), Node(40, 2, Leaf(40, 1), Leaf(41, 1))), Node(42, 4, Node(42, 2, Leaf(42, 1), Leaf(43, 1)), Node(44, 2, Leaf(44, 1), Leaf(45, 1))))))), Node(46, 64, Node(46, 32, Node(46, 16, Node(46, 8, Node(46, 4, Node(46, 2, Leaf(46, 1), Leaf(47, 1)), Node(48, 2, Leaf(48, 1), Leaf(49, 1))), Node(50, 4, Node(50, 2, Leaf(50, 1), Leaf(51, 1)), Node(52, 2, Leaf(52, 1), Leaf(53, 1)))), Node(54, 8, Node(54, 4, Node(54, 2, Leaf(54, 1), Leaf(55, 1)), Node(56, 2, Leaf(56, 1), Leaf(57, 1))), Node(58, 4, Node(58, 2, Leaf(58, 1), Leaf(59, 1)), Node(60, 2, Leaf(60, 1), Leaf(61, 1))))), Node(62, 16, Node(62, 8, Node(62, 4, Node(62, 2, Leaf(62, 1), Leaf(63, 1)), Node(64, 2, Leaf(64, 1), Leaf(65, 1))), Node(66, 4, Node(66, 2, Leaf(66, 1), Leaf(67, 1)), Node(68, 2, Leaf(68, 1), Leaf(69, 1)))), Node(70, 8, Node(70, 4, Node(70, 2, Leaf(70, 1), Leaf(71, 1)), Node(72, 2, Leaf(72, 1), Leaf(73, 1))), Node(74, 4, Node(74, 2, Leaf(74, 1), Leaf(75, 1)), Node(76, 2, Leaf(76, 1), Leaf(77, 1)))))), Node(78, 32, Node(78, 16, Node(78, 8, Node(78, 4, Node(78, 2, Leaf(78, 1), Leaf(79, 1)), Node(80, 2, Leaf(80, 1), Leaf(81, 1))), Node(82, 4, Node(82, 2, Leaf(82, 1), Leaf(83, 1)), Node(84, 2, Leaf(84, 1), Leaf(85, 1)))), Node(86, 8, Node(86, 4, Node(86, 2, Leaf(86, 1), Leaf(87, 1)), Node(88, 2, Leaf(88, 1), Leaf(89, 1))), Node(90, 4, Node(90, 2, Leaf(90, 1), Leaf(91, 1)), Node(92, 2, Leaf(92, 1), Leaf(93, 1))))), Node(94, 16, Node(94, 8, Node(94, 4, Node(94, 2, Leaf(94, 1), Leaf(95, 1)), Node(96, 2, Leaf(96, 1), Leaf(104, 1))), Node(97, 4, Leaf(97, 2), Leaf(98, 2))), Node(99, 8, Node(99, 4, Leaf(99, 2), Leaf(100, 2)), Node(101, 4, Leaf(101, 2), Leaf(102, 2))))))))
コード例 #6
0
 def test_convert_llist(self):
     self.assertEqual(convert_llist(array_list.List([10,0,0,1,2,3,0],7,7)),
                      linked_list.Pair(Leaf(3, 1), linked_list.Pair(Leaf(4, 2), linked_list.Pair(Leaf(5, 3), linked_list.Pair(Leaf(0, 10), None)))))
コード例 #7
0
 def test_count_occurances(self):
     help=count_occurances("help.txt")
     self.assertEqual(help,None)
     self.assertEqual(count_occurances("none.txt"),None)
     self.assertEqual(count_occurances("test1.txt"),array_list.List([0]*10+[1]+[0]*21+[2]+[1]*64+[2]*7+[1]*23+[0]*129,256,256))