SHARE
TWEET

Untitled

a guest Nov 26th, 2019 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include once "crt.bi"
  2.  
  3. #DEFINE LISTALLOCSIZE 32
  4.  
  5. #MACRO DeclareList(_T)
  6. type _T##List
  7.     as uinteger allocated, count
  8.     as _T ptr item
  9.    
  10.     declare constructor()
  11.     declare sub allocate()
  12.     declare sub deallocate()
  13.     declare sub add(newItem as _T)
  14.     declare sub remove(index as uinteger)
  15.     declare function length() as uinteger
  16.     declare function swapItem(srcIndex as uinteger, dstIndex as uinteger) as boolean
  17. end type
  18.  
  19. constructor _T##List
  20.     item = new _T[LISTALLOCSIZE]
  21.     allocated = LISTALLOCSIZE
  22. end constructor
  23.  
  24. sub _T##List.allocate()
  25.     allocated+=LISTALLOCSIZE
  26.     dim as _T ptr temp = new _T[allocated]
  27.     memmove(temp, item, (allocated-LISTALLOCSIZE)*sizeof(_T))
  28.     delete[] item
  29.     item = temp
  30. end sub
  31.  
  32. sub _T##List.deallocate()
  33.     allocated-=LISTALLOCSIZE
  34.     dim as _T ptr temp = new _T[allocated]
  35.     memmove(temp, item, (allocated-LISTALLOCSIZE)*sizeof(_T))
  36.     delete[] item
  37.     item = temp
  38. end sub
  39.  
  40. sub _T##List.add(newItem as _T)
  41.     if(count >= allocated) then this.allocate()
  42.     count+=1
  43.     item[count-1] = newItem
  44. end sub
  45.  
  46. sub _T##List.remove(index as uinteger)
  47.     if(count = 0 OR index > count) then return
  48.     memmove(@cptr(_T ptr,item)[index], @cptr(_T ptr,item)[index+1], sizeof(_T)*(count-index-1))
  49.     count-=1
  50.     if(count+(LISTALLOCSIZE\2)) < (allocated-LISTALLOCSIZE) then this.deallocate()
  51. end sub
  52.  
  53. function _T##List.length() as uinteger
  54.     return this.count
  55. end function
  56.  
  57. function _T##List.swapItem(srcIndex as uinteger, dstIndex as uinteger) as boolean
  58.     dim as boolean result = false
  59.     if(dstIndex <= this.count-1 AND srcIndex <= this.count-1) then
  60.         swap this.item[srcIndex], this.item[dstIndex]
  61.         result = true
  62.     end if
  63.     return result
  64. end function
  65. #ENDMACRO
  66.  
  67. #MACRO forEach(_V, _L)
  68. for i as uinteger = 1 to _L##.count
  69.     dim as typeof(*_L##.item) _V = _L##.item[i-1]
  70. #ENDMACRO
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top