dcsimg
Results 1 to 5 of 5
  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2016
    Posts
    68

    Resolved [RESOLVED] search 2d array, return index(s)?

    I have a 2D array (see here for the how/why) and I'm trying to figure out how (or even if it's possible) to search for a given value (in this case a custom enum value) and return the location (index) of that value in the array.

    Or am I just spinning my wheels?


    Edit:
    I don't know if it is the best route, but I created my own search function to do this.
    Last edited by CWITT; Mar 7th, 2018 at 11:01 AM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,148

    Re: search 2d array, return index(s)?

    Sure, it's possible to search for a given value, and you say you've created your own search function, which seems like a good thing to do. There may be multiple ways you can go about it, but the typical way is to just iterate through both dimensions of the array, and that would be as efficient as anything else unless there are special properties of the array that make some other search method more effective.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2016
    Posts
    68

    Re: search 2d array, return index(s)?

    It's a fairly simplistic array of values.

    Code:
    Function ArraySearch_Value(ByVal ArrayList As TileData.MoveType(,), ByVal Value As TileData.MoveType) As Point
            For y = 0 To 4
                For x = 0 To 4
                    If ArrayList(x, y) = Value Then
                        Return New Point(x, y)
                    End If
                Next
            Next
        End Function
    Code:
       Function ArraySearch_Values(ByVal ArrayList As TileData.MoveType(,), ByVal Value As TileData.MoveType) As List(Of Point)
            Dim TPL As New List(Of Point)
            For y = 0 To 4
                For x = 0 To 4
                    If ArrayList(x, y) = Value Then
                        TPL.Add(New Point(x, y))
                    End If
                Next
            Next
            If TPL.Count = 0 Then
                Return Nothing
            Else
                Return TPL
            End If
        End Function

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,148

    Re: [RESOLVED] search 2d array, return index(s)?

    Yeah, it's pretty unlikely that you can do anything more efficient than that. If the arrays are sorted, you might be able to do something like a binary search to improve the performance, but that wouldn't really matter unless your array dimensions are in the tens of thousands.

    Alternatively, if the values were all unique, you could maintain a Dictionary(of String, Point) in addition to the array. Looking something up from the dictionary would be far faster, but it wouldn't work if the values weren't unique, and you'd have to decide whether the overhead of maintaining both the array and the dictionary is worth it (if you changed one you'd have to change the other).

    A third option is that a 2D array can always be a 1D array, cause that's what they are in memory. In memory, the first dimension can be thought of as the row, and the 2D array is a series of contiguous chunks, each of which is a row, so any one item is (rowIndex * rowSize) + columnIndex. This really doesn't speed up searching all that much, except that a 1D array would be easier to search with things like LINQ...which wouldn't give you any advantage at all, but could look pretty compact.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2016
    Posts
    68

    Re: [RESOLVED] search 2d array, return index(s)?

    Yeah, for my use the 2D array isn't for gathering or sorting data but rather holding the "location" of a move and it's own location relative to those moves (for my tiles, not that that explanation should mean anything without a lot more context).

    Thanks for the input

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.