robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
c3836e40a2
master
1-- a set class for fast union/diff, can always return a table with the lines 2-- in the same relative order in which they were added by calling the 3-- to_table method. It does this by keeping two lua tables that mirror each 4-- other: 5-- 1) index => item 6-- 2) item => index 7--- @class Set 8--- @field nelem integer 9--- @field items string[] 10--- @field tbl table 11local Set ={} 12 13--- @param items? string[] 14function Set : new ( items ) 15local obj ={} --- @type Set 16setmetatable ( obj , self ) 17self . __index =self 18 19if type ( items ) == 'table' then 20local tempset =Set : new () 21tempset : union_table ( items ) 22obj . tbl =tempset : raw_tbl () 23obj . items =tempset : raw_items () 24obj . nelem =tempset : size () 25else 26obj . tbl ={} 27obj . items ={} 28obj . nelem =0 29end 30 31return obj 32end 33 34--- @return Set 35function Set : copy () 36local obj ={ nelem =self . nelem , tbl ={} , items ={} } --- @type Set 37for k , v in pairs ( self . tbl ) do 38obj . tbl [ k ] =v 39end 40for k , v in pairs ( self . items ) do 41obj . items [ k ] =v 42end 43setmetatable ( obj , Set ) 44obj . __index =Set 45return obj 46end 47 48-- adds the argument Set to this Set 49--- @param other Set 50function Set : union ( other ) 51for e in other : iterator () do 52self : add ( e ) 53end 54end 55 56-- adds the argument table to this Set 57function Set : union_table ( t ) 58for _ , v in pairs ( t ) do 59self : add ( v ) 60end 61end 62 63-- subtracts the argument Set from this Set 64--- @param other Set 65function Set : diff ( other ) 66if other : size () > self : size () then 67-- this set is smaller than the other set 68for e in self : iterator () do 69if other : contains ( e ) then 70self : remove ( e ) 71end 72end 73else 74-- this set is larger than the other set 75for e in other : iterator () do 76if self . items [ e ] then 77self : remove ( e ) 78end 79end 80end 81end 82 83--- @param it string 84function Set : add ( it ) 85if not self : contains ( it ) then 86local idx =# self . tbl + 1 87self . tbl [ idx ] =it 88self . items [ it ] =idx 89self . nelem =self . nelem + 1 90end 91end 92 93--- @param it string 94function Set : remove ( it ) 95if self : contains ( it ) then 96local idx =self . items [ it ] 97self . tbl [ idx ] =nil 98self . items [ it ] =nil 99self . nelem =self . nelem - 1 100end 101end 102 103--- @param it string 104--- @return boolean 105function Set : contains ( it ) 106return self . items [ it ] or false 107end 108 109--- @return integer 110function Set : size () 111return self . nelem 112end 113 114function Set : raw_tbl () 115return self . tbl 116end 117 118function Set : raw_items () 119return self . items 120end 121 122function Set : iterator () 123return pairs ( self . items ) 124end 125 126--- @return string[] 127function Set : to_table () 128-- there might be gaps in @tbl, so we have to be careful and sort first 129local keys ={} --- @type string[] 130for idx , _ in pairs ( self . tbl ) do 131keys [ # keys + 1 ] =idx 132end 133 134table . sort ( keys ) 135local copy ={} --- @type string[] 136for _ , idx in ipairs ( keys ) do 137copy [ # copy + 1 ] =self . tbl [ idx ] 138end 139return copy 140end 141 142return Set