Παρασκευή 2 Ιουνίου 2023

Find set of Differences from two sets of type item

 This is a simple program, which use a type item as enum type, and lists as automatic arrays (tuple). Also we use the current stack of values and the formula array([]) which leave an empty stack of values, and return an array of item converting the stack object to array object. The [] identifier return the stack of values, while leave an empty stack (So we didn't get a pointer to current stack of values, that not allowed from interpreter). The array() functions if get a stack pointer make a new tuple and move all elements from stack object to array object

Look this sample code. Variable k is of type mStiva, use Type$(k) to get the name. Length of k is 3, the we make L as array(k) so length of L is 3 but length of k is 0. To preserve items on k we have to use L=array(stack(k)). Function stack() return a new stack object.

k=stack:=1,2,3
Print Len(k)=3
L=array(k)
Print Len(k)=0
Print Len(L)=3

We can produce a stack item from an array (a tuple), but without reducing items on that array. Always we get shallow copies, so if we have pointers to arrays, these are just copies of these pointers not the arrays as is.

m=stack(L)
Print type$(m)="mStiva"
Print Len(L)=3
Print Len(M)=3


The program:


Module TestDiff {
// Find differnences of two set of items of type item
Enum item {
pig=random(-20, 1000), cat, dog, cow, elephant, mice, horse
}

ListA=(pig, cow, elephant, mice)
ListB=(dog, cat, pig, mice)

k=min.data(len(ListA), len(ListB))
if len(ListB)=k then M1=ListA : M2=ListB else M2=ListA:M1=ListB
M=each(M1)
flush
integer nullitem=pig-1
dim a(pig to horse)=nullitem

While M
i=array(M)
a(i)=i
End While
M=each(M2)
While M
i=array(M)
if a(i)=i then a(i)=nullitem else a(i)=i
End While
M=each(a())
flush
While M
if array(m)>0 then push array(M)
End While
Pen 15 {
dispnames(ListA, "ListA")
dispnames(ListB, "ListB")
}

if empty then
print "no differences found"
else
M3=Array([])
Pen 11 {
dispnames(M3,"Differences")
}
End if
Sub dispnames(k as array, title$)
Local M=each(k)
Stack new {
While M
Data eval$(array(M))
End While
print title$;": ";array([])#str$(", ")
}
End Sub
}
TestDiff


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.