view modules/dict.tp @ 248:96fdc5b37ceb

Added get:else method to linear dict
author Michael Pavone <pavone@retrodev.com>
date Sun, 30 Mar 2014 18:17:56 -0700
parents cea671c4056c
children c58e17f5c0f6
line wrap: on
line source
{
	linearWithEls <- :els {
		key:val <- :k v {
			#{
				key <- k
				val <- v
			}
		}
		find <- :tofind {
			idx <- 0
			while: {
				if: idx < (els length) {
					((els get: idx) key: ) != tofind
				} else: {false}
			} do: {
				idx <- idx + 1
			}
			if: idx < (els length) {idx} else: {-1}
		}
		#{
			set <- :k v {
				idx <- find: k
				if: idx < 0 {
					els append: (key: k val: v)
				} else: {
					(els get: idx) val!: v
				}
				self
			}

			get <- :k {
				get: k withDefault: false
			}

			get:withDefault <- :k default {
				idx <- find: k
				if: idx < 0 {
					default
				} else: {
					(els get: idx) val
				}
			}

			get:elseSet <- :k :else {
				get: k else: {
					v <- else:
					els append: (key: k val: v)
					v
				}
			}

			get:else <- :k :else {
				idx <- find: k
				if: idx < 0 {
					else:
				} else: {
					(els get: idx) val
				}
			}

			contains? <- :k {
				(find: k) >= 0
			}

			foreach <- :l {
				foreach: els :idx el {
					l: (el key) (el val)
				}
			}

			map <- :fun {
				newels <- #[]
				foreach: els :idx el {
					newels append: (key: (el key) val: (fun: (el val)))
				}
				linearWithEls: newels
			}

			length <- { els length }
		}
	}
	#{
		//requires only that keys support equality
		linear <- {
			linearWithEls: #[]
		}
	}
}