Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E2C681876B for ; Mon, 28 Dec 2015 17:31:49 +0000 (UTC) Received: (qmail 28291 invoked by uid 500); 28 Dec 2015 17:31:49 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 28192 invoked by uid 500); 28 Dec 2015 17:31:49 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 28181 invoked by uid 99); 28 Dec 2015 17:31:49 -0000 Received: from arcas.apache.org (HELO arcas) (140.211.11.28) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Dec 2015 17:31:49 +0000 Received: from arcas.apache.org (localhost [127.0.0.1]) by arcas (Postfix) with ESMTP id 8AD7B2C14F9 for ; Mon, 28 Dec 2015 17:31:49 +0000 (UTC) Date: Mon, 28 Dec 2015 17:31:49 +0000 (UTC) From: "Gilles (JIRA)" To: issues@commons.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Resolved] (MATH-1306) Add public void nextBytes(byte[] bytes, int position, int length) method into the RandomGenerator interface MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/MATH-1306?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Gilles resolved MATH-1306. -------------------------- Resolution: Fixed Fix Version/s: 4.0 Method introduced in master branch as part of the commit 4cbb388ba9099be121f81d75000acc3af93bf993 (MATH-1307). > Add public void nextBytes(byte[] bytes, int position, int length) method into the RandomGenerator interface > ----------------------------------------------------------------------------------------------------------- > > Key: MATH-1306 > URL: https://issues.apache.org/jira/browse/MATH-1306 > Project: Commons Math > Issue Type: Improvement > Affects Versions: 3.5 > Reporter: Rostislav Krasny > Fix For: 4.0 > > Attachments: MersenneTwister1305.java, MersenneTwister1306.java > > > I propose to add {{public void nextBytes(byte[] bytes, int position, int length)}} method into the {{RandomGenerator}} interface. > Rationality: to improve performance and memory usage in cases when one needs to fill only a specified region of a byte array. Today to do that you need to use a temporary byte array and copy it by yourself into the specified region of the destination byte array. > I propose the following code, based on the code of {{BitsStreamGenerator}} commited in MATH-1305 > {code:java} > @Override > public void nextBytes(byte[] bytes) { > nextBytesFill(bytes, 0, bytes.length); > } > // TODO add this method into RandomGenerator interface > //@Override > public void nextBytes(byte[] bytes, int position, int length) { > if (position < 0 || position > bytes.length - 1) { > throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, position, 0, bytes.length - 1); > } > if (length < 0 || length > bytes.length - position) { > throw new OutOfRangeException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, length, 0, bytes.length - position); > } > nextBytesFill(bytes, position, length); > } > private void nextBytesFill(byte[] bytes, int position, int length) { > int index = position; > // Position plus multiple 4 part of length (i.e. length with two least significant bits unset). > final int indexLoopLimit = position + (length & 0x7ffffffc); > // Start filling in the byte array, 4 bytes at a time. > while (index < indexLoopLimit) { > final int random = next(32); > bytes[index++] = (byte) random; > bytes[index++] = (byte) (random >>> 8); > bytes[index++] = (byte) (random >>> 16); > bytes[index++] = (byte) (random >>> 24); > } > final int indexLimit = position + length; > > // Fill in the remaining bytes. > if (index < indexLimit) { > int random = next(32); > while (true) { > bytes[index++] = (byte) random; > if (index < indexLimit) { > random >>>= 8; > } else { > break; > } > } > } > } > {code} -- This message was sent by Atlassian JIRA (v6.3.4#6332)