Changeset 2178

Show
Ignore:
Timestamp:
07/03/08 13:58:11 (3 months ago)
Author:
jmaessen
Message:

Refactoring of Array hierarchy to clean non-self-typed functionality
out of StandardImmutableArrayType and StandardMutableArrayType and
hoist it into ReadableArray and Array respectively. The former
two types now exist only to provide self-typed results with default
implementations for operations that need them. Meanwhile
ArrayTypeWith0 was judged to mostly be introducing an extra layer of
confusion into things and was merged with ReadableArray, its supertype.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Library/FortressLibrary.fsi

    r2164 r2178  
    954954    be either mutable or immutable. *) 
    955955trait ReadableArray[\E,I\] 
    956         extends { HasRank, Indexed[\E,I\]
     956        extends { HasRank, Indexed[\E,I\], DelegatedIndexed[\E,I\]
    957957        comprises { Array[\E,I\], ImmutableArray[\E,I\] } 
    958  
     958    (** CONCRETE GETTERS 
     959        Default implementations of getters based on abstract methods 
     960        below. **) 
     961    getter indices(): Indexed[\I,I\] 
     962    getter indexValuePairs(): Indexed[\(I,E),I\] 
     963    getter generator(): Indexed[\E,I\] 
     964 
     965    (** CONCRETE METHODS 
     966        Default implementations of most array stuff based on the above. 
     967        The things we can't provide are anything involving replica. **) 
     968 
     969    opr[i:I]:E 
     970 
     971    (** Initialize element at index i with value v.  This should occur 
     972        once, before any other access or assignment occurs to element 
     973        i.  An error will be signaled if an uninitialized element is 
     974        read or an initialized element is re-initialized. **) 
     975    init(i:I, v:E) 
     976 
     977    generate[\R\](r: Reduction[\R\], body: E->R): R 
     978    seq(self): SequentialGenerator[\E\] 
     979 
     980    (** 0-based non-bounds-checked indexing. **) 
     981    abstract get(i:I): E 
     982    abstract init0(i:I, e:E): () 
     983    abstract zeroIndices(): FullRange[\I\] 
     984    (** Convert from %base%-based indexing to 0-based indexing, 
     985        performing bounds checking. **) 
     986    abstract offset(i:I): I 
     987    (** Convert from 0-based indexing to %base%-based indexing. **) 
     988    abstract toIndex(i:I): I 
    959989    (** Indexed functionality with more specific type information. **) 
    960990    abstract opr[r:Range[\I\]] : ReadableArray[\E,I\] 
     
    968998    abstract shift(newOrigin:I):ReadableArray[\E,I\] 
    969999 
    970     (** Initialize element at index %i% with value %v%.  This should occur 
    971         once, before any other access or assignment occurs to element 
    972         %i%.  An error will be signaled if an uninitialized element is 
    973         read or an initialized element is re-initialized. **) 
    974     abstract init(i:I, v:E): () 
    975  
    976     (** Bulk initialization of an array using a given function or value. **) 
     1000    (** Bulk initialization of an array using a given function or 
     1001        value.  These are defined with more specific self types in 
     1002        StandardImmutableArrayType. **) 
    9771003    abstract fill(f:I->E):ReadableArray[\E,I\] 
    9781004    abstract fill(v:E):ReadableArray[\E,I\] 
     
    9941020    abstract map[\R\](f:E->R): ImmutableArray[\R, I\] 
    9951021    abstract shift(newOrigin:I):ImmutableArray[\E,I\] 
    996     abstract init(i:I, v:E): () 
    9971022    abstract fill(f:I->E):ImmutableArray[\E,I\] 
    9981023    abstract fill(v:E):ImmutableArray[\E,I\] 
     
    10051030 
    10061031trait Array[\E,I\] extends { ReadableArray[\E,I\], MutableIndexed[\E,I\] } 
     1032    abstract put(i:I, e:E): () 
     1033    opr[i:I]:=(v:E):() 
     1034 
     1035    opr[r:Range[\I\]]:=(a:Indexed[\E,I\]):() 
     1036 
    10071037    abstract opr[r:Range[\I\]] : Array[\E,I\] 
    10081038    abstract opr[_:OpenRange[\Any\]] : Array[\E,I\] 
     
    10101040    abstract map[\R\](f:E->R): Array[\R, I\] 
    10111041    abstract shift(newOrigin:I):Array[\E,I\] 
    1012     abstract init(i:I, v:E): () 
    10131042    abstract fill(f:I->E):Array[\E,I\] 
    10141043    abstract fill(v:E):Array[\E,I\] 
     
    10371066primitiveImmutableArray[\E\](x:ZZ32):ImmutableArray[\E,ZZ32\] 
    10381067 
    1039 (** Array type supporting un-bounds-checked 0-based indexing. 
    1040     Useful for the internals of all the array functionality. **) 
    1041 trait ArrayTypeWith0[\E,I\] 
    1042       extends { ReadableArray[\E,I\], DelegatedIndexed[\E,I\] } 
    1043     (** 0-based non-bounds-checked indexing. **) 
    1044     abstract getDefault(i:I): E 
    1045     abstract init0(i:I, e:E): () 
    1046     abstract zeroIndices(): FullRange[\I\] 
    1047     (** Convert from %base%-based indexing to 0-based indexing, 
    1048         performing bounds checking. **) 
    1049     abstract offset(i:I): I 
    1050     (** Convert from 0-based indexing to %base%-based indexing. **) 
    1051     abstract toIndex(i:I): I 
    1052 end 
    1053  
    10541068(** NOTE: %StandardImmutableArrayType% is a parent of 
    10551069    %StandardMutableArrayType%.  It therefore does not extend 
    1056     %ImmutableArrayType% as you might expect.  Other types that extend 
    1057     it should also extend %ImmutableArrayType% explicitly. **) 
     1070    %ImmutableArray% as you might expect.  Other types that extend 
     1071    it should also extend %ImmutableArray% explicitly. **) 
    10581072trait StandardImmutableArrayType[\T extends StandardImmutableArrayType[\T,E,I\],E,I\] 
    1059         extends { ArrayTypeWith0[\E,I\] } 
    1060     (** CONCRETE GETTERS: 
    1061         Default implementations of getters based on abstract methods 
    1062         in %StandardArrayType%. **) 
    1063     getter indices(): Indexed[\I,I\] 
    1064     getter indexValuePairs(): Indexed[\(I,E),I\] 
    1065     getter generator(): Indexed[\E,I\] 
    1066  
    1067     (** CONCRETE METHODS: 
    1068         Default implementations of most array stuff based on the above. 
    1069         The things we cannot provide are anything involving replica. **) 
    1070     opr[i:I]:E 
    1071     init(i:I, v:E) 
    1072  
    1073     generate[\R\](r: Reduction[\R\], body: E->R): R 
    1074     seq(self): SequentialGenerator[\E\] 
    1075  
     1073        extends { ReadableArray[\E,I\] } 
    10761074    fill(f:I->E):T 
    10771075    fill(v:E):T 
     
    10811079 
    10821080trait StandardMutableArrayType[\T extends StandardMutableArrayType[\T,E,I\],E,I\] 
    1083     extends { StandardImmutableArrayType[\T,E,I\], Array[\E,I\] } 
    1084     (** 0-based non-bounds-checked indexing. **) 
    1085     abstract put(i:I, e:E): () 
    1086     opr[i:I]:=(v:E):() 
    1087  
    1088     opr[r:Range[\I\]]:=(a:Indexed[\E,I\]):() 
    1089  
     1081        extends { StandardImmutableArrayType[\T,E,I\], Array[\E,I\] } 
    10901082    assign(v:T):T 
    10911083    assign(f:I->E):T 
     
    11061098    %b0+s0-1%. **) 
    11071099trait ReadableArray1[\T, nat b0, nat s0\] 
    1108         extends { Indexed1[\s0\], Rank1, ArrayTypeWith0[\T,ZZ32\] } 
     1100        extends { Indexed1[\s0\], Rank1, ReadableArray[\T,ZZ32\] } 
    11091101        comprises { ImmutableArray1[\T,b0,s0\], Array1[\T,b0,s0\] } 
    11101102    getter size():ZZ32 
  • trunk/Library/FortressLibrary.fss

    r2172 r2178  
    13111311   be either mutable or immutable. *) 
    13121312trait ReadableArray[\E,I\] 
    1313         extends { HasRank, Indexed[\E,I\]
     1313        extends { HasRank, Indexed[\E,I\], DelegatedIndexed[\E,I\]
    13141314        comprises { Array[\E,I\], ImmutableArray[\E,I\] } 
     1315    (** CONCRETE GETTERS 
     1316        Default implementations of getters based on abstract methods 
     1317        below. **) 
     1318    getter indices(): Indexed[\I,I\] = bounds() 
     1319    getter indexValuePairs(): Indexed[\(I,E),I\] = 
     1320        zeroIndices().map[\(I,E)\](fn (i:I):(I,E) => (toIndex(i),get(i))) 
     1321    getter generator(): Indexed[\E,I\] = 
     1322        zeroIndices().map[\E\](fn (i:I):E => get(i)) 
     1323 
     1324    (** CONCRETE METHODS 
     1325        Default implementations of most array stuff based on the above. 
     1326        The things we can't provide are anything involving replica. **) 
     1327 
     1328    opr[i:I]:E = get(offset(i)) 
     1329 
     1330    (** Initialize element at index i with value v.  This should occur 
     1331        once, before any other access or assignment occurs to element 
     1332        i.  An error will be signaled if an uninitialized element is 
     1333        read or an initialized element is re-initialized. **) 
     1334    init(i:I, v:E) = init0(offset(i),v) 
     1335 
     1336    generate[\R\](r: Reduction[\R\], body: E->R): R = 
     1337        generator().generate[\R\](r,body) 
     1338    seq(self): SequentialGenerator[\E\] = seq(generator()) 
     1339 
     1340    (** 0-based non-bounds-checked indexing **) 
     1341    get(i:I): E 
     1342    init0(i:I, e:E): () 
     1343    zeroIndices(): FullRange[\I\] 
     1344    (** Convert from base()-based indexing to 0-based indexing, 
     1345        performing bounds checking. **) 
     1346    offset(i:I): I 
     1347    (** Convert from 0-based indexing to base()-based indexing **) 
     1348    toIndex(i:I): I 
    13151349 
    13161350    (** Indexed functionality with more specific type information **) 
     
    13251359    shift(newOrigin:I):ReadableArray[\E,I\] 
    13261360 
    1327     (** Initialize element at index i with value v.  This should occur 
    1328         once, before any other access or assignment occurs to element 
    1329         i.  An error will be signaled if an uninitialized element is 
    1330         read or an initialized element is re-initialized. **) 
    1331     init(i:I, v:E): () 
    1332  
    1333     (** Bulk initialization of an array using a given function or value **) 
     1361    (** Bulk initialization of an array using a given function or 
     1362        value.  These are defined with more specific self types in 
     1363        StandardImmutableArrayType. **) 
    13341364    fill(f:I->E):ReadableArray[\E,I\] 
    13351365    fill(v:E):ReadableArray[\E,I\] 
     
    13571387    map[\R\](f:E->R): ImmutableArray[\R, I\] 
    13581388    shift(newOrigin:I):ImmutableArray[\E,I\] 
    1359     init(i:I, v:E): () 
    13601389    fill(f:I->E):ImmutableArray[\E,I\] 
    13611390    fill(v:E):ImmutableArray[\E,I\] 
     
    13681397 
    13691398trait Array[\E,I\] extends { ReadableArray[\E,I\], MutableIndexed[\E,I\] } 
     1399    (** 0-based non-bounds-checked indexing **) 
     1400    put(i:I, e:E): () 
     1401    opr[i:I]:=(v:E):() = put(offset(i),v) 
     1402 
     1403    opr[r:Range[\I\]]:=(a:Indexed[\E,I\]):() = do 
     1404        a0 = a[a.bounds()]      (* Make a have 0 origin *) 
     1405        s0 = self[r]            (* Make self have 0 origin *) 
     1406        if a0.bounds() = s0.bounds() then 
     1407            for (i,v) <- a0.indexValuePairs() do 
     1408                s0.put(i,v) 
     1409            end 
     1410        else 
     1411            fail("Can't assign indexed with bounds " a.bounds() // 
     1412                 " to differently-sized subarray " self.bounds()) 
     1413        end 
     1414      end 
     1415 
     1416    (** The following are repetition with better return type info. **) 
    13701417    opr[r:Range[\I\]] : Array[\E,I\] 
    13711418    opr[_:OpenRange[\Any\]] : Array[\E,I\] 
     
    13731420    map[\R\](f:E->R): Array[\R, I\] 
    13741421    shift(newOrigin:I):Array[\E,I\] 
    1375     init(i:I, v:E): () 
    13761422    fill(f:I->E):Array[\E,I\] 
    13771423    fill(v:E):Array[\E,I\] 
     
    14321478    PrimImmutableArray[\E,n\]() 
    14331479 
    1434 (** Array type supporting un-bounds-checked 0-based indexing. 
    1435     Useful for the internals of all the array functionality. **) 
    1436 trait ArrayTypeWith0[\E,I\] 
    1437       extends { ReadableArray[\E,I\], DelegatedIndexed[\E,I\] } 
    1438     (** 0-based non-bounds-checked indexing **) 
    1439     get(i:I): E 
    1440     init0(i:I, e:E): () 
    1441     zeroIndices(): FullRange[\I\] 
    1442     (** Convert from base()-based indexing to 0-based indexing, 
    1443         performing bounds checking. **) 
    1444     offset(i:I): I 
    1445     (** Convert from 0-based indexing to base()-based indexing **) 
    1446     toIndex(i:I): I 
    1447 end 
    1448  
    14491480(** NOTE: StandardImmutableArrayType is a parent of 
    14501481    StandardMutableArrayType.  It therefore doesn't extend 
    1451     ImmutableArrayType as you might expect.  Other types that extend 
    1452     it should also extend ImmutableArrayType explicitly. **) 
     1482    ImmutableArray as you might expect.  Other types that extend 
     1483    it should also extend ImmutableArray explicitly. **) 
    14531484trait StandardImmutableArrayType[\T extends StandardImmutableArrayType[\T,E,I\],E,I\] 
    1454         extends { ArrayTypeWith0[\E,I\] } 
    1455     (** CONCRETE GETTERS 
    1456         Default implementations of getters based on abstract methods 
    1457         in StandardArrayType. **) 
    1458     getter indices(): Indexed[\I,I\] = bounds() 
    1459     getter indexValuePairs(): Indexed[\(I,E),I\] = 
    1460         zeroIndices().map[\(I,E)\](fn (i:I):(I,E) => (toIndex(i),get(i))) 
    1461     getter generator(): Indexed[\E,I\] = 
    1462         zeroIndices().map[\E\](fn (i:I):E => get(i)) 
    1463  
    1464     (** CONCRETE METHODS 
    1465         Default implementations of most array stuff based on the above. 
    1466         The things we can't provide are anything involving replica. **) 
    1467     opr[i:I]:E = get(offset(i)) 
    1468     init(i:I, v:E) = init0(offset(i),v) 
    1469  
    1470     generate[\R\](r: Reduction[\R\], body: E->R): R = 
    1471         generator().generate[\R\](r,body) 
    1472     seq(self): SequentialGenerator[\E\] = seq(generator()) 
    1473  
     1485        extends { ReadableArray[\E,I\] } 
    14741486    fill(f:I->E):T = do 
    14751487        for i <- zeroIndices() do init0(i,f(toIndex(i))) end 
     
    14861498 
    14871499trait StandardMutableArrayType[\T extends StandardMutableArrayType[\T,E,I\],E,I\] 
    1488     extends { StandardImmutableArrayType[\T,E,I\], Array[\E,I\] } 
    1489     (** 0-based non-bounds-checked indexing **) 
    1490     put(i:I, e:E): () 
    1491     opr[i:I]:=(v:E):() = put(offset(i),v) 
    1492  
    1493     opr[r:Range[\I\]]:=(a:Indexed[\E,I\]):() = do 
    1494         a0 = a[a.bounds()]      (* Make a have 0 origin *) 
    1495         s0 = self[r]            (* Make self have 0 origin *) 
    1496         if a0.bounds() = s0.bounds() then 
    1497             for (i,v) <- a.indexValuePairs() do 
    1498                 s0.put(i,v) 
    1499             end 
    1500         else 
    1501             fail("Can't assign indexed with bounds " a.bounds() // 
    1502                  " to differently-sized subarray " self.bounds()) 
    1503         end 
    1504       end 
    1505  
     1500        extends { StandardImmutableArrayType[\T,E,I\], Array[\E,I\] } 
    15061501    assign(v:T):T = do 
    15071502        for i <- zeroIndices() do put(i,v.get(i)) end 
     
    15291524    The natural order of all generators is from b0 to b0+s0-1. **) 
    15301525trait ReadableArray1[\T, nat b0, nat s0\] 
    1531         extends { Indexed1[\s0\], Rank1, ArrayTypeWith0[\T,ZZ32\] } 
     1526        extends { Indexed1[\s0\], Rank1, ReadableArray[\T,ZZ32\] } 
    15321527        comprises { ImmutableArray1[\T,b0,s0\], Array1[\T,b0,s0\] } 
    15331528    getter size():ZZ32 = s0