1-- GunFu Deadlands 2-- Copyright 2009-2011 Christiaan Janssen, September 2009-October 2011 3-- 4-- This file is part of GunFu Deadlands. 5-- 6-- GunFu Deadlands is free software: you can redistribute it and/or modify 7-- it under the terms of the GNU General Public License as published by 8-- the Free Software Foundation, either version 3 of the License, or 9-- (at your option) any later version. 10-- 11-- GunFu Deadlands is distributed in the hope that it will be useful, 12-- but WITHOUT ANY WARRANTY; without even the implied warranty of 13-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14-- GNU General Public License for more details. 15-- 16-- You should have received a copy of the GNU General Public License 17-- along with GunFu Deadlands. If not, see <http://www.gnu.org/licenses/>. 18 19List = {} 20 21function List.newlist(val) 22 return {next = nil, prev=nil, value = val } 23end 24 25 26function List.push(node,val) 27 local last 28 if node then 29 last = node 30 else 31 last = nil 32 end 33 34 node = {next = node, prev= nil, value = val } 35 36 if last then 37 last.prev = node 38 end 39 40 return node 41end 42 43function List.del(list, node) 44 local beginning = list 45 if node == beginning then 46 beginning = beginning.next 47 end 48 if node.prev then 49 node.prev.next = node.next 50 end 51 if node.next then 52 node.next.prev = node.prev 53 end 54 node = nil 55 return beginning 56end 57 58function List.count(beginning) 59 local count = 0 60 local l = beginning 61 while l do 62 count = count + 1 63 l = l.next 64 end 65 return count 66end 67 68-- aplies func(params) to each element 69function List.apply(beginning, func, params) 70 local l = beginning 71 while l do 72 if l.value then 73 if params then 74 func(l.value, params) 75 else 76 func(l.value) 77 end 78 end 79 l = l.next 80 end 81end 82 83-- applies func(params) to each element. If func returns false, element is 84-- deleted, else it is kept 85function List.applydel(beginning, func, params) 86 local l = beginning 87 local newb = beginning 88 while l do 89 if l.value then 90 if params then 91 if not func(l.value, params) then 92 newb = List.del(newb, l) 93 end 94 else 95 if not func(l.value) then 96 newb = List.del(newb, l) 97 end 98 end 99 end 100 l = l.next 101 end 102 return newb 103end 104 105-- applies func(params) to each element until func returns false, then returns true if all the list was processed 106function List.applyfirst(beginning, func, params) 107 local l = beginning 108 while l do 109 if l.value then 110 if params then 111 if not func(l.value, params) then 112 return false 113 end 114 else 115 if not func(l.value) then 116 return false 117 end 118 end 119 end 120 l = l.next 121 end 122 return true 123end 124 125function List.reset(beginning) 126 local l = beginning 127 while l do 128 local n = l.next 129 l = {next = nil, prev= nil, value = nil } 130 l = n 131 end 132 return nil 133end 134 135function List.fromArray(array) 136 local newlist = nil 137 for i,v in ipairs(array) do 138 newlist = List.push(newlist, v) 139 end 140 return newlist 141end 142 143function List.pushToFront(beginning, node) 144 if node == beginning then 145 return beginning 146 end 147 if node.next then 148 node.next.prev = node.prev 149 end 150 if node.prev then 151 node.prev.next = node.next 152 end 153 node.prev = nil 154 node.next = beginning 155 beginning.prev = node 156 157 return node 158end 159