From 3d5a87a5879aa724e47546d1bdbb7f6c9466cf94 Mon Sep 17 00:00:00 2001
From: pacien
Date: Fri, 30 Nov 2018 21:02:29 +0100
Subject: move integers utils
---
src/bitio/bitreader.nim | 2 +-
src/bitio/bitwriter.nim | 2 +-
src/bitio/integers.nim | 40 +++++++++++++++++++++++++++++++++++++++
src/huffman/huffmanencoder.nim | 2 +-
src/huffman/huffmantree.nim | 2 +-
src/integers.nim | 40 ---------------------------------------
src/lzss/lzsschain.nim | 2 +-
src/rawblock.nim | 2 +-
src/streamblock.nim | 2 +-
tests/tbitio.nim | 28 +++++++++++++++++++++++++--
tests/tintegers.nim | 43 ------------------------------------------
11 files changed, 73 insertions(+), 92 deletions(-)
create mode 100644 src/bitio/integers.nim
delete mode 100644 src/integers.nim
delete mode 100644 tests/tintegers.nim
diff --git a/src/bitio/bitreader.nim b/src/bitio/bitreader.nim
index e4ad225..baa8bf8 100644
--- a/src/bitio/bitreader.nim
+++ b/src/bitio/bitreader.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import streams
-import ../integers
+import integers
type BitReader* = ref object
stream: Stream
diff --git a/src/bitio/bitwriter.nim b/src/bitio/bitwriter.nim
index f1b44ca..4fe499a 100644
--- a/src/bitio/bitwriter.nim
+++ b/src/bitio/bitwriter.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import streams
-import ../integers
+import integers
type BitWriter* = ref object
stream: Stream
diff --git a/src/bitio/integers.nim b/src/bitio/integers.nim
new file mode 100644
index 0000000..c93c9b8
--- /dev/null
+++ b/src/bitio/integers.nim
@@ -0,0 +1,40 @@
+# gzip-like LZSS compressor
+# Copyright (C) 2018 Pacien TRAN-GIRARD
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see .
+
+const wordBitLength* = 8
+
+proc `/^`*[T: Natural](x, y: T): T =
+ (x + y - 1) div y
+
+proc truncateToUint8*(x: SomeUnsignedInt): uint8 =
+ (x and uint8.high).uint8
+
+proc bitLength*[T: SomeUnsignedInt](x: T): int =
+ var buf = x
+ while buf > 0.T:
+ buf = buf shr 1
+ result += 1
+
+proc leastSignificantBits*[T: SomeUnsignedInt](x: T, bits: int): T =
+ let maskOffset = sizeof(T) * wordBitLength - bits
+ if maskOffset >= 0: (x shl maskOffset) shr maskOffset else: x
+
+iterator chunks*(totalBitLength: int, chunkType: typedesc[SomeInteger]): tuple[index: int, chunkBitLength: int] =
+ let chunkBitLength = sizeof(chunkType) * wordBitLength
+ let wordCount = totalBitLength div chunkBitLength
+ for i in 0..<(wordCount): yield (i, chunkBitLength)
+ let remainder = totalBitLength mod chunkBitLength
+ if remainder > 0: yield (wordCount, remainder)
diff --git a/src/huffman/huffmanencoder.nim b/src/huffman/huffmanencoder.nim
index 3ed41ec..5ae1a68 100644
--- a/src/huffman/huffmanencoder.nim
+++ b/src/huffman/huffmanencoder.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import tables
-import ../integers, ../bitio/bitwriter
+import ../bitio/integers, ../bitio/bitwriter
import huffmantree
type HuffmanEncoder*[T, U: SomeUnsignedInt] = object
diff --git a/src/huffman/huffmantree.nim b/src/huffman/huffmantree.nim
index 1140694..58a840e 100644
--- a/src/huffman/huffmantree.nim
+++ b/src/huffman/huffmantree.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import tables, heapqueue
-import ../integers, ../bitio/bitreader, ../bitio/bitwriter
+import ../bitio/integers, ../bitio/bitreader, ../bitio/bitwriter
const valueLengthFieldBitLength* = 6 # 64
diff --git a/src/integers.nim b/src/integers.nim
deleted file mode 100644
index c93c9b8..0000000
--- a/src/integers.nim
+++ /dev/null
@@ -1,40 +0,0 @@
-# gzip-like LZSS compressor
-# Copyright (C) 2018 Pacien TRAN-GIRARD
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU Affero General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program. If not, see .
-
-const wordBitLength* = 8
-
-proc `/^`*[T: Natural](x, y: T): T =
- (x + y - 1) div y
-
-proc truncateToUint8*(x: SomeUnsignedInt): uint8 =
- (x and uint8.high).uint8
-
-proc bitLength*[T: SomeUnsignedInt](x: T): int =
- var buf = x
- while buf > 0.T:
- buf = buf shr 1
- result += 1
-
-proc leastSignificantBits*[T: SomeUnsignedInt](x: T, bits: int): T =
- let maskOffset = sizeof(T) * wordBitLength - bits
- if maskOffset >= 0: (x shl maskOffset) shr maskOffset else: x
-
-iterator chunks*(totalBitLength: int, chunkType: typedesc[SomeInteger]): tuple[index: int, chunkBitLength: int] =
- let chunkBitLength = sizeof(chunkType) * wordBitLength
- let wordCount = totalBitLength div chunkBitLength
- for i in 0..<(wordCount): yield (i, chunkBitLength)
- let remainder = totalBitLength mod chunkBitLength
- if remainder > 0: yield (wordCount, remainder)
diff --git a/src/lzss/lzsschain.nim b/src/lzss/lzsschain.nim
index 2ecff9e..8b49914 100644
--- a/src/lzss/lzsschain.nim
+++ b/src/lzss/lzsschain.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import lists, tables, sugar
-import ../integers, ../huffman/huffmantree
+import ../bitio/integers, ../huffman/huffmantree
import listpolyfill, lzssnode
const maxChainByteLength = 32_000 * wordBitLength
diff --git a/src/rawblock.nim b/src/rawblock.nim
index b9a1e63..b4920fc 100644
--- a/src/rawblock.nim
+++ b/src/rawblock.nim
@@ -14,7 +14,7 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
-import integers, bitio/bitreader, bitio/bitwriter
+import bitio/integers, bitio/bitreader, bitio/bitwriter
const maxDataBitLength = high(uint16).int
const bitLengthFieldBitLength = 2 * wordBitLength
diff --git a/src/streamblock.nim b/src/streamblock.nim
index ff649b1..489097e 100644
--- a/src/streamblock.nim
+++ b/src/streamblock.nim
@@ -15,7 +15,7 @@
# along with this program. If not, see .
import sequtils
-import integers, bitio/bitreader, bitio/bitwriter
+import bitio/integers, bitio/bitreader, bitio/bitwriter
import rawblock, lzssblock
type BlockKind* = enum
diff --git a/tests/tbitio.nim b/tests/tbitio.nim
index 0391974..91b9cbf 100644
--- a/tests/tbitio.nim
+++ b/tests/tbitio.nim
@@ -15,8 +15,32 @@
# along with this program. If not, see .
import unittest, streams, sugar, sequtils
-import integers
-import bitio/bitreader, bitio/bitwriter
+import bitio/integers, bitio/bitreader, bitio/bitwriter
+
+suite "integers":
+ test "Round-up integer division":
+ check 42 /^ 2 == 21
+ check 43 /^ 2 == 22
+
+ test "truncateToUint8":
+ check truncateToUint8(0xFA'u8) == 0xFA'u8
+ check truncateToUint8(0x00FA'u16) == 0xFA'u8
+ check truncateToUint8(0xFFFA'u16) == 0xFA'u8
+
+ test "bitLength":
+ check bitLength(0b1_1111) == 5
+ check bitLength(0b1000_0000) == 8
+
+ test "leastSignificantBits":
+ check leastSignificantBits(0xFF'u8, 3) == 0b0000_0111'u8
+ check leastSignificantBits(0b0001_0101'u8, 3) == 0b0000_0101'u8
+ check leastSignificantBits(0xFF'u8, 10) == 0xFF'u8
+ check leastSignificantBits(0xFFFF'u16, 16) == 0xFFFF'u16
+ check leastSignificantBits(0xFFFF'u16, 8) == 0x00FF'u16
+
+ test "chunks iterator":
+ check toSeq(chunks(70, uint32)) == @[(0, 32), (1, 32), (2, 6)]
+ check toSeq(chunks(32, uint16)) == @[(0, 16), (1, 16)]
suite "bitreader":
test "readBool":
diff --git a/tests/tintegers.nim b/tests/tintegers.nim
deleted file mode 100644
index 851e926..0000000
--- a/tests/tintegers.nim
+++ /dev/null
@@ -1,43 +0,0 @@
-# gzip-like LZSS compressor
-# Copyright (C) 2018 Pacien TRAN-GIRARD
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as
-# published by the Free Software Foundation, either version 3 of the
-# License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU Affero General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program. If not, see .
-
-import unittest, sequtils
-import integers
-
-suite "integers":
- test "Round-up integer division":
- check 42 /^ 2 == 21
- check 43 /^ 2 == 22
-
- test "truncateToUint8":
- check truncateToUint8(0xFA'u8) == 0xFA'u8
- check truncateToUint8(0x00FA'u16) == 0xFA'u8
- check truncateToUint8(0xFFFA'u16) == 0xFA'u8
-
- test "bitLength":
- check bitLength(0b1_1111) == 5
- check bitLength(0b1000_0000) == 8
-
- test "leastSignificantBits":
- check leastSignificantBits(0xFF'u8, 3) == 0b0000_0111'u8
- check leastSignificantBits(0b0001_0101'u8, 3) == 0b0000_0101'u8
- check leastSignificantBits(0xFF'u8, 10) == 0xFF'u8
- check leastSignificantBits(0xFFFF'u16, 16) == 0xFFFF'u16
- check leastSignificantBits(0xFFFF'u16, 8) == 0x00FF'u16
-
- test "chunks iterator":
- check toSeq(chunks(70, uint32)) == @[(0, 32), (1, 32), (2, 6)]
- check toSeq(chunks(32, uint16)) == @[(0, 16), (1, 16)]
--
cgit v1.2.3