Skip to content

Commit

Permalink
support negative index
Browse files Browse the repository at this point in the history
  • Loading branch information
kemsky committed Dec 17, 2015
1 parent cd771ff commit 123e4c1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 35 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ while (values.hasNext)
}
//set item at index 0
s.setItem(0, 5);
s.setItem(5, 0);
s[0] = 5;
s.first = 5;
//last item also has index -1, item before last -2 and etc. (Ruby-like)
Expand All @@ -84,7 +84,7 @@ delete s[0];
s.removeItem(0);
```
*Stream is about 10x slower when accessed by index (`[index]`) and it seems to be Proxy overhead.
If you need better performance (3x slower than Array) use methods to access Stream items: `getItem(index)` and `setItem(index, value)`.*
If you need better performance (3x slower than Array) use methods to access Stream items: `getItem(index)` and `setItem(value, index)`.*

## Convert Stream to any collection
```as3
Expand Down
72 changes: 46 additions & 26 deletions source/com/kemsky/Stream.as
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,14 @@ package com.kemsky
*/
public function getItem(index:int):*
{
return source[index];
if(index >= 0)
{
return source[index];
}
else
{
return source[source.length + index];
}
}

/**
Expand All @@ -455,45 +462,65 @@ package com.kemsky
* @example
* <pre>
* var s:Stream = $(1, 2, 3);
* s.setItem(1, 4);
* s.setItem(4, 1);
* trace(s);
* //Stream{1, 4, 3}
* </pre>
* @internal mutable
*/
public function setItem(index:int, value:*):void
public function setItem(value:*, index:int):void
{
source[index] = value;
if(index >= 0)
{
source[index] = value;
}
else
{
source[source.length + index] = value;
}
}

/**
* Adds item to specified position.
* @param index An integer that specifies the position in the list where the item is to be added to.
* @param value An item to add.
* @param value An item to add. Item will be added to the end of stream by default.
* @example
* <pre>
* var s:Stream = $(1, 2, 3);
* s.addItem(1, 4);
* s.addItem(4, 1);
* trace(s);
* //Stream{1, 4, 2, 3}
* </pre>
* @internal mutable
*/
public function addItem(index:int, value:*):void
public function addItem(value:*, index:int = 0):void
{
if(index <= source.length)
if(arguments.length == 1)
{
index = source.length;
}

if(index < 0)
{
index = source.length + index;
}

if(index < source.length)
{
//insert
source.splice(index, 0, value);
}
else
{
//push
source[index] = value;
}
}

/**
* Removes item at specified position.
* @param index An integer that specifies the position in the list.
* @return true if remove was successful.
* @example
* <pre>
* var s:Stream = $(1, 2, 3);
Expand All @@ -503,9 +530,16 @@ package com.kemsky
* </pre>
* @internal mutable
*/
public function removeItem(index:int):void
public function removeItem(index:int):Boolean
{
delete this[index];
if(index < 0)
{
return (source.splice(source.length + index, 1) as Array).length > 0;
}
else
{
return (source.splice(index, 1) as Array).length > 0;
}
}

/**
Expand Down Expand Up @@ -2007,14 +2041,7 @@ package com.kemsky
}
else
{
if(index < 0)
{
source[index] = value;
}
else
{
source[source.length + index] = value;
}
setItem(value, index);
}
}

Expand Down Expand Up @@ -2109,14 +2136,7 @@ package com.kemsky
return false;
}

if(index < 0)
{
return (source.splice(source.length + index, 1) as Array).length > 0;
}
else
{
return (source.splice(index, 1) as Array).length > 0;
}
return removeItem(index);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion source/com/kemsky/support/ValueIterator.as
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ package com.kemsky.support
{
throw new StreamError("Current item is not available");
}
stream.setItem(_current, value);
stream.setItem(value, _current);
}

/**
Expand Down
16 changes: 10 additions & 6 deletions testSrc/com/kemsky/TestStream.as
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ package com.kemsky
public function testSet():void
{
var s:Stream = $(1, 2, 3);
s.setItem(2, 1);
s.setItem(0, 3);
s.setItem(1, 2);
s.setItem(3, 0);
assertEquals(s[0], 3);
assertEquals(s[1], 2);
assertEquals(s[2], 1);
Expand All @@ -397,8 +397,8 @@ package com.kemsky
public function testAdd():void
{
var s:Stream = $(1, 2, 3);
s.addItem(2, 1);
s.addItem(0, 3);
s.addItem(1, 2);
s.addItem(3, 0);
//3, 1, 2, 1, 3
assertEquals(s.length, 5);
assertEquals(s[0], 3);
Expand All @@ -413,9 +413,13 @@ package com.kemsky
assertEquals(d.last, 4);

var e:Stream = $();
e.addItem(0, 4);
e.addItem(4, 0);
assertEquals(e.length, 1);
assertEquals(e.last, 4);
assertEquals(e.first, 4);

e.addItem(4);
assertEquals(e.length, 2);
assertEquals(e.second, 4);
}

[Test]
Expand Down

0 comments on commit 123e4c1

Please sign in to comment.