/* filename: mylib_dict.mac contents: private library on functions/procedures to handle dict 2022/12/8: created by Hideo Kodama 2022/12/15: version 1.00 Last update: 2022/12/15 */ /******> top */ /*****> Dictionary-type variable and functions */ /* X :: dict <=> X = [[ [a]= [x,...], ..., [m]=[z,...] ]] */ /****> proc: checkdict */ /* checkdict(X) => true/false */ checkdict(X) := block ([x,result], if listp(X) and length(X)=1 and listp(X[1]) then ( result: true, for i:1 thru length(X[1]) do ( x : X[1][i], if ( (op(x)#"=") or (not listp(args(x)[1])) or (not listp(args(x)[2])) ) then ( result: [i, false], break ) /*end if*/ ) /*end do*/ ) else ( result : [0, false] ), /*end if*/ return (result) )$ /*end proc: checkdict*/ /****>proc: dictp */ dictp(X) := block ([x, result], if listp(X) and length(X)=1 and listp(X[1]) then ( result: true, for i:1 thru length(X[1]) do ( x : X[1][i], if ( (op(x)#"=") or (not listp(args(x)[1])) or (not listp(args(x)[2])) ) then ( result: false, break ) /*end if*/ ) /*end do*/ ) else ( result : false ), /*end if*/ return (result) )$ /*end proc: dictp*/ /****> proc: searchdict */ /* searchdict( X::dict, [tag], p) = true => p[1] =[i, [tag]=value ] false => p[1]=[] p becomes a global array var i = position index in the list X */ searchdict (X, a, p) := block( [ foundx ], apply(remarray, [p]), arraysetapply(p, [1], []), foundx : false, for i:1 thru length(X[1]) do ( if args(X[1][i])[1]=a then ( foundx : true, arraysetapply(p, [1] , [i, a=args(X[1][i])[2] ]), break ) /*end if*/ ), /*end do*/ return (foundx) )$ /*end proc: searchdict */ /****> proc: dicttaglist */ /* dicttaglist(X::dict) => output = [ a, b, ,...] for X=[[ a=x, b=y, ...]] */ dicttaglist (X ) := block([tags], tags : map(lambda([x], args(x)[1]), X[1]), return (tags) )$ /*end proc: dicttaglist*/ /****> proc: newdict */ /* X : newdict() => dict [[]] newdict(item1,...,itemn) => dict [[item1, ..., itemn]] */ newdict([opt]) := block( if length(opt)=0 then ( return([[]]) ) elseif dictp([opt]) then ( return ([opt]) ) else ( error("Invalid dict data") ) /*end if*/ )$ /*end proc: newdict */ /****> proc: showdict */ /* showdict( X::dict [, opt]) => output=[ a=x, c=u, ...] opt = all / tag list [a, c, ..] */ showdict (X, [opt]) := block( [range,outlist, y], if ( length(opt)=0 or opt[1] = all ) then ( outlist: X[1] ) elseif ( listp(opt[1]) ) then ( outlist: [], for a in opt[1] do ( if searchdict(X, a, y) then ( /* searchdict => y[1]= [ i, a= x] */ outlist : endcons( y[1][2], outlist), remarray(y) ) /*end if*/ ) /*end do*/ ), /*end if*/ return( outlist ) )$ /*end proc: showdict */ /****>proc: getdict */ /* getdict(X, a) => x if a=x is in X , false otherwise */ getdict(X, a) := block( [result], local(result), if searchdict(X, a, result) then ( return ( args(result[1][2])[2]) ) else ( return ([]) ) /*end if */ )$ /*end proc: getdict */ /****>proc: rmdict */ /* rmdict(X, item) => X ->new dict: X -item item : [tag]=[val] */ rmdict(X, item) := block([p, newdata, x], local(p), if searchdict(X, args(item)[1], p) then ( newdata: [], for x in args(p[1][2])[2] do ( if not member (x, args(item)[2]) then newdata : append(newdata, [x]) ) /*end if*/ ), /*end do*/ if length(newdata)>0 then ( X[1][p[1][1]] : args(item)[1] = newdata ) else ( X[1] : append(slist(X[1], 1, p[1][1]-1), slist(X[1], p[1][1]+1, length(X[1])) ) /*end if*/ ) /*end if*/ )$ /*end proc: rmdict*/ /****> proc: adddict */ /* adddict(X, item) => X -> new dict: X + data; item : [tag]= [data] */ adddict (X, item) := block ([p, tmp],local(p), if searchdict(X, args(item)[1], p) then ( tmp : append( args(X[1][p[1][1]])[2] , args(item)[2]), X[1][p[1][1]] : args(item)[1] = tmp ) else ( X[1] : append(X[1], [item]) ) /*end if*/ )$ /*end proc: adddict*/ /****> proc: addsdict */ /* addsdict(X, itemlist) => X -> new dict: X + itemlist */ addsdict (X, itemlist) := block ( for x in itemlist do ( adddict(X, x) )/*end do*/ )$ /*end proc: addsdict*/ /****>proc: repldict */ /* repldict(X, [tag]=[data]) => X[tag] => [data] */ repldict(X, item) := block ([ p], if searchdict(X, args(item)[1], p) then ( rmdict(X, X[1][p[1][1]]), adddict(X, item) ) else ( adddict(X, item) ) /*end if*/ )$ /*end proc: repldict*/ /******>End */ print("loading mylib_dict.mac --- done");