I have a large amount of data that is retrieved from a database. They are ordered by timestamp (which each item has) and I wish to quickly scroll smoothly through this data as well as being able to retrieve the next/previous record given a specific time.
Is there any data structure that will help me do this? At the moment I do a kind of binary search through the data to retrieve the next items.
Inserts, deletions and edits are rare (but occasionally necessary) so seek time is the most critical.
Any ideas?
A simple example:
public class TimedDataItem
{
DateTime Timestamp { get; set; }
}
// Large populated timestamped data set
IList<TimedDataItem> timedDataItemsList = new Last<TimedDataItem>();
// Get a 'random' time
DateTime myTime = DateTime.Now;
// Find items around that 'random' time
TimedDataItem next = timedDataItemsList.FirstOrDefault(t=>t.Timestamp > myTime);
TimedDataItem previous = timedDataItemsList.LastOrDefault(t=>t.Timestamp < myTime);
// Also foreach over the collection in time order if required
foreach (TimedDataItem item in timedDataItemsList)
DoStuff(item);
// Inserts, deletions, edits are extremely rare
Thanks.
Edited answer
Preferred solution – SortedSet
plus: sorted collection, allows logic use of elements within a certain range
minus: not sure what search algorithm, no previous and next
Solution SortedList
plus: sorted, typeSafe, allows next and prev
minus: linear search
Solution ArrayList
plus: allows use of an index, binarySearch (!)
minus: what should you do with an index in an unordered collection …