local f=require("\112\108\046\117\116\105\108\115")local A=require("\112\108\046\099\108\097\115\115")local Z=require("\112\108\046\076\105\115\116")local C=require("\112\108\046\077\097\112")local O=f.stdmt.MultiMap A(C,nil,O)O._name="\077\117\108\116\105\077\097\112"function O._init(A,f)if not f then return end A:update(f)end function O.update(O,A)f.assert_arg(1,A,"\116\097\098\108\101")if C:class_of(A)then for f,A in pairs(A)do O[f]=Z()O[f]:append(A)end else for f,A in pairs(A)do O[f]=Z(A)end end end function O.set(C,f,A)if A==nil then C[f]=nil else if not C[f]then C[f]=Z()end C[f]:append(A)end end return O
--- MultiMap, a Map which has multiple values per key.
--
-- Dependencies: `pl.utils`, `pl.class`, `pl.List`, `pl.Map`
-- @classmod pl.MultiMap

local utils = require 'pl.utils'
local class = require 'pl.class'
local List = require 'pl.List'
local Map = require 'pl.Map'

-- MultiMap is a standard MT
local MultiMap = utils.stdmt.MultiMap

class(Map,nil,MultiMap)
MultiMap._name = 'MultiMap'

function MultiMap:_init (t)
    if not t then return end
    self:update(t)
end

--- update a MultiMap using a table.
-- @param t either a Multimap or a map-like table.
-- @return the map
function MultiMap:update (t)
    utils.assert_arg(1,t,'table')
    if Map:class_of(t) then
        for k,v in pairs(t) do
            self[k] = List()
            self[k]:append(v)
        end
    else
        for k,v in pairs(t) do
            self[k] = List(v)
        end
    end
end

--- add a new value to a key.  Setting a nil value removes the key.
-- @param key the key
-- @param val the value
-- @return the map
function MultiMap:set (key,val)
    if val == nil then
        self[key] = nil
    else
        if not self[key] then
            self[key] = List()
        end
        self[key]:append(val)
    end
end

return MultiMap
