Changeset 558


Ignore:
Timestamp:
Jul 20, 2008, 10:31:12 PM (13 years ago)
Author:
NoWest
Message:

Queueジェネリッククラスを追加
Listジェネリッククラスの内部でメモリの再確保が頻繁に行われないように改良

Location:
trunk/ab5.0/ablib/src/Classes
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/List.ab

    r557 r558  
    99    items As *T
    1010    size As Long
     11    count As Long
    1112
    1213    currentIndexForEnumerator As Long
     
    3233    Sub List( capacity As Long )
    3334        This.List()
    34         This.Realloc( capacity )
     35        If capacity > 0 Then
     36            This.size = capacity
     37            Realloc(capacity)
     38        End If
    3539    End Sub
    3640
     
    4145    @param  インデックス
    4246    */
    43     Sub List( array As *T, count As Long )
    44         This.List( count )
    45         memmove( This.items, array, count * SizeOf(T) )
    46         This.size = count
    47     End Sub
     47    Sub List( array As *T, length As Long )
     48        This.List( length )
     49        memmove( This.items, array, length * SizeOf(T) )
     50        This.count = length
     51    End Sub
     52
    4853    Sub ~List()
    4954    End Sub
     
    8388    */
    8489    Override Function Count() As Long
    85         Return size
     90        Return This.count
    8691    End Function
    8792
     
    106111    */
    107112    Override Sub Add( item As T )
    108         Realloc( size + 1 )
    109 
    110         items[size] = item
    111         size++
     113        If This.size < (This.count + 1) Then
     114            This.size++
     115            Realloc( This.size )
     116        End If
     117        This.items[ This.count ] = item
     118        This.count++
    112119    End Sub
    113120
     
    120127    */
    121128    Override Sub Clear()
    122         size = 0
     129        This.count = 0
    123130    End Sub
    124131
     
    135142    Override Function IndexOf( item As T ) As Long
    136143        Dim i As Long
    137         For i = 0 To ELM(size)
     144        For i = 0 To ELM( This.count )
    138145            If items[i].Equals(item) Then
    139146                Return i
     
    149156    */
    150157    Override Sub Insert( index As Long, item As T )
    151         Realloc( size + 1 )
    152         memmove( items + (index+1)*SizeOf(T), items + index*SizeOf(T), (size-index)*SizeOf(T) )
     158        If This.size < (This.count + 1) Then
     159            This.size++
     160            Realloc( This.size )
     161        End If
     162        memmove( items + (index+1)*SizeOf(T), items + index*SizeOf(T), (This.count - index)*SizeOf(T) )
    153163        items[index] = item
    154         size++
     164        This.count++
    155165    End Sub
    156166
     
    175185    @author Daisuke Yamamoto
    176186    @date   2007/10/03
    177     @return 値が削除されたときはTrue、されなかったときはFlaseが返る
    178187    */
    179188    Override Sub RemoveAt( index As Long )
    180         memmove( items + index*SizeOf(T), items + (index+1)*SizeOf(T), (size-(index+1))*SizeOf(T) )
    181         Realloc( size - 1 )
    182         size--
     189        memmove( items + index*SizeOf(T), items + (index+1)*SizeOf(T), (This.count - (index+1))*SizeOf(T) )
     190        This.count--
    183191    End Sub
    184192
     
    192200        Dim string As String
    193201        Dim i As Long
    194         For i = 0 To This.Count-2
     202        For i = 0 To ELM( This.count )
    195203            string += This[i].ToString() + Ex"\r\n"
    196204        Next
    197205        Return string + This[i].ToString()
    198206    End Function
     207
     208    /*!
     209    @brief  実際の要素数が現在の容量の 90% 未満の場合は、容量をその数に設定します
     210    @author NoWest
     211    @date   2008/07/20
     212    */
     213    Sub TrimExcess ()
     214        Dim pmemobj = _System_pGC->GetMemoryObjectPtr( items )
     215        If ( This.size ) * 0.9 > This.count Then
     216            This.size = This.count
     217            Realloc( This.size )
     218        End If
     219    End Sub
    199220
    200221    /*!
  • trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/Stack.ab

    r557 r558  
    7272    */
    7373    Function Peek () As T
    74         Peek = This.items[ This.count -1 ]
     74        Peek = This.items[ 0 ]
    7575    End Function
    7676
     
    8181    */
    8282    Function Pop () As T
    83         Pop = This.items[ This.count -1 ]
     83        Pop = This.items[ 0 ]
     84        memmove( items, items + SizeOf(T), (This.count - 1)*SizeOf(T) )
    8485        This.count--
    8586    End Function
     
    9192    */
    9293    Sub Push ( item As T )
    93         This.count++
    94         If This.size < This.count Then
     94        If This.size < (This.count + 1) Then
    9595            This.size++
    9696            Realloc( This.size )
    9797        End If
    98         This.items[ This.count -1 ] = item
     98        memmove( items + SizeOf(T), items, This.count * SizeOf(T) )
     99        items[ 0 ] = item
     100        This.count++
    99101    End Sub
    100102
  • trunk/ab5.0/ablib/src/Classes/index.ab

    r557 r558  
    3030#require "./System/Collections/Generic/Dictionary.ab"
    3131#require "./System/Collections/Generic/List.ab"
     32#require "./System/Collections/Generic/Queue.ab"
    3233#require "./System/Collections/Generic/Stack.ab"
    3334#require "./System/Collections/Generic/KeyNotFoundException.ab"
Note: See TracChangeset for help on using the changeset viewer.