Daniel
Daniel

Reputation: 518

Is there a faster way to find an item from a List(of T)?

I create vectors and store them in a list (of Vector3D). In order to find the correct vector again later, I also store the Phis and Thetas (both List (of Double)). I use the getVector function to find the right vector based on the theta and phi set by the user. Is there a faster way?

Public Sub New(ByVal radius As Double)
        Me.Radius = radius
        Camera_Vector = New Vector3D(0, 0, Camera)
        For _phi As Double = 0.0 To 359.5 Step 0.5
            For _theta As Double = 0.0 To 90.0 Step 0.5
                List_with_all_the_vectors.Add(New Vector3D(
                                                radius * Math.Cos(_phi * Math.PI / 180.0) * Math.Sin(_theta * Math.PI / 180.0),
                                                radius * Math.Sin(_phi * Math.PI / 180.0) * Math.Sin(_theta * Math.PI / 180.0),
                                                radius * Math.Cos(_theta * Math.PI / 180.0)))
                Liste_thetas.Add(_theta)
                Liste_phis.Add(_phi)
            Next
        Next
    End Sub

    Public Function getVector() As Vector3D
        Dim Value As Vector3D
        For i As Integer = 0 To List_with_all_the_vectors.Count - 1 Step 1
            If Liste_phis(i) = CDbl(Phi) AndAlso Liste_thetas(i) = CDbl(Theta) Then
                Value = List_with_all_the_vectors(i)
                Return Value
            End If
        Next
        Return Value
    End Function

Upvotes: 1

Views: 242

Answers (1)

Mary
Mary

Reputation: 15081

It looks like you are creating three parallel lists. Why no create a little class of the 3 types and create a single list. I added a constructor to the VecThetaPhi class to make construction easier. Then a little bit of Linq magic (not necessarily faster than for loops but way cooler) finds the vector you are looking for.

Private Camera As Double
Private Camera_Vector As Vector3D
Private Radius As Double

Public Class VecThetaPhi
    Public Vect As Vector3D
    Public Theta As Double
    Public Phi As Double
    Public Sub New(vec As Vector3D, the As Double, ph As Double)
        Vect = vec
        Theta = the
        Phi = ph
    End Sub
End Class

Private LstVecThetaPhi As New List(Of VecThetaPhi)
Public Sub New(ByVal radius As Double)
    Me.Radius = radius
    Camera_Vector = New Vector3D(0, 0, Camera)
    For _phi As Double = 0.0 To 359.5 Step 0.5
        For _theta As Double = 0.0 To 90.0 Step 0.5
            Dim vec As New Vector3D(
                                    radius * Math.Cos(_phi * Math.PI / 180.0) * Math.Sin(_theta * Math.PI / 180.0),
                                    radius * Math.Sin(_phi * Math.PI / 180.0) * Math.Sin(_theta * Math.PI / 180.0),
                                    radius * Math.Cos(_theta * Math.PI / 180.0))
            LstVecThetaPhi.Add(New VecThetaPhi(vec, _theta, _phi))
        Next
    Next
End Sub

Public Function getVector(ThetaValue As Double, PhiValue As Double) As Vector3D
    Dim Value As Vector3D = (From item In LstVecThetaPhi
                             Where item.Theta = ThetaValue And item.Phi = PhiValue
                             Select item.Vect).FirstOrDefault
    Return Value
End Function

Upvotes: 2

Related Questions