最近用lua模拟实现vector、list、map相应部分功能,在这里做下记录
-
vector
vector = {}
vector.__index = vector;
--构造函数
function vector.new()
local ret = {}
setmetatable(ret, vector)
ret.tempVec = {}
return ret
end
function vector:at(index)
return self.tempVec[index]
end
function vector:push_back(val)
table.insert(self.tempVec,val)
end
function vector:erase(index)
table.remove(self.tempVec,index)
end
function vector:size()
return #self.tempVec;
end
function vector:iter()
local i = 0
return function ()
i = i + 1
return self.tempVec[i]
end
end
-
list
node = {}
node.__index = node
function node.new(data,prev,next)
local o = {
o.data = data,
o.prev = prev,
o.next = next,
}
setmetatable(o,node)
return o
end
list = {}
list.__index = list
function list.new()--创建List对象
local o = {
o.first = nil,
o.last = nil,
o.count = 0,
}
setmetatable(o, list)
return o
end
function list:empty()
return self.count == 0
end
function list:push_back(data)
local node = node.new(data)
if self.first == nil then
self.first = node
self.last = node
self.count = 1
else
self.last.next = node
node.prev = self.last
self.last = node
self.count = self.count + 1
end
end
function list:push_front(data)
local node = node.new(data)
if self.last == nil then
self.first = node
self.last = node
self.count = 1
else
node.next = self.first
self.first.prev = node
self.first = node
self.count = self.count + 1
end
end
function list:pop_back()
assert(not self:empty(),"list is empty")
if self.count == 1 then
self.first = nil
self.last = nil
self.count = 0
else
local node = self.last
self.last = node.prev
self.last.next = nil
self.count = self.count - 1
end
end
function list:pop_front()
assert(not self:empty(),"list is empty")
if self.count == 1 then
self.first = nil
self.last = nil
self.count = 0
else
self.first = self.first.next
self.first.prev = nil
self.count = self.count - 1
end
end
function list:size()
return self.count
end
function list:clear()
assert(not self:empty(),"list is empty")
while not self:empty() do
self:pop_back()
end
end
function list:front()
assert(not self:empty(),"list is empty")
return self.first.data
end
function list:back()
assert(not self:empty(),"list is empty")
return self.last.data
end
function list:iter()
assert(not self:empty(),"list is empty")
local node = self.first
return function ()
local b = node
if node then
node = node.next
end
return b
end
end
-
map
map = {}
map.__index = map
map.count = 0
function map.new()
local ret = {}
setmetatable(ret,map)
ret.tempVec = {}
return ret
end
function map:insert(k,v)
if nil == self.tempVec[k] then
self.tempVec[k] = v
self.count = self.count + 1
else
self.tempVec[k] = v
end
end
function map:erase(k)
if nil ~= self.tempVec[k] then
self.tempVec[k] = nil
self.count = self.count - 1
end
end
function map:size()
return self.count
end
function map:find(k)
return self.tempVec[k]
end
function map:clear()
for k,_ in pairs(self.tempVec) do
self.tempVec[k] = nil
end
self.count = 0
end
function map:begin()
return next(self.tempVec, nil);
end
function map:iter()
local i = 0
local key = nil
local value = nil
return function ()
key, value = next(self.tempVec, key);
if key ~= nil then
return key, value
else
return nil, nil
end
end
end