From 432f1ab09e52c2663ace77e9f373f8260f40385c Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Sat, 11 Mar 2017 00:09:50 +0900 Subject: [PATCH 1/6] add DecimalWriter --- .../doubdabc/io/DecimalWriter.java | 108 ++++++++++++++++++ .../olyutorskii/doubdabc/io/package-info.java | 12 ++ 2 files changed, 120 insertions(+) create mode 100644 src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java create mode 100644 src/main/java/io/github/olyutorskii/doubdabc/io/package-info.java diff --git a/src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java b/src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java new file mode 100644 index 0000000..2ed215e --- /dev/null +++ b/src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java @@ -0,0 +1,108 @@ +/* + * License : The MIT License + * Copyright(c) 2017 olyutorskii + */ + +package io.github.olyutorskii.doubdabc.io; + +import io.github.olyutorskii.doubdabc.BcdRegister; +import io.github.olyutorskii.doubdabc.BitFeeder; +import io.github.olyutorskii.doubdabc.DecimalOut; +import java.io.FilterWriter; +import java.io.IOException; +import java.io.Writer; + +/** + * Decimal-form-integer writer with Double-Dabble algorithm. + * + *

It's similar to {@link java.io.PrintWriter} + * + * @see java.io.PrintWriter + */ +public class DecimalWriter extends FilterWriter{ + + private final BcdRegister bcd; + private final DecimalOut decOut; + + + /** + * Constructor. + * @param out char output stream + */ + public DecimalWriter(Writer out){ + super(out); + + this.bcd = new BcdRegister(BcdRegister.MAX_COL_UINT64); + this.decOut = new DecimalOut(this.bcd); + + return; + } + + + /** + * Print signed int value. + * + *

It's similar to {@link java.io.PrintWriter#print(int)}. + * + * @param iVal int value + * @throws IOException output error + * @see java.io.PrintWriter#print(int) + */ + public void print(int iVal) throws IOException{ + boolean negSign; + int absVal; + + if(iVal < 0){ + negSign = true; + absVal = -iVal; + }else{ + negSign = false; + absVal = iVal; + } + + synchronized(this.bcd){ + BitFeeder.feedUInt32(this.bcd, absVal); + + synchronized(this.lock){ + if(negSign) append('-'); + this.decOut.writeDigits(this); + } + } + + return; + } + + /** + * Print signed long value. + * + *

It's similar to {@link java.io.PrintWriter#print(long)}. + * + * @param lVal long value + * @throws IOException output error + * @see java.io.PrintWriter#print(long) + */ + public void print(long lVal) throws IOException{ + boolean negSign; + long absVal; + + if(lVal < 0){ + negSign = true; + absVal = -lVal; + }else{ + negSign = false; + absVal = lVal; + } + + synchronized(this.bcd){ + BitFeeder.feedUInt64(this.bcd, absVal); + + synchronized(this.lock){ + if(negSign) append('-'); + this.decOut.writeDigits(this); + } + } + + return; + } + +} diff --git a/src/main/java/io/github/olyutorskii/doubdabc/io/package-info.java b/src/main/java/io/github/olyutorskii/doubdabc/io/package-info.java new file mode 100644 index 0000000..9b14f93 --- /dev/null +++ b/src/main/java/io/github/olyutorskii/doubdabc/io/package-info.java @@ -0,0 +1,12 @@ +/* + * License : The MIT License + * Copyright(c) 2017 olyutorskii + */ + +/** + * Extended I/O-classes with Double-Dabble algorithm. + */ + +package io.github.olyutorskii.doubdabc.io; + +/* EOF */ From ab84e3ddf2a12b78bfc105bd39b29e415007ca12 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Sun, 12 Mar 2017 03:00:04 +0900 Subject: [PATCH 2/6] add DecimalWriter Unit test --- .../doubdabc/io/DecimalWriterTest.java | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java diff --git a/src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java b/src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java new file mode 100644 index 0000000..f13df4c --- /dev/null +++ b/src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java @@ -0,0 +1,129 @@ +/* + * License : The MIT License + * Copyright(c) 2017 olyutorskii + */ + +package io.github.olyutorskii.doubdabc.io; + +import java.io.CharArrayWriter; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * + */ +public class DecimalWriterTest { + + public DecimalWriterTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of print method, of class DecimalWriter. + */ + @Test + public void testPrint_int() throws Exception { + System.out.println("print"); + + DecimalWriter writer; + CharArrayWriter out; + + out = new CharArrayWriter(); + writer = new DecimalWriter(out); + + out.reset(); + writer.print(1); + assertEquals("1", out.toString()); + + out.reset(); + writer.print(0); + assertEquals("0", out.toString()); + + out.reset(); + writer.print(-1); + assertEquals("-1", out.toString()); + + out.reset(); + writer.print(999); + assertEquals("999", out.toString()); + + out.reset(); + writer.print(-1_234_567_890); + assertEquals("-1234567890", out.toString()); + + out.reset(); + writer.print(Integer.MAX_VALUE); + assertEquals("2147483647", out.toString()); + + out.reset(); + writer.print(Integer.MIN_VALUE); + assertEquals("-2147483648", out.toString()); + + return; + } + + /** + * Test of print method, of class DecimalWriter. + */ + @Test + public void testPrint_long() throws Exception { + System.out.println("print"); + + DecimalWriter writer; + CharArrayWriter out; + + out = new CharArrayWriter(); + writer = new DecimalWriter(out); + + out.reset(); + writer.print(1L); + assertEquals("1", out.toString()); + + out.reset(); + writer.print(0L); + assertEquals("0", out.toString()); + + out.reset(); + writer.print(-1L); + assertEquals("-1", out.toString()); + + out.reset(); + writer.print(999L); + assertEquals("999", out.toString()); + + out.reset(); + writer.print(-1_234_567_890L); + assertEquals("-1234567890", out.toString()); + + out.reset(); + writer.print(Long.MAX_VALUE); + assertEquals("9223372036854775807", out.toString()); + + out.reset(); + writer.print(Long.MIN_VALUE); + assertEquals("-9223372036854775808", out.toString()); + + return; + } + +} From 8ba744a7bc0b1ad2622a02049a5bae5fcb17179e Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Tue, 14 Mar 2017 02:58:36 +0900 Subject: [PATCH 3/6] Modify docs about DecimalWriter. --- CHANGELOG.md | 1 + README.md | 9 +++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e94ef82..73294df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ DoubDabC Changelog ## WIP Released on 20XX-XX-XX +- Add DecimalWriter which supports print(int) #16 ## v1.102.6 Released on 2017-02-19 diff --git a/README.md b/README.md index 58a2d9b..850c691 100644 --- a/README.md +++ b/README.md @@ -47,10 +47,15 @@ as Arabic numeral characters\(0-9\) output. * `CharSequence` wrapper class is provided. +* Extended `Writer` class is provided +which supports `print(int)` & `print(long)` methods +like `PrintWriter` but fast. + ## Limitations ## -* DoubDabC does not support negative values. +* DoubDabC does not support negative values +with the exception of DecimalWriter. Signed-values are treated as Unsigned-value like `Integer.toUnsignedString(int)`. Let's convert absolute value that follows minus\(-\) sign. @@ -72,7 +77,7 @@ Just compile Java sources under `src/main/java/` if you don't use Maven. ## Project founder ## -* By [Olyutorskii](https://github.com/olyutorskii) at 2017 +* By [olyutorskii](https://github.com/olyutorskii) at 2017 ## Key technology ## From 9467fe8aa12ae21d7508041dd379315ea053c040 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Sat, 18 Mar 2017 10:51:28 +0900 Subject: [PATCH 4/6] Add special MIN case comment. --- .../doubdabc/io/DecimalWriter.java | 2 + .../doubdabc/io/DecimalWriterTest.java | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java b/src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java index 2ed215e..6d3190f 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/io/DecimalWriter.java @@ -55,6 +55,7 @@ public void print(int iVal) throws IOException{ if(iVal < 0){ negSign = true; absVal = -iVal; + // -2147483648 does not change. OK, NP. }else{ negSign = false; absVal = iVal; @@ -88,6 +89,7 @@ public void print(long lVal) throws IOException{ if(lVal < 0){ negSign = true; absVal = -lVal; + // -9223372036854775808 does not change. OK, NP. }else{ negSign = false; absVal = lVal; diff --git a/src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java b/src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java index f13df4c..04ccb61 100644 --- a/src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java +++ b/src/test/java/io/github/olyutorskii/doubdabc/io/DecimalWriterTest.java @@ -67,10 +67,22 @@ public void testPrint_int() throws Exception { writer.print(999); assertEquals("999", out.toString()); + out.reset(); + writer.print(-999); + assertEquals("-999", out.toString()); + + out.reset(); + writer.print(1_234_567_890); + assertEquals("1234567890", out.toString()); + out.reset(); writer.print(-1_234_567_890); assertEquals("-1234567890", out.toString()); + out.reset(); + writer.print(Integer.MAX_VALUE - 1); + assertEquals("2147483646", out.toString()); + out.reset(); writer.print(Integer.MAX_VALUE); assertEquals("2147483647", out.toString()); @@ -79,6 +91,10 @@ public void testPrint_int() throws Exception { writer.print(Integer.MIN_VALUE); assertEquals("-2147483648", out.toString()); + out.reset(); + writer.print(Integer.MIN_VALUE + 1); + assertEquals("-2147483647", out.toString()); + return; } @@ -111,10 +127,38 @@ public void testPrint_long() throws Exception { writer.print(999L); assertEquals("999", out.toString()); + out.reset(); + writer.print(-999L); + assertEquals("-999", out.toString()); + + out.reset(); + writer.print(1_234_567_890L); + assertEquals("1234567890", out.toString()); + out.reset(); writer.print(-1_234_567_890L); assertEquals("-1234567890", out.toString()); + out.reset(); + writer.print((long)Integer.MAX_VALUE); + assertEquals("2147483647", out.toString()); + + out.reset(); + writer.print((long)Integer.MAX_VALUE + 1L); + assertEquals("2147483648", out.toString()); + + out.reset(); + writer.print((long)Integer.MIN_VALUE); + assertEquals("-2147483648", out.toString()); + + out.reset(); + writer.print((long)Integer.MIN_VALUE - 1L); + assertEquals("-2147483649", out.toString()); + + out.reset(); + writer.print(Long.MAX_VALUE - 1L); + assertEquals("9223372036854775806", out.toString()); + out.reset(); writer.print(Long.MAX_VALUE); assertEquals("9223372036854775807", out.toString()); @@ -123,6 +167,10 @@ public void testPrint_long() throws Exception { writer.print(Long.MIN_VALUE); assertEquals("-9223372036854775808", out.toString()); + out.reset(); + writer.print(Long.MIN_VALUE + 1L); + assertEquals("-9223372036854775807", out.toString()); + return; } From 0050ad3145df509f999a57ec419eab540dfeeeb7 Mon Sep 17 00:00:00 2001 From: olyutorskii Date: Sat, 18 Mar 2017 13:26:59 +0900 Subject: [PATCH 5/6] Remove overblown terms on perfomance. --- CHANGELOG.md | 1 + README.md | 8 ++++---- .../java/io/github/olyutorskii/doubdabc/package-info.java | 3 +-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73294df..a5ac54e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ DoubDabC Changelog ## WIP Released on 20XX-XX-XX - Add DecimalWriter which supports print(int) #16 +- Remove overblown terms on perfomance #20 ## v1.102.6 Released on 2017-02-19 diff --git a/README.md b/README.md index 850c691..8da394c 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ that supports **binary integer value to decimal sequence conversion**. aka **Shift-and-add-3**. Double-Dabble is a radix-conversion algorithm but there is no division\(/\) nor remainder\(%\) operation. -That means, **Fast !** +It's a bit fast. * There is no String constructor during conversion. That means, **GC-friendry !** @@ -43,19 +43,19 @@ Just losing higher decimals over you specified. * You can assign `Appendable`, `Writer`, `StringBuffer`, `StringBuilder`, or `CharBuffer` -as Arabic numeral characters\(0-9\) output. +as Arabic numeral characters\(0-9\) sequence output. * `CharSequence` wrapper class is provided. * Extended `Writer` class is provided which supports `print(int)` & `print(long)` methods -like `PrintWriter` but fast. +like `PrintWriter`. ## Limitations ## * DoubDabC does not support negative values -with the exception of DecimalWriter. +with the exception of `DecimalWriter`. Signed-values are treated as Unsigned-value like `Integer.toUnsignedString(int)`. Let's convert absolute value that follows minus\(-\) sign. diff --git a/src/main/java/io/github/olyutorskii/doubdabc/package-info.java b/src/main/java/io/github/olyutorskii/doubdabc/package-info.java index edb718c..753921e 100644 --- a/src/main/java/io/github/olyutorskii/doubdabc/package-info.java +++ b/src/main/java/io/github/olyutorskii/doubdabc/package-info.java @@ -12,8 +12,7 @@ * but there is no division(/) nor remainder(%) operation. * *

There is no String constructor during conversion. - * - *

So, DoubDabC is Fast and GC-friendly. + * So, DoubDabC is a GC-friendly. * *

Project Home : * Date: Wed, 22 Mar 2017 22:49:41 +0900 Subject: [PATCH 6/6] Release 1.103.2 preparation --- CHANGELOG.md | 5 ++++- pom.xml | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5ac54e..f3fa285 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,8 +3,11 @@ DoubDabC Changelog ## WIP Released on 20XX-XX-XX + +## v1.103.2 +Released on 2017-03-22 - Add DecimalWriter which supports print(int) #16 -- Remove overblown terms on perfomance #20 +- Remove overblown terms on perfomance #20 ## v1.102.6 Released on 2017-02-19 diff --git a/pom.xml b/pom.xml index 846e5ec..e4801ff 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ io.github.olyutorskii doubdabc - 1.102.7-SNAPSHOT + 1.103.2 jar DoubDabC