Results 1 to 11 of 11

Thread: Arraylist sort and binarysearch

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Arraylist sort and binarysearch

    Guys - I have a windows app and I'm trying to cache images in memory. I have:-
    HTML Code:
    Public TileCache As New ArrayList
    
        Structure TileCacheDescriptor
    
            Public TileURL As String
            Public TileLayer As String
            Public TileImage As Image
    
        End Structure
    
       Dim TileDetails As New TileCacheDescriptor
       TileDetails.TileImage = ThisMap.TileGrid(C - Col + w, T - CentreTile + w).TileImage
       TileDetails.TileURL = src
       TileDetails.TileLayer = Layer
       TileCache.Add(TileDetails)
       TileCache.Sort()
                       
    I think I have to sort the array to be able to use binarysearch.
    So I need to sort the arraylist on TileDetails.TileURL(if possible) and then binarysearch on it.

    Any hints much appreciated

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Arraylist sort and binarysearch

    An object needs to implement IComparable for you to tell it how to sort.

    Change your TileCacheDescriptor into a class and implement it. I can't tell why you need to sort it however .

  3. #3
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Smile Re: Arraylist sort and binarysearch

    Create a class which implements Icomparer
    Use its compare function to sort the array items.

    After that use ArrayList.Sort(New ComparereClassYouCreated)
    thanks
    amrita

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Arraylist sort and binarysearch

    Thanks Guys but I'm a bit lost now

    Should that be ICompare or IComarable?

    HTML Code:
    Imports Microsoft.VisualBasic
    
    Public Class TileCache : Implements IComparable
    
        Public Structure TileCacheDescriptor
    
            Public TileURL As String
            Public TileLayer As String
            Public TileImage As Image
    
        End Structure
    
    End Class
    It's now telling me:-

    HTML Code:
    Error	1	Class 'TileCache' must implement 'Function CompareTo(obj As Object) As Integer' for interface 'System.IComparable'.	C:\Dot Net 2008 Code\BSGPSV3530\TileCache.vb	3	37	BSGPS
    I read in several places that an arraylist needs to be sorted so that binarysearch can be used. And if it uses binary chop then it must be sorted.

  5. #5
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: Arraylist sort and binarysearch

    implements Icomparer
    and implements its compare method
    thanks
    amrita

  6. #6
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888
    thanks
    amrita

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Arraylist sort and binarysearch

    try this. it uses a List(Of TileCacheDescriptor) instead of an arraylist, + array.find instead of binarysearch:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Public TileCache As New List(Of TileCacheDescriptor)
    4.  
    5.     Structure TileCacheDescriptor
    6.         Public TileURL As String
    7.         Public TileLayer As String
    8.         Public TileImage As Image
    9.     End Structure
    10.  
    11.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    12.  
    13.         Dim TileDetails As New TileCacheDescriptor
    14.         TileDetails.TileImage = ThisMap.TileGrid(c - Col + w, T - CentreTile + w).TileImage
    15.         TileDetails.TileURL = "testURL1" 'src
    16.         TileDetails.TileLayer = "testLayer1" 'Layer
    17.         TileCache.Add(TileDetails)
    18.         TileDetails = New TileCacheDescriptor
    19.         TileDetails.TileImage = ThisMap.TileGrid(c - Col + w, T - CentreTile + w).TileImage
    20.         TileDetails.TileURL = "testURL2" 'src
    21.         TileDetails.TileLayer = "testLayer2" 'Layer
    22.         TileCache.Add(TileDetails)
    23.         TileDetails = New TileCacheDescriptor
    24.         TileDetails.TileImage = ThisMap.TileGrid(c - Col + w, T - CentreTile + w).TileImage
    25.         TileDetails.TileURL = "testURL3" 'src
    26.         TileDetails.TileLayer = "testLayer3" 'Layer
    27.         TileCache.Add(TileDetails)
    28.         TileDetails = New TileCacheDescriptor
    29.         TileDetails.TileImage = ThisMap.TileGrid(c - Col + w, T - CentreTile + w).TileImage
    30.         TileDetails.TileURL = "testURL4" 'src
    31.         TileDetails.TileLayer = "testLayer4" 'Layer
    32.         TileCache.Add(TileDetails)
    33.  
    34.         Dim c As New comparer
    35.         c.sortBy = comparer.sortOptions.TileURL
    36.  
    37.         TileCache.Sort(c)
    38.  
    39.         PictureBox1.Image = Array.Find(TileCache.ToArray, Function(item As TileCacheDescriptor) item.TileURL = "testURL4").TileImage
    40.  
    41.     End Sub
    42. End Class
    43.  
    44. Public Class comparer
    45.     Implements System.Collections.Generic.IComparer(Of WindowsApplication1.Form1.TileCacheDescriptor)
    46.  
    47.     Public Enum sortOptions
    48.         TileURL
    49.         TileLayer
    50.     End Enum
    51.  
    52.     Private _sortBy As sortOptions
    53.     Public Property sortBy() As sortOptions
    54.         Get
    55.             Return _sortBy
    56.         End Get
    57.         Set(ByVal value As sortOptions)
    58.             _sortBy = value
    59.         End Set
    60.     End Property
    61.  
    62.     Public Function Compare(ByVal x As Form1.TileCacheDescriptor, ByVal y As Form1.TileCacheDescriptor) As Integer Implements System.Collections.Generic.IComparer(Of Form1.TileCacheDescriptor).Compare
    63.         If _sortBy = sortOptions.TileURL Then
    64.             Return x.TileURL.CompareTo(y.TileURL)
    65.         ElseIf _sortBy = sortOptions.TileLayer Then
    66.             Return x.TileLayer.CompareTo(y.TileLayer)
    67.         End If
    68.     End Function
    69.  
    70. End Class

  8. #8
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Arraylist sort and binarysearch

    Quote Originally Posted by T120 View Post
    Thanks Guys but I'm a bit lost now

    Should that be ICompare or IComarable?

    I read in several places that an arraylist needs to be sorted so that binarysearch can be used. And if it uses binary chop then it must be sorted.
    You can use either, but the ICompare does specifically say about the binarysearch (I did not know that ). Can you not use a key to get this information, when and why do you need to sort it I still can't work out. There may be better ways with generic collections as pointed out by .paul.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Arraylist sort and binarysearch

    Guys

    I'm still confused. All I want to do is create a caches of images in memory to try and save time on hard drive accesses!

    If the arraylist.binarysearch uses the binary chop techneque then the array must be sorted.

    If I'm "barking up the wrong tree" is there a better way of achieving this?

  10. #10
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Arraylist sort and binarysearch

    How do you access the images? By name, by a binary version of the image, by a number, its position?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Devon - UK
    Posts
    214

    Re: Arraylist sort and binarysearch

    I intended accessing them by the originating web site url ie http://tile.openstreetmap.org/mapnik/17/64234/44116.png My original struture contained the URL and the image. But it could also be the locally stored folder/file.
    Last edited by T120; Aug 26th, 2010 at 06:53 AM.

Posting Permissions

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



Click Here to Expand Forum to Full Width