[primitives] I would like to contribute two methods to the array lists

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[primitives] I would like to contribute two methods to the array lists

Michael Mirwaldt
Hi.
I am using your library for one of my projects.

Given the ByteArrayList, the following two methods can be very handy:

public byte[] removeElementsFromTo(int fromIndex,int toIndex,boolean returnArray) {
     if (fromIndex <0 ||this._size < fromIndex) {
         throw new IndexOutOfBoundsException("From-index should be at least 0 and less than " +this._size +", found " + fromIndex);
     }
     if (toIndex <1 ||this._size <= toIndex) {
         throw new IndexOutOfBoundsException("To-index should be at least 1 and less than or equal to " +this._size +", found " + toIndex);
     }
     if (toIndex <= fromIndex) {
         throw new IndexOutOfBoundsException("From-index must lie before to-index." +"from-index=" + fromIndex +", to-index=" + toIndex);
     }

     int length = toIndex - fromIndex;
     byte[] removedBytes =null;
     
     this.incrModCount();
     if (0 < length) {
         if (returnArray) {
             removedBytes =new byte[length];
             System.arraycopy(this._data, fromIndex, removedBytes,0, length);
         }

         System.arraycopy(this._data, fromIndex + length,this._data, fromIndex, length);
         this._size -= length;
     }else {
         if (returnArray) {
             removedBytes =new byte[0];
         }
     }

     return removedBytes;
}

public void addElementsAt(int index,byte[] elements) {
     this.checkRangeIncludingEndpoint(index);
     this.incrModCount();
     this.ensureCapacity(this._size + elements.length);
     int numtomove =this._size - index - elements.length;
     System.arraycopy(this._data, index,this._data, index + elements.length, numtomove);
     System.arraycopy(elements,0,this._data, index, elements.length);
     this._size+=elements.length;
}

They avoid copying arrays for every single byte.

Maybe you want to add them.

Best regards,'
Michael MIrwaldt
Reply | Threaded
Open this post in threaded view
|

Re: [primitives] I would like to contribute two methods to the array lists

garydgregory
Hi Michael,

I'm not sure how alive the primitives component is, but the best way to
contribute new methods like these is to provide a GitHub PR with unit
tests. It is very unlikely that new code like this would be accepted
without unit tests and decent code coverage. But, this component is not
mirrored on GitHub so you'll have to do it the old fashioned way:
- Create a JIRA
- Attach a patch in unified diff format for your new methods and their unit
tests

Thank you!
Gary

On Mon, Feb 19, 2018 at 5:34 PM, Michael Mirwaldt <[hidden email]>
wrote:

> Hi.
> I am using your library for one of my projects.
>
> Given the ByteArrayList, the following two methods can be very handy:
>
> public byte[] removeElementsFromTo(int fromIndex,int toIndex,boolean
> returnArray) {
>     if (fromIndex <0 ||this._size < fromIndex) {
>         throw new IndexOutOfBoundsException("From-index should be at
> least 0 and less than " +this._size +", found " + fromIndex);
>     }
>     if (toIndex <1 ||this._size <= toIndex) {
>         throw new IndexOutOfBoundsException("To-index should be at least
> 1 and less than or equal to " +this._size +", found " + toIndex);
>     }
>     if (toIndex <= fromIndex) {
>         throw new IndexOutOfBoundsException("From-index must lie before
> to-index." +"from-index=" + fromIndex +", to-index=" + toIndex);
>     }
>
>     int length = toIndex - fromIndex;
>     byte[] removedBytes =null;
>         this.incrModCount();
>     if (0 < length) {
>         if (returnArray) {
>             removedBytes =new byte[length];
>             System.arraycopy(this._data, fromIndex, removedBytes,0,
> length);
>         }
>
>         System.arraycopy(this._data, fromIndex + length,this._data,
> fromIndex, length);
>         this._size -= length;
>     }else {
>         if (returnArray) {
>             removedBytes =new byte[0];
>         }
>     }
>
>     return removedBytes;
> }
>
> public void addElementsAt(int index,byte[] elements) {
>     this.checkRangeIncludingEndpoint(index);
>     this.incrModCount();
>     this.ensureCapacity(this._size + elements.length);
>     int numtomove =this._size - index - elements.length;
>     System.arraycopy(this._data, index,this._data, index +
> elements.length, numtomove);
>     System.arraycopy(elements,0,this._data, index, elements.length);
>     this._size+=elements.length;
> }
>
> They avoid copying arrays for every single byte.
>
> Maybe you want to add them.
>
> Best regards,'
> Michael MIrwaldt
>
Reply | Threaded
Open this post in threaded view
|

Re: [primitives] I would like to contribute two methods to the array lists

Pascal Schumacher
In reply to this post by Michael Mirwaldt
Hi Michael,

thanks for your contribution!

Commons-primitives was moved to dormant one and a half year ago (see:
http://mail-archives.apache.org/mod_mbox/commons-user/201607.mbox/%3CCAB917R%2Bsku6f4gJVAicWUgrzAauuuPc4zoAbsuVgbKeWXbhvEA%40mail.gmail.com%3E).
New releases are unlikely. Best not spend any effort on this.

Sorry,
Pascal

Am 20.02.2018 um 01:34 schrieb Michael Mirwaldt:

> Hi.
> I am using your library for one of my projects.
>
> Given the ByteArrayList, the following two methods can be very handy:
>
> public byte[] removeElementsFromTo(int fromIndex,int toIndex,boolean
> returnArray) {
>     if (fromIndex <0 ||this._size < fromIndex) {
>         throw new IndexOutOfBoundsException("From-index should be at
> least 0 and less than " +this._size +", found " + fromIndex);
>     }
>     if (toIndex <1 ||this._size <= toIndex) {
>         throw new IndexOutOfBoundsException("To-index should be at
> least 1 and less than or equal to " +this._size +", found " + toIndex);
>     }
>     if (toIndex <= fromIndex) {
>         throw new IndexOutOfBoundsException("From-index must lie
> before to-index." +"from-index=" + fromIndex +", to-index=" + toIndex);
>     }
>
>     int length = toIndex - fromIndex;
>     byte[] removedBytes =null;
>         this.incrModCount();
>     if (0 < length) {
>         if (returnArray) {
>             removedBytes =new byte[length];
>             System.arraycopy(this._data, fromIndex, removedBytes,0,
> length);
>         }
>
>         System.arraycopy(this._data, fromIndex + length,this._data,
> fromIndex, length);
>         this._size -= length;
>     }else {
>         if (returnArray) {
>             removedBytes =new byte[0];
>         }
>     }
>
>     return removedBytes;
> }
>
> public void addElementsAt(int index,byte[] elements) {
>     this.checkRangeIncludingEndpoint(index);
>     this.incrModCount();
>     this.ensureCapacity(this._size + elements.length);
>     int numtomove =this._size - index - elements.length;
>     System.arraycopy(this._data, index,this._data, index +
> elements.length, numtomove);
>     System.arraycopy(elements,0,this._data, index, elements.length);
>     this._size+=elements.length;
> }
>
> They avoid copying arrays for every single byte.
>
> Maybe you want to add them.
>
> Best regards,'
> Michael MIrwaldt
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]