git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [JGIT PATCH 1/6] Add set to IntList

From
Robin Rosenberg <robin.rosenberg.lists@dewire.com>
Date
May 3, 2009, 07:07 UTC
Message-ID
<200905030907.02287.robin.rosenberg.lists@dewire.com>
In-Reply-To
<1241230127-28279-2-git-send-email-spearce@spearce.org>
lördag 02 maj 2009 04:08:42 skrev "Shawn O. Pearce" <spearce@spearce.org>:
Show 33 quoted lines
> Some applications may wish to modify an int list.
> 
> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
> ---
>  .../tst/org/spearce/jgit/util/IntListTest.java     |   21 ++++++++++++++++++++
>  .../src/org/spearce/jgit/util/IntList.java         |   17 ++++++++++++++++
>  2 files changed, 38 insertions(+), 0 deletions(-)
> 
> diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/util/IntListTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/util/IntListTest.java
> index c470d55..ce0d7af 100644
> --- a/org.spearce.jgit.test/tst/org/spearce/jgit/util/IntListTest.java
> +++ b/org.spearce.jgit.test/tst/org/spearce/jgit/util/IntListTest.java
> @@ -144,6 +144,27 @@ public void testClear() {
>  		}
>  	}
>  
> +	public void testSet() {
> +		final IntList i = new IntList();
> +		i.add(1);
> +		assertEquals(1, i.size());
> +		assertEquals(1, i.get(0));
> +		
> +		i.set(0, 5);
> +		assertEquals(5, i.get(0));
> +		
> +		try {
> +			i.set(5, 5);
> +			fail("accepted set of 5 beyond end of list");
> +		} catch (ArrayIndexOutOfBoundsException e){
> +			assertTrue(true);
> +		}
> +
> +		i.set(1, 2);
Oh, you grow the array here. Not obvious.
Show 27 quoted lines
> +		assertEquals(2, i.size());
> +		assertEquals(2, i.get(1));
> +	}
> +
>  	public void testToString() {
>  		final IntList i = new IntList();
>  		i.add(1);
> diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/IntList.java b/org.spearce.jgit/src/org/spearce/jgit/util/IntList.java
> index 0a84793..9d86a5c 100644
> --- a/org.spearce.jgit/src/org/spearce/jgit/util/IntList.java
> +++ b/org.spearce.jgit/src/org/spearce/jgit/util/IntList.java
> @@ -94,6 +94,23 @@ public void add(final int n) {
>  	}
>  
>  	/**
> +	 * Assign an entry in the list.
> +	 * 
> +	 * @param index
> +	 *            index to set, must be in the range [0, {@link #size()}).
> +	 * @param n
> +	 *            value to store at the position.
> +	 */
> +	public void set(final int index, final int n) {
> +		if (count < index)
> +			throw new ArrayIndexOutOfBoundsException(index);
> +		else if (count == index)
> +			add(n);

The interface is quite obscure here. One the one hand it checks for assignment outside the set but it does grow the array when only one entry is missing. The reader of the code won't see that easily. A different name and reflection of the behaviour in the javadoc is needed. The class javadoc says "A more efficient List<Integer> using a primitive integer array." and I know of no java.util.List's that expand implicitly.

-- robin
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 10 of 15 in “Diff processing utilities”
  1. 0/6 Diff processing utilitiesShawn O. Pearce, May 2, 2009
  2. 1/6 Add set to IntListShawn O. Pearce, May 2, 2009
  3. 2/6 Add diff.Edit to describe an edit region within a fileShawn O. Pearce, May 2, 2009
  4. 3/6 Add diff.EditList to provide for a list of Edit instancesShawn O. Pearce, May 2, 2009
  5. 4/6 Add diff.RawText to index a file content for later comparesShawn O. Pearce, May 2, 2009
  6. 5/6 Teach FileHeader, HunkHeader how to create an EditListShawn O. Pearce, May 2, 2009
  7. 6/6 Add diff.DiffFormatter to create Git style unified patch scriptsShawn O. Pearce, May 2, 2009
  8. 6/6 Add diff.DiffFormatter to create Git style unified patch scriptsShawn O. Pearce, May 3, 2009
  9. 6/6 Add diff.DiffFormatter to create Git style unified patch scriptsShawn O. Pearce, May 3, 2009
  10. Robin RosenbergMay 3, 2009
  11. Shawn O. PearceMay 4, 2009
  12. Johannes SchindelinMay 4, 2009
  13. Shawn O. PearceMay 4, 2009
  14. Johannes SchindelinMay 4, 2009
  15. Johannes SchindelinMay 3, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.