array optimizer

R

Ricardo Furtado

I have a big problem concerning arrays.
I have an array of structs that is deply implemented in the code. If i
change this implementation then i have to change everything, so after
searching google and a lot of newsgroups i came up with this question:
With the following array, how can i make a search without having to use a
loop to seek for a certain "thing" in the array of structs?
(i've tryed to use array.find(), but because its an array of structs i just
wasn't able to make it work)




Friend Sub AdicionaPontosAImagem(ByVal ValorX As Long, ByVal ValorY As Long,
ByVal strNomePonto As String, _
ByVal lngHwnd As Long, ByVal strNomeAnalise As String, _
Optional ByRef myGraphics As System.Drawing.Graphics = Nothing)
Dim lngContador As Long
Dim bolFlag As Boolean


bolFlag = False
If ArrayPontos(0).bolInicializacao = True Then
ArrayPontos(0).bolInicializacao = False
ArrayPontos(0).strNomePonto = strNomePonto
ArrayPontos(0).lngValorX = ValorX
ArrayPontos(0).lngValorY = ValorY
ArrayPontos(0).strAbreviatura =
BD.retornaAbreviaturaDeDeterminadoPonto(strNomePonto)
ArrayPontos(0).Posicoes.intPreenchidas = 0

ArrayPontosImg(0).bolInicializacao = False
ArrayPontosImg(0).lngValorX = ValorX
ArrayPontosImg(0).lngValorY = ValorY
ReDim ArrayPontosImg(0).ptnPontos(5)
ArrayPontosImg(0).ptnPontos(0).X = ValorX
ArrayPontosImg(0).ptnPontos(0).Y = ValorY - 1
ArrayPontosImg(0).ptnPontos(1).X = ValorX
ArrayPontosImg(0).ptnPontos(1).Y = ValorY
ArrayPontosImg(0).ptnPontos(2).X = ValorX
ArrayPontosImg(0).ptnPontos(2).Y = ValorY + 1
ArrayPontosImg(0).ptnPontos(3).X = ValorX - 1
ArrayPontosImg(0).ptnPontos(3).Y = ValorY
ArrayPontosImg(0).ptnPontos(4).X = ValorX + 1
ArrayPontosImg(0).ptnPontos(4).Y = ValorY

If (defPerfilGlobal = DefinicaoPerfil.pontosFulcrais) AndAlso (Not
IsNothing(myGraphics)) Then
identificaPerfilAutomaticamente(myGraphics)
End If
Else



For lngContador = 0 To UBound(ArrayPontos) - 1
If ArrayPontos(lngContador).strNomePonto = strNomePonto Then
ArrayPontos(lngContador).lngValorX = ValorX
ArrayPontos(lngContador).lngValorY = ValorY
ArrayPontos(lngContador).strAbreviatura =
BD.retornaAbreviaturaDeDeterminadoPonto(strNomePonto)

ArrayPontosImg(lngContador).bolInicializacao = False
ArrayPontosImg(lngContador).lngValorX = ValorX
ArrayPontosImg(lngContador).lngValorY = ValorY
ReDim ArrayPontosImg(lngContador).ptnPontos(5)
ArrayPontosImg(lngContador).ptnPontos(0).X = ValorX
ArrayPontosImg(lngContador).ptnPontos(0).Y = ValorY - 1
ArrayPontosImg(lngContador).ptnPontos(1).X = ValorX
ArrayPontosImg(lngContador).ptnPontos(1).Y = ValorY
ArrayPontosImg(lngContador).ptnPontos(2).X = ValorX
ArrayPontosImg(lngContador).ptnPontos(2).Y = ValorY + 1
ArrayPontosImg(lngContador).ptnPontos(3).X = ValorX - 1
ArrayPontosImg(lngContador).ptnPontos(3).Y = ValorY
ArrayPontosImg(lngContador).ptnPontos(4).X = ValorX + 1
ArrayPontosImg(lngContador).ptnPontos(4).Y = ValorY
bolFlag = True
Exit For
End If
Next

If Not bolFlag Then
ReDim Preserve ArrayPontos(UBound(ArrayPontos) + 1)
ArrayPontos(UBound(ArrayPontos) - 1).bolInicializacao = False
ArrayPontos(UBound(ArrayPontos) - 1).strNomePonto = strNomePonto
ArrayPontos(UBound(ArrayPontos) - 1).lngValorX = ValorX
ArrayPontos(UBound(ArrayPontos) - 1).lngValorY = ValorY
ArrayPontos(UBound(ArrayPontos) - 1).strAbreviatura =
BD.retornaAbreviaturaDeDeterminadoPonto(strNomePonto)
ArrayPontos(UBound(ArrayPontos) - 1).Posicoes.intPreenchidas = 0

ReDim Preserve ArrayPontosImg(UBound(ArrayPontosImg) + 1)
ArrayPontosImg(UBound(ArrayPontosImg) - 1).bolInicializacao = False
ArrayPontosImg(UBound(ArrayPontosImg) - 1).lngValorX = ValorX
ArrayPontosImg(UBound(ArrayPontosImg) - 1).lngValorY = ValorY
ReDim ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(5)
ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(0).X = ValorX
ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(0).Y = ValorY - 1
ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(1).X = ValorX
ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(1).Y = ValorY
ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(2).X = ValorX
ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(2).Y = ValorY + 1
ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(3).X = ValorX - 1
ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(3).Y = ValorY
ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(4).X = ValorX + 1
ArrayPontosImg(UBound(ArrayPontosImg) - 1).ptnPontos(4).Y = ValorY
If (defPerfilGlobal = DefinicaoPerfil.pontosFulcrais) AndAlso (Not
IsNothing(myGraphics)) Then
identificaPerfilAutomaticamente(myGraphics)
End If

End If
End If
TracaRectas(lngHwnd, strNomeAnalise)
RaiseEvent registaPontoNaListagem(strNomePonto)
End Sub
 
R

Ricardo Furtado

The great problem, for me, in this case, with collections is that a
collection only acepts an index and a name.
In my case, i need an index, a name, the mouse.X value, the mouse.Y value
and the tag value
 
R

Ricardo Furtado

thanks

i've implemented the code in the following way (it might work after changing
all the other references in the project):

Friend Sub AdicionaPontosAImagem(ByVal ValorX As Long, ByVal ValorY As
Long, ByVal strNomePonto As String, _
ByVal lngHwnd As Long, ByVal
strNomeAnalise As String, _
Optional ByRef myGraphics As
System.Drawing.Graphics = Nothing)
Dim lngContador As Long
Dim bolFlag As Boolean
bolFlag = False
Dim myPT As Pontos
Dim defPt As DefinePonto

myPT.bolInicializacao = False
myPT.strNomePonto = strNomePonto
myPT.lngValorX = ValorX
myPT.lngValorY = ValorY
myPT.strAbreviatura =
BD.retornaAbreviaturaDeDeterminadoPonto(strNomePonto)
myPT.Posicoes.intPreenchidas = 0


defPt.bolInicializacao = False
defPt.lngValorX = ValorX
defPt.lngValorY = ValorY
defPt.ptnPontos = New Hashtable
defPt.ptnPontos.Add("X0", ValorX)
defPt.ptnPontos.Add("Y0", ValorY - 1)
defPt.ptnPontos.Add("X1", ValorX)
defPt.ptnPontos.Add("Y1", ValorY)
defPt.ptnPontos.Add("X2", ValorX)
defPt.ptnPontos.Add("Y2", ValorY + 1)
defPt.ptnPontos.Add("X3", ValorX - 1)
defPt.ptnPontos.Add("Y3", ValorY)
defPt.ptnPontos(3).Y = ValorY
defPt.ptnPontos.Add("X4", ValorX + 1)
defPt.ptnPontos.Add("Y4", ValorY)




If ArrPtHash.Count < 1 Then

ArrPtHash.Add(strNomePonto, myPT)
ArrImgHash.Add(strNomePonto, defPt)
If (defPerfilGlobal = DefinicaoPerfil.pontosFulcrais) AndAlso
(Not IsNothing(myGraphics)) Then
identificaPerfilAutomaticamente(myGraphics)
End If
Else
If Not ArrPtHash.ContainsKey(strNomePonto) Then

ArrPtHash.Add(strNomePonto, myPT)
ArrImgHash.Add(strNomePonto, defPt)
Else

ArrPtHash.Item(strNomePonto) = myPT
ArrPtHash.Item(strNomePonto) = defPt
If (defPerfilGlobal = DefinicaoPerfil.pontosFulcrais)
AndAlso (Not IsNothing(myGraphics)) Then
identificaPerfilAutomaticamente(myGraphics)
End If
End If
End If

TracaRectas(lngHwnd, strNomeAnalise)
RaiseEvent registaPontoNaListagem(strNomePonto)
End Sub
 
J

James Hahn

If the find is only ever going to use one item of the struct, you could
build a hashtable when you populate the array and use that to look up the
value and get an index into the array.

A hashtable lookup seems to be quite fast, but any of the collections that
implement find would do.

This is some code from a recent project. ID is what I need to access the
array. In this case I am creating the hashtable as soon as the array is
fully populated, but it might be easier for you to do it as the array is
being built. The ID is the key and the array position is the data.

Public fht As Hashtable

fht = New Hashtable(1000)
For I = 0 To FRIDs.Count - 1
fht.Add(FRIDs.FRID_Item(I).ID, I)
Next I
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top