用lua实现STL中vector、list、map相应功能

最近用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

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值