Ejemplo n.º 1
0
func testWritePostClose(obs kanzi.OutputBitStream) {
	defer func() {
		if r := recover(); r != nil {
			fmt.Printf("Error: %v\n", r.(error).Error())
		}
	}()

	fmt.Printf("\nTrying to write to closed stream\n")
	obs.WriteBit(1)
}
Ejemplo n.º 2
0
// alphabet must be sorted in increasing order
func EncodeAlphabet(obs kanzi.OutputBitStream, alphabet []byte) int {
	alphabetSize := len(alphabet)

	// First, push alphabet encoding mode
	if alphabetSize == 256 {
		// Full alphabet
		obs.WriteBit(FULL_ALPHABET)
		obs.WriteBit(EIGHT_BIT_ALPHABET)
		return 256
	}

	if alphabetSize == 128 {
		flag := true

		for i := byte(0); i < 128; i++ {
			if alphabet[i] != i {
				flag = false
				break
			}
		}

		if flag == true {
			obs.WriteBit(FULL_ALPHABET)
			obs.WriteBit(SEVEN_BIT_ALPHABET)
			return 128
		}
	}

	obs.WriteBit(PARTIAL_ALPHABET)

	diffs := make([]int, 32)
	maxSymbolDiff := 1

	if (alphabetSize != 0) && ((alphabetSize < 32) || (alphabetSize > 224)) {
		obs.WriteBit(DELTA_ENCODED_ALPHABET)

		if alphabetSize >= 224 {
			// Big alphabet, encode all missing symbols
			alphabetSize = 256 - alphabetSize
			obs.WriteBits(uint64(alphabetSize), 5)
			obs.WriteBit(ABSENT_SYMBOLS_MASK)
			symbol := uint8(0)
			previous := uint8(0)

			for i, n := 0, 0; n < alphabetSize; {
				if symbol == alphabet[i] {
					if i < len(alphabet)-1 {
						i++
					}

					symbol++
					continue
				}

				diffs[n] = int(symbol) - int(previous)
				symbol++
				previous = symbol

				if diffs[n] > maxSymbolDiff {
					maxSymbolDiff = diffs[n]
				}

				n++
			}
		} else {
			// Small alphabet, encode all present symbols
			obs.WriteBits(uint64(alphabetSize), 5)
			obs.WriteBit(PRESENT_SYMBOLS_MASK)
			previous := uint8(0)

			for i := 0; i < alphabetSize; i++ {
				diffs[i] = int(alphabet[i]) - int(previous)
				previous = alphabet[i] + 1

				if diffs[i] > maxSymbolDiff {
					maxSymbolDiff = diffs[i]
				}
			}
		}

		// Write log(max(diff)) to bitstream
		log := uint(1)

		for 1<<log <= maxSymbolDiff {
			log++
		}

		obs.WriteBits(uint64(log-1), 3) // delta size

		// Write all symbols with delta encoding
		for i := 0; i < alphabetSize; i++ {
			obs.WriteBits(uint64(diffs[i]), log)
		}
	} else {
		// Regular (or empty) alphabet
		obs.WriteBit(BIT_ENCODED_ALPHABET)
		masks := make([]uint64, 4)

		for i := 0; i < alphabetSize; i++ {
			masks[alphabet[i]>>6] |= (1 << uint(alphabet[i]&63))
		}

		for i := range masks {
			obs.WriteBits(masks[i], 64)
		}
	}

	return alphabetSize
}