Skip to content

Commit ff88089

Browse files
committed
CLJCLR-191: PersistentVector.TransientVector should implement IFn.invoke(object arg1)
1 parent 8c6da2d commit ff88089

File tree

1 file changed

+54
-42
lines changed

1 file changed

+54
-42
lines changed

Clojure/Clojure/Lib/PersistentVector.cs

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414

1515
using System;
1616
using System.Collections;
17-
using System.Threading;
1817
using System.Collections.Generic;
18+
using System.Threading;
1919

2020
namespace clojure.lang
2121
{
2222
/// <summary>
2323
/// Implements a persistent vector using a specialized form of array-mapped hash trie.
2424
/// </summary>
2525
[Serializable]
26-
public class PersistentVector: APersistentVector, IObj, IEditableCollection, IEnumerable, IReduce, IKVReduce, IDrop
26+
public class PersistentVector : APersistentVector, IObj, IEditableCollection, IEnumerable, IReduce, IKVReduce, IDrop
2727
{
2828
#region Node class
2929

@@ -38,16 +38,16 @@ public sealed class Node
3838
public AtomicReference<Thread> Edit
3939
{
4040
get { return _edit; }
41-
}
41+
}
4242

4343
readonly object[] _array;
4444

4545
public object[] Array
4646
{
4747
get { return _array; }
48-
}
48+
}
49+
4950

50-
5151
#endregion
5252

5353
#region C-tors
@@ -63,16 +63,16 @@ public Node(AtomicReference<Thread> edit)
6363
_edit = edit;
6464
_array = new object[32];
6565
}
66-
66+
6767
#endregion
6868
}
6969

7070
#endregion
7171

7272
#region Data
7373

74-
static readonly AtomicReference<Thread> NoEdit = new AtomicReference<Thread>();
75-
internal static readonly Node EmptyNode = new Node(NoEdit, new object[32]);
74+
static readonly AtomicReference<Thread> NoEdit = new();
75+
internal static readonly Node EmptyNode = new(NoEdit, new object[32]);
7676

7777
readonly int _cnt;
7878
readonly int _shift;
@@ -81,14 +81,14 @@ public Node(AtomicReference<Thread> edit)
8181

8282
public int Shift { get { return _shift; } }
8383
public Node Root { get { return _root; } }
84-
public object[] Tail() { return _tail; }
84+
public object[] Tail() { return _tail; }
8585

8686
readonly IPersistentMap _meta;
8787

8888
/// <summary>
8989
/// An empty <see cref="PersistentVector">PersistentVector</see>.
9090
/// </summary>
91-
static public readonly PersistentVector EMPTY = new PersistentVector(0,5,EmptyNode, new object[0]);
91+
static public readonly PersistentVector EMPTY = new(0, 5, EmptyNode, new object[0]);
9292

9393
#endregion
9494

@@ -148,7 +148,7 @@ static public PersistentVector create(ISeq items)
148148
if (items != null)
149149
{
150150
// >32, construct with array directly
151-
PersistentVector start = new PersistentVector(32, 5, EmptyNode, arr);
151+
PersistentVector start = new(32, 5, EmptyNode, arr);
152152
TransientVector ret = (TransientVector)start.asTransient();
153153
for (; items != null; items = items.next())
154154
ret = (TransientVector)ret.conj(items.first());
@@ -300,7 +300,7 @@ public override Object nth(int i, Object notFound)
300300
return notFound;
301301
}
302302

303-
object[] ArrayFor(int i)
303+
object[] ArrayFor(int i)
304304
{
305305
if (i >= 0 && i < _cnt)
306306
{
@@ -344,13 +344,13 @@ public override IPersistentVector assocN(int i, Object val)
344344
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "ClojureJVM name match")]
345345
static private Node doAssoc(int level, Node node, int i, object val)
346346
{
347-
Node ret = new Node(node.Edit, (object[])node.Array.Clone());
347+
Node ret = new(node.Edit, (object[])node.Array.Clone());
348348
if (level == 0)
349349
ret.Array[i & 0x01f] = val;
350350
else
351351
{
352-
int subidx = ( i >> level ) & 0x01f;
353-
ret.Array[subidx] = doAssoc(level-5,(Node) node.Array[subidx], i, val);
352+
int subidx = (i >> level) & 0x01f;
353+
ret.Array[subidx] = doAssoc(level - 5, (Node)node.Array[subidx], i, val);
354354
}
355355
return ret;
356356
}
@@ -365,7 +365,7 @@ static private Node doAssoc(int level, Node node, int i, object val)
365365
public override IPersistentVector cons(object o)
366366
{
367367
//if (_tail.Length < 32)
368-
if ( _cnt - tailoff() < 32 )
368+
if (_cnt - tailoff() < 32)
369369
{
370370
object[] newTail = new object[_tail.Length + 1];
371371
Array.Copy(_tail, newTail, _tail.Length);
@@ -375,9 +375,9 @@ public override IPersistentVector cons(object o)
375375

376376
// full tail, push into tree
377377
Node newroot;
378-
Node tailnode = new Node(_root.Edit, _tail);
378+
Node tailnode = new(_root.Edit, _tail);
379379
int newshift = _shift;
380-
380+
381381
// overflow root?
382382
if ((_cnt >> 5) > (1 << _shift))
383383
{
@@ -401,7 +401,7 @@ private Node pushTail(int level, Node parent, Node tailnode)
401401
// else alloc new path
402402
// return nodeToInsert placed in copy of parent
403403
int subidx = ((_cnt - 1) >> level) & 0x01f;
404-
Node ret = new Node(parent.Edit, (object[])parent.Array.Clone());
404+
Node ret = new(parent.Edit, (object[])parent.Array.Clone());
405405
Node nodeToInsert;
406406

407407
if (level == 5)
@@ -423,7 +423,7 @@ static Node newPath(AtomicReference<Thread> edit, int level, Node node)
423423
if (level == 0)
424424
return node;
425425

426-
Node ret = new Node(edit);
426+
Node ret = new(edit);
427427
ret.Array[0] = newPath(edit, level - 5, node);
428428
return ret;
429429
}
@@ -449,7 +449,7 @@ public override IPersistentCollection empty()
449449
{
450450
return (IPersistentCollection)EMPTY.withMeta(meta());
451451
}
452-
452+
453453
#endregion
454454

455455
#region IPersistentStack members
@@ -460,24 +460,24 @@ public override IPersistentCollection empty()
460460
/// <returns>The new stack.</returns>
461461
public override IPersistentStack pop()
462462
{
463-
if ( _cnt == 0 )
463+
if (_cnt == 0)
464464
throw new InvalidOperationException("Can't pop empty vector");
465-
if ( _cnt == 1)
465+
if (_cnt == 1)
466466
return (IPersistentStack)EMPTY.withMeta(meta());
467467
//if ( _tail.Length > 1 )
468468
if (_cnt - tailoff() > 1)
469469
{
470-
object[] newTail = new object[_tail.Length-1];
471-
Array.Copy(_tail,newTail,newTail.Length);
472-
return new PersistentVector(meta(),_cnt-1,_shift,_root,newTail);
470+
object[] newTail = new object[_tail.Length - 1];
471+
Array.Copy(_tail, newTail, newTail.Length);
472+
return new PersistentVector(meta(), _cnt - 1, _shift, _root, newTail);
473473
}
474474
object[] newtail = ArrayFor(_cnt - 2);
475475

476-
Node newroot = popTail(_shift,_root);
476+
Node newroot = popTail(_shift, _root);
477477
int newshift = _shift;
478-
if ( newroot == null )
478+
if (newroot == null)
479479
newroot = EmptyNode;
480-
if ( _shift > 5 && newroot.Array[1] == null )
480+
if (_shift > 5 && newroot.Array[1] == null)
481481
{
482482
newroot = (Node)newroot.Array[0];
483483
newshift -= 5;
@@ -496,7 +496,7 @@ private Node popTail(int level, Node node)
496496
return null;
497497
else
498498
{
499-
Node ret = new Node(_root.Edit, (object[])node.Array.Clone());
499+
Node ret = new(_root.Edit, (object[])node.Array.Clone());
500500
ret.Array[subidx] = newchild;
501501
return ret;
502502
}
@@ -505,7 +505,7 @@ private Node popTail(int level, Node node)
505505
return null;
506506
else
507507
{
508-
Node ret = new Node(_root.Edit, (object[])node.Array.Clone());
508+
Node ret = new(_root.Edit, (object[])node.Array.Clone());
509509
ret.Array[subidx] = null;
510510
return ret;
511511
}
@@ -693,7 +693,7 @@ public object reduce(IFn f)
693693

694694
return acc;
695695
}
696-
696+
697697

698698
public object reduce(IFn f, object start)
699699
{
@@ -751,7 +751,7 @@ public Sequential drop(int n)
751751

752752
public override IEnumerator<object> GetEnumerator()
753753
{
754-
return _vec.RangedIteratorT(_i+_offset,_vec._cnt );
754+
return _vec.RangedIteratorT(_i + _offset, _vec._cnt);
755755
}
756756

757757
IEnumerator IEnumerable.GetEnumerator()
@@ -851,7 +851,7 @@ Node PushTail(int level, Node parent, Node tailnode)
851851
// else alloc new path
852852
//return nodeToInsert placed in copy of parent
853853
int subidx = ((_cnt - 1) >> level) & 0x01f;
854-
Node ret = new Node(parent.Edit, (object[])parent.Array.Clone());
854+
Node ret = new(parent.Edit, (object[])parent.Array.Clone());
855855
Node nodeToInsert;
856856
if (level == 5)
857857
{
@@ -944,7 +944,7 @@ public ITransientVector assocN(int i, object val)
944944
Node doAssoc(int level, Node node, int i, Object val)
945945
{
946946
node = EnsureEditable(node);
947-
Node ret = new Node(node.Edit, (object[])node.Array.Clone());
947+
Node ret = new(node.Edit, (object[])node.Array.Clone());
948948
if (level == 0)
949949
{
950950
ret.Array[i & 0x01f] = val;
@@ -1053,7 +1053,7 @@ public ITransientCollection conj(object val)
10531053
}
10541054
//full tail, push into tree
10551055
Node newroot;
1056-
Node tailnode = new Node(_root.Edit, _tail);
1056+
Node tailnode = new(_root.Edit, _tail);
10571057
_tail = new object[32];
10581058
_tail[0] = val;
10591059
int newshift = _shift;
@@ -1077,16 +1077,16 @@ public IPersistentCollection persistent()
10771077
{
10781078
EnsureEditable();
10791079
_root.Edit.Set(null);
1080-
object[] trimmedTail = new object[_cnt-Tailoff()];
1081-
Array.Copy(_tail,trimmedTail,trimmedTail.Length);
1080+
object[] trimmedTail = new object[_cnt - Tailoff()];
1081+
Array.Copy(_tail, trimmedTail, trimmedTail.Length);
10821082
return new PersistentVector(_cnt, _shift, _root, trimmedTail);
10831083
}
10841084

10851085
#endregion
10861086

10871087
#region ITransientAssociative2 methods
10881088

1089-
private static readonly Object NOT_FOUND = new object();
1089+
private static readonly Object NOT_FOUND = new();
10901090

10911091
public bool containsKey(object key)
10921092
{
@@ -1103,6 +1103,18 @@ public IMapEntry entryAt(object key)
11031103

11041104
#endregion
11051105

1106+
#region IFn methods
1107+
1108+
public override object invoke(object arg1)
1109+
{
1110+
// note - relies on EnsureEditable in nth (Comment in JVM version)
1111+
if (Util.IsInteger(arg1))
1112+
return nth(Util.ConvertToInt(arg1));
1113+
throw new ArgumentException("Key must be integer");
1114+
}
1115+
1116+
#endregion
1117+
11061118

11071119
#region ILookup Members
11081120

@@ -1127,7 +1139,7 @@ public object valAt(object key, object notFound)
11271139

11281140
#endregion
11291141
}
1130-
1142+
11311143
#endregion
11321144

11331145
#region IReduce members and kvreduce
@@ -1210,7 +1222,7 @@ public Sequential drop(int n)
12101222
public override IEnumerator RangedIterator(int start, int end)
12111223
{
12121224
int i = start;
1213-
int b = i - (i%32);
1225+
int b = i - (i % 32);
12141226
object[] arr = (start < count()) ? ArrayFor(i) : null;
12151227

12161228
while (i < end)
@@ -1245,7 +1257,7 @@ IEnumerator IEnumerable.GetEnumerator()
12451257
{
12461258
return RangedIterator(0, count());
12471259
}
1248-
1260+
12491261
public override IEnumerator<object> GetEnumerator()
12501262
{
12511263
return RangedIteratorT(0, count());

0 commit comments

Comments
 (0)