def test_Mix3(self): """Three Levels.""" frequencies = PartiallySortedArray([8] * 8 + [255, 1024]) frequencies, externals, internals = INITIALIZE(frequencies) self.assertEqual(nodeListToWeightList(internals), [16]) self.assertEqual(nodeListToWeightList(externals), [8] * 6 + [255, 1024]) frequencies, externals, internals = GroupExternals( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [16] * 4) self.assertEqual(nodeListToWeightList(externals), [255, 1024]) frequencies, externals, internals = DockInternals( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [64]) self.assertEqual(nodeListToWeightList(externals), [255, 1024]) frequencies, externals, internals = MixInternalWithExternal( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [319]) self.assertEqual(nodeListToWeightList(externals), [1024]) frequencies, externals, internals = GroupExternals( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [319]) self.assertEqual(nodeListToWeightList(externals), [1024]) frequencies, externals, internals = DockInternals( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [319]) self.assertEqual(nodeListToWeightList(externals), [1024]) frequencies, externals, internals = MixInternalWithExternal( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [1343]) self.assertEqual(nodeListToWeightList(externals), [])
def test_TREE7(self): """Alpha Equal One. Three Equal Weights.""" W = PartiallySortedArray([10] * 3) T = gdmCodeTree(W) self.assertEqual(str(T), "(30,[10],(20,[select(0)],[select(1)]))") L = T.depths() self.assertEqual(L, [1, 2, 2])
def test_TREE5(self): """Alpha Equal One. Sixteen Equal Weights.""" W = PartiallySortedArray([10] * 16) T = gdmCodeTree(W) self.assertEqual(T.weight(), W.rangeSum(0, len(W))) L = T.depths() self.assertEqual(L, [4] * 16)
def test_TREE3(self): """Alpha Equal One. Two Weights.""" W = PartiallySortedArray([10, 10]) T = gdmCodeTree(W) self.assertEqual(str(T), "(20,[select(0)],[select(1)])") L = T.depths() self.assertEqual(L, [1] * 2)
def test_TREE10(self): """Exponential Sequence.""" W = PartiallySortedArray([1, 2, 4]) T = dvlCodeTree(W) self.assertEqual(str(T), '(7,(3,[select(0)],[select(1)]),[4])') L = T.depths() self.assertEqual(sorted(L), [1, 2, 2])
def test_TREE8(self): """Alpha Equal One. Three Similar Weights.""" W = PartiallySortedArray([12, 11, 10]) T = gdmCodeTree(W) self.assertEqual(str(T), "(33,[12],(21,[select(0)],[select(1)]))") L = T.depths() self.assertEqual(L, [1, 2, 2])
def test_TREE10(self): """Exponential Sequence.""" W = PartiallySortedArray([1, 2, 4]) T = gdmCodeTree(W) self.assertEqual(T.toStringWithAllWeightsCalculated(), '(7,(3,[1],[2]),[4])') L = T.depths() self.assertEqual(sorted(L), [1, 2, 2])
def test_INIT1(self): """Alpha Equal One. Two Weights.""" frequencies = PartiallySortedArray([10, 10]) frequencies, externals, internals = INITIALIZE(frequencies) self.assertEqual(nodeListToString(internals), '[(rangeSum(0,2),[select(0)],[select(1)])]') self.assertEqual(nodeListToWeightList(internals), [20]) self.assertEqual(nodeListToWeightList(externals), [])
def test_TREE4(self): """Alpha Equal One. Four Equal Weights.""" W = PartiallySortedArray([10] * 4) T = gdmCodeTree(W) self.assertEqual(T.toStringWithAllWeightsCalculated(), '(40,(20,[10],[10]),(20,[10],[10]))') L = T.depths() self.assertEqual(L, [2] * 4)
def test_INIT3(self): """Exponential Sequence.""" frequencies = PartiallySortedArray( [10, 20, 40, 80, 160, 320, 640, 1280, 2560]) frequencies, externals, internals = INITIALIZE(frequencies) self.assertEqual(nodeListToString(internals), '[(rangeSum(0,2),[select(0)],[select(1)])]') self.assertEqual(nodeListToWeightList(internals), [30]) self.assertEqual(nodeListToWeightList(externals), [40, 80, 160, 320, 640, 1280, 2560])
def test_TREE12(self): """Exponential Sequence.""" W = PartiallySortedArray([1, 2, 4, 8, 16, 32, 64, 128, 256]) T = gdmCodeTree(W) self.assertEqual( T.toStringWithAllWeightsCalculated(), '(511,(255,(127,(63,(31,(15,(7,(3,[1],[2]),[4]),[8]),[16]),[32]),[64]),[128]),[256])' ) L = T.depths() self.assertEqual(sorted(L), [1, 2, 3, 4, 5, 6, 7, 8, 8])
def test_DockInternals1(self): """Number of nodes to dock equal to a power of three.""" frequencies = PartiallySortedArray([8] * 4 + [32]) frequencies, externals, internals = INITIALIZE(frequencies) frequencies, externals, internals = GroupExternals( frequencies, externals, internals) frequencies, externals, internals = DockInternals( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [32]) self.assertEqual(nodeListToWeightList(externals), [32])
def test_DockInternals2(self): """Number of nodes to dock is a power of two minus one.""" frequencies = PartiallySortedArray([14, 13, 12, 11, 10, 9, 8, 256]) frequencies, externals, internals = INITIALIZE(frequencies) frequencies, externals, internals = GroupExternals( frequencies, externals, internals) frequencies, externals, internals = DockInternals( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [77]) self.assertEqual(nodeListToWeightList(externals), [256])
def test_TREE4(self): """Alpha Equal One. Four Equal Weights.""" W = PartiallySortedArray([10] * 4) T = dvlCodeTree(W) self.assertEqual( str(T), '(40,(20,[select(0)],[select(1)]),(rangeSum(2,4),[select(2)],[select(3)]))' ) L = T.depths() self.assertEqual(L, [2] * 4)
def test_GroupExternals1(self): """Basic Example. """ frequencies = PartiallySortedArray([10, 10, 11, 13, 14, 15, 20, 30]) frequencies, externals, internals = INITIALIZE(frequencies) self.assertEqual(nodeListToWeightList(internals), [20]) self.assertEqual(nodeListToWeightList(externals), [11, 13, 14, 15, 20, 30]) frequencies, externals, internals = GroupExternals( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [24, 29, 40]) self.assertEqual(nodeListToWeightList(externals), [30])
def initialize(self, weights, D): self.weights = PartiallySortedArray(weights) self.D = D self.N = len(weights) n0 = (self.N - 2) % (self.D - 1) + 2 # 2 <= n0 <= D self.externals = [ ExternalNode(self.weights, i) for i in range(n0, len(self.weights)) ] # N - n0 external nodes self.internals = [ InternalNode(self.weights, [ExternalNode(self.weights, i) for i in range(0, n0)]) ] #internal with n0 external nodes
def gdm(frequencies): """Given a sorted list of weights, return an array with the code lengths of an optimal prefix free code according to the GDM algorithm. >>> print(gdm([1]*4+[8]*3)) [4, 4, 4, 4, 2, 2, 2] """ # Degenerated cases if len(frequencies) == 0 : return [] elif len(frequencies)==1: return [0] elif len(frequencies)==2: return [1,1] codeTree = gdmCodeTree(PartiallySortedArray(frequencies)) codeLengths = codeTree.depths() return codeLengths
def dvl(frequencies): """Given a sorted list of weights, return an array with the code lengths of an optimal prefix free code according to the Deferred van Leeuwen's algorithm. >>> print(dvl([1,1,1,1])) [2, 2, 2, 2] """ # Degenerated cases if len(frequencies) == 0: return [] elif len(frequencies) == 1: return [0] elif len(frequencies) == 2: return [1, 1] codeTree = dvlCodeTree(PartiallySortedArray(frequencies)) codeLengths = codeTree.depths() return codeLengths
def test_Mix1(self): """All weights equal.""" frequencies = PartiallySortedArray([8] * 8) frequencies, externals, internals = INITIALIZE(frequencies) self.assertEqual(nodeListToWeightList(internals), [16]) self.assertEqual(nodeListToWeightList(externals), [8] * 6) frequencies, externals, internals = GroupExternals( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [16] * 4) self.assertEqual(nodeListToWeightList(externals), []) frequencies, externals, internals = DockInternals( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [16] * 4) self.assertEqual(nodeListToWeightList(externals), []) frequencies, externals, internals = MixInternalWithExternal( frequencies, externals, internals) self.assertEqual(nodeListToWeightList(internals), [16] * 4) self.assertEqual(nodeListToWeightList(externals), [])
def test_TREE1(self): """Empty input.""" frequencies = PartiallySortedArray([]) self.assertEqual(dvlCodeTree(frequencies), None)
def test_TREE6(self): """Alpha Equal One. Eight Similar Weights.""" W = PartiallySortedArray([10, 11, 12, 13, 14, 15, 16, 17]) T = gdmCodeTree(W) L = T.depths() self.assertEqual(L, [3] * 8)
def test_TREE2(self): """Alpha Equal One. Singleton input.""" frequencies = PartiallySortedArray([10]) self.assertEqual(dvlCodeTree(frequencies), ExternalNode(frequencies, 0))
def test_TREE9(self): """Alpha Equal Two. Single very small weight.""" W = PartiallySortedArray([1] + [8] * 3) T = gdmCodeTree(W) L = T.depths() self.assertEqual(L, [2] * 4)