How to search an array of classes?

I have a problem that I can’t seem to figure out a solution for and so I thought I’d turn to those for whom this would be a warmup before breakfast. :slight_smile:

I have a class, cCountStructure, that is nothing more than a storage unit for multiple values that describe a machine reading:

MachineID as Integer ReadingA as Integer ReadingB as Integer ReadingC as Integer ReadingD as Integer ReadDate as Date

For a particular operation in the application, many of these cCountStructure are stored in a one-dimensional array. The order of the array will always be by MachineID within ReadDate—i.e.:

2015-04-04 MachineID = 1 MachineID = 2 MachineID = 3 MachineID = 4 2015-04-05 MachineID = 1 MachineID = 2 MachineID = 3 MachineID = 4 2015-04-06 MachineID = 1 MachineID = 2 MachineID = 3 MachineID = 4

My problem is that this operation requires that I find all cCountStructures with a specific date and I’m not sure the best way to go about doing it. The only thing I can come up with is to perform a while loop with an index that starts at zero and then keep increasing the index by one until I find the date I’m looking for.

[code]Dim i as Integer = 0
Dim Found as Boolean = False

While Not Found
if cCountStructure( i ).ReadDate = DateToLookFor then
Found = True
i = i + NumberOfMachines // Since MachineIDs will already be sorted within matching dates, we can jump ahead by the number of machines to get to the next date.
end if

// i is the first index to contain the date we’re looking for[/code]

I would think this method is perfectly fine for small arrays, but would be horribly inefficient for larger arrays. Plus, this search itself could end up happening within a loop (need to find the starting indexes of 100 or 1000 dates at a time).

I’m sure there is something I’m missing here. I’d love to get some suggestions from the experts.

Thank you in advance!

If your array is sorted you could use a binary search for the date. If the array is not sorted, it might make sense to sort it, especially if the look ups will happen in a loop. Another approach you could take would be to refactor your setup so that you are using a dictionary and storing an array of the objects with the key as the date. This would allow for constant time lookup by date, and then you would proceed with whatever processing needs to occur from there. Looking at the ordering you posted above, this might be the better solution if it is not too late.

Store them in an in-memory database instead of an array.

@Jason King: I hadn’t thought about a dictionary. I didn’t realize you could store an array in a dictionary with a value as a key for the entire dictionary. That might work well, assuming its fast enough. I’ll spend some time tomorrow looking at this.

@Tim Hare: Could you provide a little more info about this? Would this be faster than a dictionary?

If you only ever have one measurement per machine per day, then a dictionary makes sense, but a database could be more flexible. The layout of your data looks perfect for a database table.

If you connect to an sqlite database without providing a DatabaseFile, it creates the database in memory. You can do complex queries extremely quicky with such a setup. It is a little more complicated, of course, so you have to consider your present (and future) requirements.

Well, the data is originating in a sqlite database. The current array is being generated from the result of a query (that can obviously span multiple dates).

The reason it has to be in an array is because this will be used in a client/server environment. The server will maintain the database. Clients will send requests to the server and the server will run the query and return the data to the client in an array. The client will do calculations on that data locally.

Recreating a database locally seems to me it might be overkill, but it’s something I’m willing to look into. Thanks for the direction!

if the sqlite database is in memory its not much more overhead than an array and the extra flexibility you have with searching is sometimes very much worth it

Why? Couldn’t it simply return a RecordSet? An array seems overly complicated for what you describe …

Unless the server is on the internet and the “array” is really a JSON or XML packet. Then it makes perfect sense. It also makes sense to reconstitute it into a database if need be. Note I said IF. I don’t know your requirements.

@Markus Winter How would I return a RecordSet from the server to the client over the network?

@Tim Hare The server is on the LAN, although there is a slight possibility that there could be clients connecting from remote locations at a future date, depending on where the company decides to go with this. The array is really an array, but can be changed to whatever will make the most sense for what I need it for. The format of the data being sent from the server to the client has not yet been totally pinned down and can be changed at this time. Right now, it is literally comma-delimited data with a CRLF delimiting records.

Although I’ve been writing various apps for years for the various companies I’ve worked for (and my own software business), I’m a total novice at this database stuff and have been learning as I go along. This is the first time I’ve had to deal with a client/server database system that I have to create because the company will not allow the use of a true multi-user database.