Skip to content
Permalink
Browse files

Fix re-initialization of shaped arrays

- in a nutshell: old values would stay behind on incomplete re-init
  - my @A[2;2] = (0,1),(2,3); @A = (5,6),(); say @A  # [[5,6],2,3]]
- fixed by basically creating a new array with same shape
  - and binding the reified of that into the current object
  - and then do the normal initialization process
  • Loading branch information...
lizmat committed Nov 19, 2018
1 parent 7262b4e commit 49f07abe99508d3fe3f9777ac78f0b676d41438b
Showing with 33 additions and 20 deletions.
  1. +18 −16 src/core/Shaped1Array.pm6
  2. +15 −4 src/core/ShapedArray.pm6
@@ -92,6 +92,17 @@
nqp::bindpos(nqp::getattr(self,List,'$!reified'),one,value)
}

method !RE-INITIALIZE(::?CLASS:D:) {
my \list := nqp::getattr(self,List,'$!reified');
nqp::bind( # rebind newly created list
list,
nqp::bindattr(
self,List,'$!reified',
nqp::setelems(nqp::create(list),nqp::elems(list))
)
)
}

proto method STORE(::?CLASS:D: |) {*}
multi method STORE(::?CLASS:D: ::?CLASS:D \from-array) {
nqp::stmts(
@@ -121,17 +132,13 @@
)
}
multi method STORE(::?CLASS:D: Iterable:D \in, :$INITIALIZE) {
my \list := nqp::getattr(self,List,'$!reified');
my int $elems = nqp::elems(list);
nqp::bind( # rebind newly created list if not the first time
list,
nqp::bindattr(
self,List,'$!reified',nqp::setelems(nqp::create(list),$elems)
)
) unless $INITIALIZE;
my \list := $INITIALIZE
?? nqp::getattr(self,List,'$!reified')
!! self!RE-INITIALIZE;
my \desc := nqp::getattr(self,Array,'$!descriptor');
my \iter := in.iterator;
my int $i = -1;
my int $elems = nqp::elems(list);
nqp::until(
nqp::eqaddr((my \pulled := iter.pull-one),IterationEnd)
|| nqp::iseq_i(($i = nqp::add_i($i,1)),$elems),
@@ -145,14 +152,9 @@
self
}
multi method STORE(::?CLASS:D: Mu \item, :$INITIALIZE) {
my \list := nqp::getattr(self,List,'$!reified');
nqp::bind( # rebind newly created list if not the first time
list,
nqp::bindattr(
self,List,'$!reified',
nqp::setelems(nqp::create(list),nqp::elems(list))
)
) unless $INITIALIZE;
my \list := $INITIALIZE
?? nqp::getattr(self,List,'$!reified')
!! self!RE-INITIALIZE;
nqp::bindpos(list,0,
nqp::p6scalarfromdesc(nqp::getattr(self,Array,'$!descriptor'))
) = item;
@@ -278,11 +278,19 @@
}
sub NUMCPY(Mu \to, Mu \from) { NumCopy.new(to,from).sink-all }

method !RE-INITIALIZE(::?CLASS:D:) {
nqp::bindattr( # this is a yucky way to re-init, but it works
self,List,'$!reified',
nqp::getattr(self.new(:shape(self.shape)),List,'$!reified')
)
}

proto method STORE(::?CLASS:D: |) {*}
multi method STORE(::?CLASS:D: ::?CLASS:D \in) {
multi method STORE(::?CLASS:D: ::?CLASS:D \in, :$INITIALIZE) {
nqp::if(
in.shape eqv self.shape,
nqp::stmts(
nqp::unless($INITIALIZE,self!RE-INITIALIZE),
MEMCPY(self,in), # VM-supported memcpy-like thing?
self
),
@@ -292,10 +300,11 @@
).throw
)
}
multi method STORE(::?CLASS:D: array:D \in) {
multi method STORE(::?CLASS:D: array:D \in, :$INITIALIZE) {
nqp::if(
in.shape eqv self.shape,
nqp::stmts(
nqp::unless($INITIALIZE,self!RE-INITIALIZE),
nqp::if(
nqp::istype(in.of,Int),
INTCPY(self,in), # copy from native int
@@ -376,7 +385,8 @@
)
}
}
multi method STORE(::?CLASS:D: Iterable:D \in) {
multi method STORE(::?CLASS:D: Iterable:D \in, :$INITIALIZE) {
self!RE-INITIALIZE unless $INITIALIZE;
StoreIterable.new(self,in).sink-all;
self
}
@@ -404,7 +414,8 @@
)
}
}
multi method STORE(::?CLASS:D: Iterator:D \iterator) {
multi method STORE(::?CLASS:D: Iterator:D \iterator, :$INITIALIZE) {
self!RE-INITIALIZE unless $INITIALIZE;
StoreIterator.new(self,iterator).sink-all;
self
}

0 comments on commit 49f07ab

Please sign in to comment.
You can’t perform that action at this time.