3030# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3131
3232from SimpleDataTypes import *
33+ from TypeChecker import *
3334
3435class BaseAggregate (object ):
3536 """ A class that define common properties to ARRAY, LIST, SET and BAG.
3637 """
3738 def __init__ ( self , bound1 , bound2 , base_type ):
38- # init base list with an empty list
39- list .__init__ (self ,[])
4039 # check that bound1<bound2
4140 if (bound1 != None and bound2 != None ):
4241 if bound1 > bound2 :
4342 raise AssertionError ("bound1 shall be less than or equal to bound2" )
4443 self ._bound1 = bound1
4544 self ._bound2 = bound2
4645 self ._base_type = base_type
47- print base_type
48-
46+
4947 def __getitem__ (self , index ):
5048 if index < self ._bound1 :
5149 raise IndexError ("ARRAY index out of bound (lower bound is %i, passed %i)" % (self ._bound1 ,index ))
@@ -63,21 +61,65 @@ def __setitem__(self,index,value):
6361 raise TypeError ("%s type expected, passed %s." % (self ._base_type , type (value )))
6462 else :
6563 # first find the length of the list, and extend it if ever
66- # the index is
64+ # the index is
6765 list .__setitem__ (self ,index ,value )
6866
69- class ARRAY (list , BaseAggregate ):
67+ class ARRAY (object ):
7068 """An array data type has as its domain indexed, fixed-size collections of like elements. The lower
7169 and upper bounds, which are integer-valued expressions, define the range of index values, and
7270 thus the size of each array collection.
7371 An array data type definition may optionally specify
7472 that an array value cannot contain duplicate elements.
7573 It may also specify that an array value
7674 need not contain an element at every index position.
75+
76+ Given that m is the lower bound and n is the upper bound, there are exactly n-m+1 elements
77+ in the array. These elements are indexed by subscripts from m to n, inclusive (see 12.6.1).
78+ NOTE 1 { The bounds may be positive, negative or zero, but may not be indeterminate (?) (see
79+ 14.2).
7780 """
78- def __init__ ( self , bound1 , bound2 , base_type ):
79- BaseAggregate .__init__ ( self , bound1 , bound2 , base_type )
81+ def __init__ ( self , bound_1 , bound_2 , base_type , UNIQUE = False , OPTIONAL = False ):
82+ if not type (bound_1 )== int :
83+ raise TypeError ("ARRAY lower bound must be an integer" )
84+ if not type (bound_2 )== int :
85+ raise TypeError ("ARRAY upper bound must be an integer" )
86+ if not (bound_1 <= bound_2 ):
87+ raise AssertionError ("ARRAY lower bound must be less than or equal to upper bound" )
88+ # set up class attributes
89+ self ._bound_1 = bound_1
90+ self ._bound_2 = bound_2
91+ self ._base_type = base_type
92+ self ._unique = UNIQUE
93+ self ._optional = OPTIONAL
94+ # preallocate list elements
95+ list_size = bound_2 - bound_1 + 1
96+ self ._container = list_size * [None ]
97+
98+ def __getitem__ (self , index ):
99+ if index < self ._bound_1 :
100+ raise IndexError ("ARRAY index out of bound (lower bound is %i, passed %i)" % (self ._bound_1 ,index ))
101+ elif (index > self ._bound_2 ):
102+ raise IndexError ("ARRAY index out of bound (upper bound is %i, passed %i)" % (self ._bound_2 ,index ))
103+ else :
104+ value = self ._container [index - self ._bound_1 ]
105+ if not self ._optional and value == None :
106+ raise AssertionError ("Not OPTIONAL prevent the value with index %i from being None (default). Please set the value first." % index )
107+ return value
80108
109+ def __setitem__ (self , index , value ):
110+ if index < self ._bound_1 :
111+ raise IndexError ("ARRAY index out of bound (lower bound is %i, passed %i)" % (self ._bound_1 ,index ))
112+ elif (index > self ._bound_2 ):
113+ raise IndexError ("ARRAY index out of bound (upper bound is %i, passed %i)" % (self ._bound_2 ,index ))
114+ else :
115+ # first check the type of the value
116+ check_type (value ,self ._base_type )
117+ # then check if the value is already in the array
118+ if self ._unique :
119+ if value in self ._container :
120+ raise AssertionError ("UNIQUE keyword prevent inserting this instance." )
121+ self ._container [index - self ._bound_1 ] = value
122+
81123class LIST (list , BaseAggregate ):
82124 """A list data type has as its domain sequences of like elements. The optional lower and upper
83125 bounds, which are integer-valued expressions, dfine the minimum and maximum number of
0 commit comments