types, question about deleting
Blitz3D Forums/Blitz3D Beginners Area/types, question about deleting
| ||
What I am trying to do is make a function that runs through a list of types and deletes the ones that have the same content. type test field x field y end type I want to be able to trim the list of types down so no two types have the same content for the two fields. How would you go about doing this? For instance, lets say that this was the content of five types: 1,0 3,4 5,2 2,4 5,2 Then what I would like to end up with is only four types and have one of the ones that are repeated deleted. |
| ||
make an array with as much elements as there are type-instances of the particular type in Blitz's typelist. loop through all the types and per type-instance loop through all the elements of the array and compare its values to the values in the type-instance, then if they are found in the array you delete the type-instance, else you store the values in the array. maybe it is more simple by using an extra type with the same fields and use that one to compare to and add to instead of an array. |
| ||
If the x and y values are limited, then you may create an array(max_x,max_y), and each time you create a new element in the type list, first check the array(x,y). It it holds the value 0, then create the new element, and set the array(x,y) element to 1. Example, supposing you have x always <= 100, as y: Dim arr_test(100,100) ;each element is set to 0 by default type t_elem ;the type structure field x field y end type global elem.t_elem ;the type pointer ;init the random number generator seedrnd millisecs() ;let's create some type element for n = 1 to 500 x = rand(100) y = rand(100) create_element(x,y) next function create_element(x,y) if arr_test(x,y) = 0 then elem.t_elem = new t_elem elem\x = x elem\y = y arr_test(x,y) = 1 debuglog.print x + "; " + y + " : created" else debuglog.print x + "; " + y + " : already used" endif end function Hope this has sense for you, Sergio. |
| ||
i dont think your example shows how to check for redundancy and act upon it. your example only shows how to prevent existing values from being added to the typelist, wich can be useful, but maybe not in his situation, as he said that he wanted to check for redundancy in an existing typelist. |
| ||
Sometimes I surprise even myself,Type mytype Field xfactor End Type For some=1 To 10 m.mytype=New mytype m\xfactor=Rnd(10) Next For m.mytype=Each mytype Print m\xfactor Next For a.mytype=Each mytype For b.mytype=Each mytype If Handle(a)<>Handle(b) If a\xfactor=b\xfactor Delete b EndIf EndIf Next Next Print "after search" For m.mytype=Each mytype Print m\xfactor Next While Not KeyHit(1) Wend |
| ||
This is what I tried but it doesn't work, perhaps you could see why not.Delete Each checkpoints For p.points = Each points is_it_there = 0 For c.checkpoints = Each checkpoints If p\x = c\x And p\z = c\z Then is_it_there = 1 Else is_it_there = 0 End If Next If is_it_there = 0 Then c.checkpoints = New checkpoints c\x = p\x c\z = p\z End If Next Delete Each points For c.checkpoints = Each checkpoints p.points = New points p\x = c\x p\z = c\z Next |
| ||
It does not work because the variable is_it_there could be set to 1, even if it was previously set to 0, so it does not create a new point element, even if it should. This should work: Delete Each checkpoints For p.points = Each points is_it_there = 0 For c.checkpoints = Each checkpoints If p\x = c\x And p\z = c\z Then is_it_there = 1 End If Next If is_it_there = 0 Then c.checkpoints = New checkpoints c\x = p\x c\z = p\z End If Next Delete Each points For c.checkpoints = Each checkpoints p.points = New points p\x = c\x p\z = c\z Next |
| ||
http://www.blitzbasic.com/codearcs/codearcs.php?code=920 |
| ||
@semar, yes now it works, and I see what you mean by my mistake of having the "is_it_there = 0" in there. @shambler, your solution seems to work fine too. Thanks all for replies. |