Advertisement
Guest User

Untitled

a guest
May 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'AuList.bi
  2. '12/21/2017
  3.  
  4. #IFNDEF _AULIST_BI_
  5. #DEFINE _AULIST_BI_
  6.  
  7. #include once "crt.bi"
  8.  
  9. #DEFINE LISTALLOCSIZE 32
  10.  
  11. nameSpace AuLib
  12.     #MACRO DeclareList(_T)
  13.     type _T##List
  14.         as uinteger allocated, count
  15.         as _T ptr item
  16.        
  17.         declare constructor()
  18.         declare sub allocate()
  19.         declare sub deallocate()
  20.         declare sub add(newItem as _T)
  21.         declare sub remove(index as uinteger)
  22.         declare function length() as uinteger
  23.         declare function swapItem(srcIndex as uinteger, dstIndex as uinteger) as boolean
  24.     end type
  25.    
  26.     constructor _T##List
  27.         item = new _T[LISTALLOCSIZE]
  28.         allocated = LISTALLOCSIZE
  29.     end constructor
  30.    
  31.     sub _T##List.allocate()
  32.         allocated+=LISTALLOCSIZE
  33.         dim as _T ptr temp = new _T[allocated]
  34.         memmove(temp, item, (allocated-LISTALLOCSIZE)*sizeof(_T))
  35.         delete[] item
  36.         item = temp
  37.     end sub
  38.    
  39.     sub _T##List.deallocate()
  40.         allocated-=LISTALLOCSIZE
  41.         dim as _T ptr temp = new _T[allocated]
  42.         memmove(temp, item, (allocated-LISTALLOCSIZE)*sizeof(_T))
  43.         delete[] item
  44.         item = temp
  45.     end sub
  46.    
  47.     sub _T##List.add(newItem as _T)
  48.         if(count >= allocated) then this.allocate()
  49.         count+=1
  50.         item[count-1] = newItem
  51.     end sub
  52.    
  53.     sub _T##List.remove(index as uinteger)
  54.         if(count = 0 OR index > count) then return
  55.         memmove(@cptr(_T ptr,item)[index], @cptr(_T ptr,item)[index+1], sizeof(_T)*(count-index-1))
  56.         count-=1
  57.         if(count+(LISTALLOCSIZE\2)) < (allocated-LISTALLOCSIZE) then this.deallocate()
  58.     end sub
  59.    
  60.     function _T##List.length() as uinteger
  61.         return this.count
  62.     end function
  63.    
  64.     function _T##List.swapItem(srcIndex as uinteger, dstIndex as uinteger) as boolean
  65.         dim as boolean result = false
  66.         if(dstIndex <= this.count-1 AND srcIndex <= this.count-1) then
  67.             swap this.item[srcIndex], this.item[dstIndex]
  68.             result = true
  69.         end if
  70.         return result
  71.     end function
  72.     #ENDMACRO
  73.    
  74.     #MACRO forEach(_V, _L)
  75.     for i as uinteger = 1 to _L##.count
  76.         dim as typeof(*_L##.item) _V = _L##.item[i-1]
  77.     #ENDMACRO
  78. end nameSpace
  79.  
  80. #ENDIF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement