annotate modules/array.tp @ 249:fd9005253861

Added resize method to arrays
author Michael Pavone <pavone@retrodev.com>
date Wed, 09 Apr 2014 22:54:52 -0700
parents abde5d2918cf
children 56409de95f55
rev   line source
pavone@84 1 #{
pavone@84 2 llProperty: size withType: uint32_t
pavone@84 3 llProperty: storage withType: uint32_t
pavone@84 4 llProperty: data withType: ((object ptr) ptr)
pavone@84 5 llMessage: get withVars: {
pavone@84 6 index <- obj_int32 ptr
pavone@84 7 } andCode: :index {
pavone@84 8 if: (index num) >= 0 && (index num) < size {
pavone@84 9 (self data) get: (index num)
pavone@84 10 } else: {
pavone@84 11 false
pavone@84 12 }
pavone@84 13 }
pavone@144 14
pavone@84 15 llMessage: set withVars: {
pavone@84 16 index <- obj_int32 ptr
pavone@84 17 value <- object ptr
pavone@84 18 } andCode: :index value {
pavone@84 19 if: (index num) >= 0 && (index num) < size {
pavone@84 20 data set: (index num) value
pavone@84 21 }
pavone@84 22 self
pavone@84 23 }
pavone@144 24
pavone@84 25 llMessage: foreach withVars: {
pavone@84 26 clos <- lambda ptr
pavone@84 27 i <- uint32_t
pavone@84 28 index <- obj_int32 ptr
pavone@84 29 } andCode: :clos {
pavone@84 30 i <- 0
pavone@84 31 while: { i < size } do: {
pavone@84 32 index <- make_object: (addr_of: obj_int32_meta) NULL 0
pavone@84 33 index num!: i
pavone@84 34 ccall: clos 2 index (data get: i)
pavone@84 35 i <- i + 1
pavone@84 36 }
pavone@84 37 self
pavone@84 38 }
pavone@144 39
pavone@84 40 llMessage: append withVars: {
pavone@84 41 value <- object ptr
pavone@84 42 tmp <- (object ptr) ptr
pavone@84 43 } andCode: :value {
pavone@84 44 if: storage = size {
pavone@84 45 storage <- storage * 2
pavone@84 46 tmp <- GC_REALLOC: data storage * (sizeof: (object ptr))
pavone@84 47 if: (not: tmp) {
pavone@84 48 fputs: "Failed to increase array size\n" stderr
pavone@84 49 exit: 1
pavone@84 50 }
pavone@84 51 data <- tmp
pavone@84 52 }
pavone@84 53 data set: size value
pavone@84 54 size <- size + 1
pavone@84 55 self
pavone@84 56 }
pavone@144 57
pavone@249 58 llMessage: resize withVars: {
pavone@249 59 newsize <- obj_uint32 ptr
pavone@249 60 tmp <- (object ptr) ptr
pavone@249 61 } andCode: :newsize {
pavone@249 62 self storage!: (newsize num)
pavone@249 63 tmp <- GC_REALLOC: data storage * (sizeof: (object ptr))
pavone@249 64 if: (not: tmp) {
pavone@249 65 fputs: "Failed to adjust array size\n" stderr
pavone@249 66 exit: 1
pavone@249 67 }
pavone@249 68 data <- tmp
pavone@249 69 if: size > storage {
pavone@249 70 size <- storage
pavone@249 71 }
pavone@249 72 self
pavone@249 73 }
pavone@249 74
pavone@84 75 llMessage: length withVars: {
pavone@84 76 intret <- obj_int32 ptr
pavone@84 77 } andCode: {
pavone@84 78 intret <- make_object: (addr_of: obj_int32_meta) NULL 0
pavone@84 79 intret num!: size
pavone@84 80 intret
pavone@84 81 }
pavone@144 82
pavone@84 83 fold:with <- :acc :fun {
pavone@84 84 foreach: self :idx el {
pavone@144 85 acc <- fun: acc el
pavone@84 86 }
pavone@144 87 acc
pavone@84 88 }
pavone@144 89
pavone@84 90 foldr:with <- :acc :fun {
pavone@84 91 idx <- length - 1
pavone@84 92 while: {idx >= 0} do: {
pavone@144 93 acc <- fun: acc (get: idx)
pavone@84 94 }
pavone@144 95 acc
pavone@84 96 }
pavone@144 97
pavone@84 98 map <- :fun {
pavone@84 99 new <- #[]
pavone@84 100 foreach: self :idx el {
pavone@84 101 new append: (fun: el)
pavone@84 102 }
pavone@144 103 new
pavone@84 104 }
pavone@144 105
pavone@84 106 find:withDefault <- :pred :default{
pavone@84 107 idx <- 0
pavone@84 108 l <- length
pavone@84 109 ret <- default
pavone@84 110 while: {idx < l} do: {
pavone@84 111 v <- get: idx
pavone@84 112 if: (pred: v) {
pavone@84 113 ret <- #{
pavone@84 114 key <- idx
pavone@84 115 value <- v
pavone@84 116 }
pavone@84 117 idx <- l
pavone@84 118 }
pavone@84 119 }
pavone@84 120 ret
pavone@84 121 }
pavone@184 122
pavone@184 123 join <- :sep {
pavone@184 124 if: length > 0 {
pavone@186 125 str <- string: (get: 0)
pavone@184 126 idx <- 1
pavone@184 127 l <- length
pavone@184 128 while: { idx < l } do: {
pavone@184 129 str <- str . sep . (get: idx)
pavone@191 130 idx <- idx + 1
pavone@184 131 }
pavone@184 132 str
pavone@184 133 } else: {
pavone@184 134 ""
pavone@184 135 }
pavone@184 136 }
pavone@84 137 }