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
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 :).
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)
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.
Re: Arraylist sort and binarysearch
implements Icomparer
and implements its compare method
Re: Arraylist sort and binarysearch
Re: Arraylist sort and binarysearch
try this. it uses a List(Of TileCacheDescriptor) instead of an arraylist, + array.find instead of binarysearch:
vb Code:
Public Class Form1
Public TileCache As New List(Of TileCacheDescriptor)
Structure TileCacheDescriptor
Public TileURL As String
Public TileLayer As String
Public TileImage As Image
End Structure
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim TileDetails As New TileCacheDescriptor
TileDetails.TileImage = ThisMap.TileGrid(c - Col + w, T - CentreTile + w).TileImage
TileDetails.TileURL = "testURL1" 'src
TileDetails.TileLayer = "testLayer1" 'Layer
TileCache.Add(TileDetails)
TileDetails = New TileCacheDescriptor
TileDetails.TileImage = ThisMap.TileGrid(c - Col + w, T - CentreTile + w).TileImage
TileDetails.TileURL = "testURL2" 'src
TileDetails.TileLayer = "testLayer2" 'Layer
TileCache.Add(TileDetails)
TileDetails = New TileCacheDescriptor
TileDetails.TileImage = ThisMap.TileGrid(c - Col + w, T - CentreTile + w).TileImage
TileDetails.TileURL = "testURL3" 'src
TileDetails.TileLayer = "testLayer3" 'Layer
TileCache.Add(TileDetails)
TileDetails = New TileCacheDescriptor
TileDetails.TileImage = ThisMap.TileGrid(c - Col + w, T - CentreTile + w).TileImage
TileDetails.TileURL = "testURL4" 'src
TileDetails.TileLayer = "testLayer4" 'Layer
TileCache.Add(TileDetails)
Dim c As New comparer
c.sortBy = comparer.sortOptions.TileURL
TileCache.Sort(c)
PictureBox1.Image = Array.Find(TileCache.ToArray, Function(item As TileCacheDescriptor) item.TileURL = "testURL4").TileImage
End Sub
End Class
Public Class comparer
Implements System.Collections.Generic.IComparer(Of WindowsApplication1.Form1.TileCacheDescriptor)
Public Enum sortOptions
TileURL
TileLayer
End Enum
Private _sortBy As sortOptions
Public Property sortBy() As sortOptions
Get
Return _sortBy
End Get
Set(ByVal value As sortOptions)
_sortBy = value
End Set
End Property
Public Function Compare(ByVal x As Form1.TileCacheDescriptor, ByVal y As Form1.TileCacheDescriptor) As Integer Implements System.Collections.Generic.IComparer(Of Form1.TileCacheDescriptor).Compare
If _sortBy = sortOptions.TileURL Then
Return x.TileURL.CompareTo(y.TileURL)
ElseIf _sortBy = sortOptions.TileLayer Then
Return x.TileLayer.CompareTo(y.TileLayer)
End If
End Function
End Class
Re: Arraylist sort and binarysearch
Quote:
Originally Posted by
T120
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.
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?
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?
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.