www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - home made copy-on-write arrays

Discussing constant-ness / read-only-ness.....

As we know, in D, there is no difference between
array (master buffer) and slices from it (slave buffer).
So far this unisex solution is working (at least in Harmonia) and
allows to build effective code.
So I can e.g. return subvector of Widgets from a container and
caller can use it effectively for immutable enumerations (90% of cases).
The main problem for me is that I want to prevent cases when caller
will decide to modify such subvector - it can modify master buffer
too. The bad thing that it may happen in code far from original place
and in worst case asynchronously. Implementation nightmare to be short.

So to add some robustness to the library and keep effectiveness almost
at the same level I've came up with the idea of using array
wrappers with 'mutable' flags. (Kris, thanks, this is sort of your 
read-only)

struct array(T)
{
    T[] elements;
    bit  mutable;

    void mutate();
   ....
}

Full implementation of the array(T) attached.
It is an imperfect solution - there are some holes - but it seems is the
best one of what we can reach without compiler/runtime support.
(Actually it is feasible to implement mutable flag as higher bit in the
 length field of builtin arrays - max possible length will be twice less 
though. )

Any comments will be appreciated at this point
as I am going to use this approach for strings also - so it will be a 
massive update.

Andrew.


begin 666 types.d
M+RH-"B  *RTM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM

M:6$ 1U5)($9R86UE=V]R:RP =F5R<VEO;B Q+C N("  ("  ("  ("  ("  
M("  ("  ("  ("  ("  ("  ? T*(" K+2TM+2TM+2TM+2TM+2TM+2TM+2TM
M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM


M<F$ 26YF;W)M871I8V$ 4V]F='=A<F4L($EN8RX ("  ("  ("  ("  ("  
M("  ("  ("  ("  ("  ("  ("  ? T*(" K+2TM+2TM+2TM+2TM+2TM+2TM
M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM
M+2TM+2TM+2L-"B  ?"!4:&ES('-O=7)C92!F:6QE(&ES('-U8FIE8W0 =&\ 

M=&AA="!I<R!B=6YD;&5D('=I=&  =&AI<R!P86-K86=E(&EN('1H92!F:6QE
M($Q)0T5.4T4N5%A4+"!A;F0 :7, ("  ? T*("!\(&%V86EL86)L92!T:')O
M=6=H('1H92!W;W)L9"UW:61E+7=E8B!A="!T:&4 9F]L;&]W:6YG('5R;#H 
M("  ("  ("  ('P-"B  ?"!H='1P.B\O=W=W+G1E<G)A:6YF;W)M871I8V$N

M("LM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM
M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM*PT*("!\($%U=&AO<G,Z($%N
M9')E=R!&<F5D;VYI;W5K("  ("  ("  ("  ("  ("  ("  ("  ("  ("  
M("  ("  ("  ("  ('P-"B  *RTM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM
M+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TM+2TK

M+WP-"B\O?"!A<G)A>2A4*2P 1&EC=&EO;F%R>2A++%8I+"!3>6U486)L92A4

M:6UP;W)T(&AA<FUO;FEA+G-T<FEN9SL-" T*<')I=F%T92!I;7!O<G0 <W1D





M86)L92 ](&9A;'-E("D >R!A<G)A>2!A.R!A+F5L96UE;G1S(#T =CL 82YM
M=71A8FQE(#T
M<$-A;&PH(&%R<F%Y('8 *2![(&%R<F%Y(&$[(&$ /2!V.R!R971U<FX 83L 


M871E*'5I;G0 97AT<F%%;&5M96YT<RD +R\ =7-E('1H:7, ;VYL>2!F;W( 



M=&A=.R -"B  ("  (&YE6S N+F5L96UE;G1S+FQE;F=T:%T /2!E;&5M96YT
M<ULP+BYE;&5M96YT<RYL96YG=&A=.R -"B  ("  (&YE+FQE;F=T:" ](&5L
M96UE;G1S+FQE;F=T:#L-"B  ("  (&5L96UE;G1S(#T
M=71A8FQE(#T
M87D 8GD ;VYE(&5L96UE;G0-"B  =F]I9"!P=7-H*%0 =BD-"B  ("  ('L-

M("  ("  ?0T*(" O+R!A<'!E;F0 87)R87D 8GD 96QE;65N=',-"B  =F]I
M9"!P=7-H*%1;72!V*0T*("  ("  >PT*("  ("  ("!M=71A=&4H=BYL96YG

M(&1E;&5T92!L87-T(&5L96UE;G0 86YD(')E='5R;B!I= T*("!4('!O<" I

M('=E( T*("  ("  (" O+R!A<F4 ;VYL>2!D96-R96%S:6YG(&QE;F=T: T*

M(&%S<V5R="AL(#X


M87D-"B  =F]I9"!I;G-E<G0H=6EN="!A="P 5"!V*0T*("  ("  >PT*("  

M;F=T:#L-"B  ("  ("  :68H870 /CT ;"D >R!E;&5M96YT<R!^/2!V.R!R
M971U<FX[('T-"B  ("  ("  96QE;65N=',N;&5N9W1H(#T
M("  ("  (&UE;6UO=F4H)F5L96UE;G1S6V%T*S%=+"9E;&5M96YT<UMA=%TL
M*&PM870I*E0N<VEZ96]F*3L-"B  ("  ("  96QE;65N='-;871=(#T =CL-


M("  (&UU=&%T92AV82YL96YG=& I.PT*("  ("  ("!U:6YT(&P /2!E;&5M

M?CT =F$[(')E='5R;CL ?0T*("  ("  ("!E;&5M96YT<RYL96YG=&  /2!L
M("L =F$N;&5N9W1H.PT*("  ("  ("!M96UM;W9E*"9E;&5M96YT<UMA=" K
M('9A+FQE;F=T:%TL)F5L96UE;G1S6V%T72PH;"UA="DJ5"YS:7IE;V8I.PT*
M("  ("  ("!E;&5M96YT<UMA="XN870K=F$N;&5N9W1H72 ]('9A.PT*("  

M=70H=6EN="!A="D-"B  ("  ('L-"B  ("  ("  =6EN="!L(#T 96QE;65N
M=',N;&5N9W1H("T ,3L-"B  ("  ("  :68H870 /CT ;"D <F5T=7)N('!O
M<" I.PT*("  ("  ("!M=71A=&4H*3L-"B  ("  ("  5"!T(#T 96QE;65N
M='-;871=.R -"B  ("  ("  ;65M;6]V92 F96QE;65N='-;871=+"9E;&5M
M96YT<UMA="LQ72PH;"UA="DJ5"YS:7IE;V8I.PT*("  ("  ("!E;&5M96YT
M<RYL96YG=&  /2!L.PT*("  ("  ("!R971U<FX =#L-"B  ("  ('T-" T*
M(" O+R!J=7-T(&1E;&5T92!R86YG90T*(" O+R!H86QF+6]P96X <F%N9V4 

M('L-"B  ("  ("  ("!A<W-E<G0H870 /#T 96YD*3L-"B  ("  ("  ("!I
M9BAA=" ]/2!E;F0I(')E='5R;CL-"B  ("  ("  ("!U:6YT(&P /2!E;&5M
M96YT<RYL96YG=&  +2 H96YD("T 870I.PT*("  ("  ("  (&EF*&%T(#X]
M(&PI('L 96QE;65N=',N;&5N9W1H(#T 870[(')E='5R;CL ?0T*("  ("  
M("  (&UU=&%T92 I.PT*("  ("  ("  (&UE;6UO=F4H)F5L96UE;G1S6V%T
M72PF96QE;65N='-;96YD72PH;"UA="DJ5"YS:7IE;V8I.PT*("  ("  ("  

M;F1E>"!O9B!T:&4 96QE;65N="!I;B!A<G)A>2P +3$ :68 ;F]T(&9O=6YD

M;&5M96YT<RYL96YG=&  +2 Q.PT*("  ("  (&9O<B [(&D /CT ,#L +2UI
M*0T*("  ("  ("  (&EF*"!E;&5M96YT<UMI72 ]/3T =" I(&)R96%K.PT*


M;G0 ;FPI( T*("![( T*("  (&EF*"!N;" ^(&5L96UE;G1S+FQE;F=T:" I


M971U<FX 96QE;65N='-;,%T[('T-"B  5"  ;&%S=" I('L <F5T=7)N(&5L


M861S('8 /2!A6VE=.PT*("!4("  (&]P26YD97 H=6EN="!I*2![(')E='5R

M(%0 ("  ;W!);F1E>$%S<VEG;BA4('<L('5I;G0 :2D >R!M=71A=&4H*3L 

M6UT-"B  87)R87D (&]P4VQI8V4H*2![(')E='5R;B!O<$-A;&PH96QE;65N

M<F%Y("!O<%-L:6-E*'5I;G0 ;"P =6EN="!H*2![(')E='5R;B!O<$-A;&PH

M<F%Y('X](%0-"B  87)R87DJ("!O<$-A=$%S<VEG;BA4('<I('L <'5S:"AW
M*3L <F5T=7)N('1H:7,[('T ( T*(" O+R!O=F5R;&]A9', 87)R87D ?CT 
M5%M=( T*("!A<G)A>2H (&]P0V%T07-S:6=N*%1;72!W82D >R!P=7-H*'=A


M='5R;B!T:&ES.R!]( T*(" O+R!O=F5R;&]A9', 87)R87D /2!A<G)A>2!^
M(%1;70T*("!A<G)A>2H (&]P0V%T*%1;72!V82D >R!P=7-H*'9A*3L <F5T

M>2 -"B  87)R87DJ("!O<$-A=%]R*%0 =BD >R!I;G-E<G0H,"QV*3L <F5T

M<F%Y( T*("!A<G)A>2H (&]P0V%T7W(H5%M=('9A*2![(&EN<V5R=" P+'9A

M="!O;FQY(&9O<B!R96%D+6]N;'D 86-C97-S(0T*("!46UT ("  (&]P0V%S


M;7,-"B  :6YT("  ("!O<$%P<&QY*&EN="!D96QE9V%T92AI;F]U="!4*2!D
M9RD-"B  >R  ( T*("  (&EN="!R97-U;'0 /2 P.PT*("  (&9O<BAU:6YT
M(&D /2 P.R!I(#P
M("  ("!4('0 /2!E;&5M96YT<UMI73L-"B  ("  (')E<W5L=" ](&1G*'0I
M.PT*("  ("  :68 *')E<W5L="D (&)R96%K.PT*("  ('T-"B  ("!R971U


M("!?9F]R=V%R9#L-"B  ("!I;G0 ;W!!<'!L>2AI;G0 9&5L96=A=&4H:6YO


M/"!?82YL96YG=& [("LK:2 I('L 5"!T(#T 7V%;:5T[(')E<W5L=" ](&1G


M>R!4('0 /2!?85MI73L <F5S=6QT(#T 9&<H="D[(&EF("AR97-U;'0I("!B
M<F5A:SL ?0T*("  ("  <F5T=7)N(')E<W5L=#L-
M14Y532!I=&5M<RAB;V]L(&9O<G=A<F0I('L 14Y532!W.R!W+E]F;W)W87)D
M(#T 9F]R=V%R9#L =RY?82 ](&5L96UE;G1S.R!R971U<FX =SL ?0T*("!%
M3E5-(&9O<G=A<F0H*2![(')E='5R;B!I=&5M<RAT<G5E*3L ?0T*("!%3E5-

M=6-T($U55$%"3$5?14Y532 O+R!B:61I<F5C=&EO;F%L(&UU=&%B;&4 96YU


M*0T*("  ('L (" -"B  ("  (&EN="!R97-U;'0 /2 P.PT*("  ("  :68H

M9W1H.R K*VD *2![(')E<W5L=" ](&1G*%]A6VE=*3L (&EF("AR97-U;'0I
M("!B<F5A:SL ?0T*("  ("  96QS90T*("  ("  ("!F;W(H:6YT(&D /2!?
M82YL96YG=&  +2 Q.R!I(#X
M:5TI.R!I9B H<F5S=6QT*2  8G)E86L[('T-"B  ("  (')E='5R;B!R97-U

M*&)O;VP 9F]R=V%R9"D >R!M=71A=&4H*3L 355404),15]%3E5-('<[('<N
M7V9O<G=A<F0 /2!F;W)W87)D.R!W+E]A(#T 96QE;65N=',[(')E='5R;B!W


M8FQE7V)A8VMW87)D*"D >R!R971U<FX ;75T86)L95]I=&5M<RAF86QS92D[

M=&AR;W5G:" -"B  +R\ ;VYL>2!E;&5M96YT<R!O9B!T:&4 87)R87D =VAI


M8W0 4T5.54T-"B  ("  ('L-"B  ("  ("  5%M=(%]A.PT*("  ("  ("!I


M<F5S=6QT(#T ,#L-"B  ("  ("  ("!I9BA?9F]R=V%R9"D-"B  ("  ("  
M("  (&9O<BAU:6YT(&D /2 P.R!I(#P 7V$N;&5N9W1H.R K*VD *2 -"B  
M("  ("  ("  ('L-"B  ("  ("  ("  ("  5%0 ='0 /2!C87-T*%14*5]A
M6VE=.PT*("  ("  ("  ("  ("!I9BAT="D-"B  ("  ("  ("  ("  ("!R


M("  ("!F;W(H:6YT(&D /2!?82YL96YG=&  +2 Q.R!I(#X
M( T*("  ("  ("  ("  >PT*("  ("  ("  ("  ("!45"!T=" ](&-A<W0H

M("  (')E<W5L=" ](&1G*'1T*3L-"B  ("  ("  ("  ("  :68 *')E<W5L
M="D (&)R96%K.PT*("  ("  ("  ("  ?0T*("  ("  ("  (')E='5R;B!R

M="AB;V]L(&9O<G=A<F0 /2!T<G5E*2![(%-%3E5-('<[('<N7V9O<G=A<F0 
M/2!F;W)W87)D.R!W+E]A(#T
M" T*(" O+R!D96QE=&5S('1H92!B=69F97(L($9,04U-04),12P 55-%(%=)
M5$  0T%212$-"B  =F]I9"!D97-T<F]Y*"D >R!I9BAM=71A8FQE*2!D96QE
M=&4 96QE;65N=',[(&5L96UE;G1S(#T ;G5L;#L

M8VQA<W, 4WEM5&%B;&4-"GL-"B  86QI87, ("!U:6YT('-Y;6)O;#L-"B  
M<W1A=&EC("!C:&%R6UT =6YD969I;F5D(#T (GMU;F1E9FEN961](CL +R\ 
M=6YD969I;F5D(&5L96UE;G0-"G!R:79A=&4Z(" -"B  =6EN=%MC:&%R6UU=

M9&0 4F5F(&]F(&5L96UE;G0-"B  =F]I9"!A9&12968H8VAA<EM=(&5L96UE

M("  <WEM8F]L('-Y;2 ](&-A<W0H<WEM8F]L*2AE;&5M96YT<RYL96YG=&  

M='-;<WEM("T ,5T /2!E;&5M96YT.PT*("  (&UA<%L 96QE;65N="!=(#T 

M;FET:6%L:7IE(%!O;VP 8GD =&%B;&4 ;V8 <W1A=&EC(&]B:F5C=',-"B  
M=&AI<RAC:&%R6UT =&%B;&5;72D >R!F;W)E86-H*&-H87);72!W.R!T86)L


M<WEM8F]L('-Y;2 ](&UA<%ME;&5M96YT73L-"B  ("!I9B  <WEM(#T



M72 ](&5L96UE;G0N9'5P.PT*("  ("  ;6%P6R!E;&5M96YT(%T /2!S>6T[




M('T-"B  +R\ =&5S="!F;W( 97AI<W1E;F-E(&]F(&5L96UE;G0 :6X =&AE
M('!O;VP-"B  8F]O;"!E>&ES=',H8VAA<EM=(&5L96UE;G0I('L <F5T=7)N

M;B!E;&5M96YT+"!R971U<FYS(&$ =6YI<75E(&-O<'D ;V8 =&AE(&5L96UE
M;G0-"B  8VAA<EM=(&EN=&5R;BAC:&%R6UT 96QE;65N="D >R!R971U<FX 

M+R!$:6-T:6]N87)Y+"!M87!S(&ME>2]V86QU92!P86ER('1O('1H92!S>6UB
M;VPH=6EN="D ( T*+R\ 06QL;W=S('1O(')E=')I979E('9A;'5E(&)Y(&ET

M<GDH2T59+"!604Q512D-"GL-"B  86QI87, ("!U:6YT("  <WEM8F]L.PT*
M("!S=&%T:6, (%9!3%5%("!U;F1E9FEN960[("\O('5N9&5F:6YE9"!E;&5M




M>6UB;VP ;W!);F1E>"A+15D :V5Y+"!604Q512!D96QE9V%T92A+15D :RD 
M8W1O<BD-"B  >PT*("  ('-Y;6)O;"!S>6T /2!M87!;:V5Y73L-"B  ("!I
M9B  <WEM(#T


M("!E;&5M96YT<UMS>6T +2 Q72 ](&-T;W(H:V5Y*3L-"B  ("  (&UA<%L 

M<WEM8F]L(&]P26YD97 H2T59(&ME>2P 5D%,544 9G5N8W1I;VXH2T59(&LI



M<W-I9VXH5D%,544 =BP 2T59(&ME>2D-"B  >PT*("  ('-Y;6)O;"!S>6T 
M/2!M87!;:V5Y73L-"B  ("!I9B  <WEM(#T






M*"!S>6T /B P("8F('-Y;2 \/2!E;&5M96YT<RYL96YG=&  *3\ 96QE;65N

M<W1E;F-E(&]F(&5L96UE;G0 :6X =&AE('!O;VP-"B  8F]O;"!E>&ES=',H

M(&)O;VP =F%L:60H<WEM8F]L('-Y;2D >R!R971U<FX *"!S>6T /B P("8F
M('-Y;2 \/2!E;&5M96YT<RYL96YG=&  *3L ?0T*(" -"B  +R\ :6YT97)N
M(&5L96UE;G0L(')E='5R;G, 82!U;FEQ=64 8V]P>2!O9B!T:&4 96QE;65N
M= T*("!604Q512!I;G1E<FXH2T59(&ME>2P 5D%,544 9G5N8W1I;VXH2T59

M*&ME>2QC=&]R*2 I.R -"B  ?0T*("!604Q512!I;G1E<FXH2T59(&ME>2P 
M5D%,544 9&5L96=A=&4H2T59(&LI(&-T;W( *2 -"B  >R -"B  ("!R971U

M"G-T<G5C="!D871E=&EM90T*>PT*("!D7W1I;64 9'0 /2!L;VYG+FUI;CL-
M" T*("!B;V]L('9A;&ED*"D >R!R971U<FX 9'0 (3T 9%]T:6UE7VYA;CL 

M=&5T:6UE(&0[(&0N9'0 /2!D=#L <F5T=7)N(&0[('T-"B  <W1A=&EC(&1A
M=&5T:6UE(&]P0V%L;"AC:&%R6UT <RD >R!D871E=&EM92!D.R!D+F1T(#T 

M=&EM92!O<$-A;&PH=V-H87);72!S*2![(&1A=&5T:6UE(&0[(&0N9'0 /2!S
M=&0N9&%T92YP87)S92AW,F$H<RDI.R!R971U<FX 9#L

M3U)-050 9B ]($9/4DU!5"Y55$,I( T*("![( T*("  (&EF*"!F(#T]($9/
M4DU!5"Y55$, *2!R971U<FX <W1D+F1A=&4N=&]55$-3=')I;F<H9'0I.PT*
M("  (&5L<V4 :68H(&8 /3T 1D]234%4+D=-5" I(')E='5R;B!S=&0N9&%T





` end
May 26 2005