Ignore:
Timestamp:
Mar 30, 2007, 4:22:30 AM (18 years ago)
Author:
dai
Message:

オブジェクトの循環参照を許容した(構造体はダメ)。
抽象クラスをメンバの型に指定できるようにした。
メンバがオブジェクトだったとき、自動的にNewするのをやめ、初期値としてNothingを指定するようにした。

【ArrayListの改良】
・ArrayList_Elementを廃止し、実装をArrayListのprivateに置いた。
・一通りのパラメータを*ObjectからObjectへ変更した。

【その他】
・TypeInfo改良中...
・Objectクラスに実行時型情報用のtypeInfoメンバを追加した。

Location:
Include/Classes/System/Collections
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • Include/Classes/System/Collections/ArrayList.ab

    r188 r195  
    55Class ArrayList
    66    Inherits IList /*, ICollection, IEnumerable, ICloneable*/
     7
     8    pObject As *Object
     9    size As Long
     10    capacity As Long
     11
     12    Sub Init( size As Long )
     13        If size < 0 Then
     14            ' Error
     15            debug
     16        End If
     17
     18        This.size = size
     19        This.capacity = size
     20
     21        pObject = GC_malloc( SizeOf(*Object) * size + 1 )
     22        If pObject = 0 Then
     23            ' OutOfMemoryException
     24            Debug
     25        End If
     26    End Sub
     27
     28    Sub Realloc( size As Long )
     29        If size > capacity Then
     30            ' miss!
     31            Debug
     32        End If
     33
     34        pObject = realloc( pObject, SizeOf(*Object) * size + 1  )
     35        If pObject = 0 Then
     36            ' OutOfMemoryException
     37            Debug
     38        End If
     39    End Sub
     40
     41    Sub SetLeastCapacity( capacity As Long )
     42        If This.capacity < capacity Then
     43            This.capacity = capacity
     44            Realloc( capacity )
     45        End If
     46    End Sub
     47
    748Public
    8     ' --------------------------------
     49
     50    '----------------------------------------------------------------
    951    ' Properties
     52    '----------------------------------------------------------------
     53
    1054    /*Const*/ Virtual Function Capacity() As Long
    11         Return data.Capacity
     55        Return capacity
    1256    End Function
    1357
    1458    Virtual Sub Capacity(c As Long)
    15         If c <> data.Capacity Then
    16             If c < data.Size Then
     59        If c <> capacity Then
     60            If c < size Then
    1761                'Throw ArgumentOutOfRangeException
    1862                Debug
    1963            Else
    20                 reallocBuffer(c)
     64                Realloc(c)
    2165            End If
    2266        End If
     
    2468
    2569    /*Const*/ /*Override*/ Virtual Function Count() As Long
    26         Return data.Size
     70        Return size
    2771    End Function
    2872
     
    4185
    4286    ' Operators
    43     /*Const*/ /*Override*/ Virtual Function Operator [](i As Long) As *Object
    44         If 0 <= i And i < data.Size Then
    45             Return data.Elm[i]
     87    /*Const*/ /*Override*/ Virtual Function Operator [](index As Long) As Object
     88        If 0 <= index And index < size Then
     89            Return pObject[index]
    4690        Else
    4791            'Throw OutOfRangeException?
     
    5094    End Function
    5195   
    52     /*Override*/ Virtual Sub Operator []=(i As Long, x As *Object)
    53         If 0 <= i And i < data.Size Then
    54             data.Elm[i] = 0
     96    /*Override*/ Virtual Sub Operator []=(index As Long, object As Object)
     97        If 0 <= index And index < size Then
     98            pObject[i] = object
    5599        Else
    56100            'Throw OutOfRangeException?
     
    59103    End Sub
    60104
    61     ' --------------------------------
     105
     106
     107    '----------------------------------------------------------------
    62108    ' Methods
     109    '----------------------------------------------------------------
     110
    63111    '   Constractors
    64112    Sub ArrayList()
    65         data.Init(16)
     113        Init( 0 )
    66114    End Sub
    67115/*
     
    72120*/
    73121    Sub ArrayList(c As Long)
    74         data.Init(c)
     122        Init( c )
    75123    End Sub
    76124
     
    81129'   Sub Operator =(ByRef a As ArrayList)
    82130
    83     /*Override*/ Virtual Function Add(x As *Object) As Long
    84         setLeastCapacity(data.Size + 1)
    85         data.Elm[data.Size] = x
    86         Add = data.Size
    87         data.Size++
    88     End Function
    89 
    90     /*Override*/ Virtual Function Add(x As Object) As Long
    91         setLeastCapacity(data.Size + 1)
    92         data.Elm[data.Size] = VarPtr(x)
    93         Add = data.Size
    94         data.Size++
     131    /*Override*/ Virtual Function Add( object As Object ) As Long
     132        SetLeastCapacity( size + 1 )
     133        pObject[size] = object
     134        size++
     135        Return size - 1
    95136    End Function
    96137
     
    112153
    113154    /*Override*/ Virtual Sub Clear()
    114         data.Size = 0
    115     End Sub
    116 
    117     Virtual Function Clone() As *ArrayList
    118         Dim p As *ArrayList
    119         p = New ArrayList(data.Size)
    120         p->data.Size = data.Size
    121         memcpy(p->data.Elm, This.data.Elm, SizeOf (*Object) * data.Size)
    122         Return p
     155        size = 0
     156    End Sub
     157
     158    Virtual Function Clone() As ArrayList
     159        Dim arrayList = New ArrayList( size )
     160        memcpy( arrayList.pObject, This.pObject, SizeOf(*Object) * size )
     161        Return arrayList
    123162    End Function
    124163
    125164    /*Const*/ /*Override*/ /*Virtual Function Contains(x As *Object) As Boolean
    126165        Dim i As Long
    127         For i = 0 To ELM(data.Size)
     166        For i = 0 To ELM(size)
    128167            If x->Equals(data.Elm[i]) Then
    129168                Return True
     
    145184    End Function
    146185
    147     /*Const*/ Override Function IndexOf(x As *Object) As Long
    148         Return IndexOf(x, 0, data.Size)
    149     End Function
    150 
    151     /*Const*/ Virtual Function IndexOf(x As *Object, startIndex As Long) As Long
    152         Return IndexOf(x, startIndex, data.Size - startIndex)
    153     End Function
    154 
    155     /*Const*/ Virtual Function IndexOf(x As *Object, startIndex As Long, count As Long) As Long
     186    /*Const*/ Override Function IndexOf(object As Object) As Long
     187        Return IndexOf(object, 0, size)
     188    End Function
     189
     190    /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long) As Long
     191        Return IndexOf(object, startIndex, size - startIndex)
     192    End Function
     193
     194    /*Const*/ Virtual Function IndexOf(object As Object, startIndex As Long, count As Long) As Long
    156195        Dim i As Long
    157         Dim last = Math.Min(startIndex + count - 1, data.Size)
     196        Dim last = Math.Min(startIndex + count - 1, size)
    158197        For i = startIndex To last
    159             If x->Equals(ByVal data.Elm[i]) Then
     198            If object.Equals( pObject[i] ) Then
    160199                Return i
    161200            End If
    162201        Next
    163     End Function
    164 
    165     Override Sub Insert(i As Long, x As *Object)
    166         setLeastCapacity(data.Size + 1)
    167         memmove(VarPtr(data.Elm[i + 1]), VarPtr(data.Elm[i]), SizeOf (*Object) * (data.Size - i))
    168         data.Elm[i] = x
    169         data.Size++
     202        Return -1
     203    End Function
     204
     205    Override Sub Insert(i As Long, object As Object)
     206        SetLeastCapacity(size + 1)
     207        memmove(VarPtr(pObject[i + 1]), VarPtr(pObject[i]), SizeOf (*Object) * (size - i))
     208        pObject[i] = object
     209        size++
    170210    End Sub
    171211
     
    174214    End Sub
    175215
    176     /*Const*/ Virtual Function LastIndexOf(x As *Object) As Long
    177         LastIndexOf(x, 0, data.Size)
    178     End Function
    179 
    180     /*Const*/ Virtual Function LastIndexOf(x As *Object, startIndex As Long) As Long
    181         Return LastIndexOf(x, startIndex, data.Size - startIndex)
    182     End Function
    183 
    184     /*Const*/ Virtual Function LastIndexOf(x As *Object, startIndex As Long, count As Long) As Long
    185         ' TODO: 実装
    186     End Function
    187 
    188     Override Sub Remove(x As *Object)
    189         Dim i = IndexOf(x)
     216    /*Const*/ Virtual Function LastIndexOf(object As Object) As Long
     217        LastIndexOf(object, 0, size)
     218    End Function
     219
     220    /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long) As Long
     221        Return LastIndexOf(object, startIndex, size - startIndex)
     222    End Function
     223
     224    /*Const*/ Virtual Function LastIndexOf(object As Object, startIndex As Long, count As Long) As Long
     225        ' TODO: 実装
     226    End Function
     227
     228    Override Sub Remove(object As Object)
     229        Dim i = IndexOf(object)
    190230        If i > 0 Then
    191231            RemoveAt(i)
     232            size--
    192233        End If
    193234    End Sub
     
    195236    Override Sub RemoveAt(i As Long)
    196237        RemoveRange(i, 1)
     238        size--
    197239    End Sub
    198240   
    199241    Virtual Sub RemoveRange(i As Long, count As Long)
    200242        Dim removeLastPos = i + count
    201         memmove(VarPtr(data.Elm[i]), VarPtr(data.Elm[removeLastPos]), SizeOf (*Object) * (data.Size - removeLastPos))
     243        memmove(VarPtr(pObject[i]), VarPtr(pObject[removeLastPos]), SizeOf(*Object) * (size - removeLastPos))
     244        size -= count
    202245    End Sub
    203246
    204247    Virtual Sub Reverse()
    205         Reverse(0, data.Size)
     248        Reverse(0, size)
    206249    End Sub
    207250
     
    219262
    220263    Virtual Sub Sort(ByRef c As IComparer)
    221         Sort(0, data.Size, c)
     264        Sort(0, size, c)
    222265    End Sub
    223266
     
    228271    ' ToArray
    229272    Virtual Sub TrimToSize()
    230         reallocBuffer(data.Size)
     273        Realloc(size)
    231274    End Sub
    232275
     
    273316    End Function
    274317
    275 Private
    276     Sub setLeastCapacity(c As Long)
    277         If data.Capacity < c Then
    278             reallocBuffer(c)
    279         End If
    280     End Sub
    281 
    282     Sub reallocBuffer(c As Long)
    283         Dim newBuf As ArrayList_Element(c)
    284         newBuf.Size = data.Size
    285         memcpy(newBuf.Elm, data.Elm, SizeOf (*Object) * newBuf.Size)
    286         newBuf.Swap(data)
    287     End Sub
    288 
    289     data As ArrayList_Element
    290318End Class
    291319
     320/*
    292321Class ArrayList_Element
    293322Public
     
    335364    Capacity As Long
    336365End Class
     366*/
  • Include/Classes/System/Collections/misc.ab

    r118 r195  
    6969    'Sub Clear()
    7070    '/*Const*/ Function Contains(value As *Object) As Boolean
    71     /*Const*/ Function IndexOf(value As *Object) As Long
    72     Sub Insert(index As Long, value As *Object)
    73     Sub Remove(value As *Object)
     71    /*Const*/ Function IndexOf(value As Object) As Long
     72    Sub Insert(index As Long, value As Object)
     73    Sub Remove(value As Object)
    7474    Sub RemoveAt(index As Long)
    7575    ' Properties
Note: See TracChangeset for help on using the changeset viewer.