I need a data structure that can store non-overlapping ranges within a single dimension. The entire range of the dimension need not be completely covered.
An example
A doubly linked list works well because you only use as much memory as you have filled ranges, and you need only check overlapping on insertions - it's almost trivial to do so at that point. If there's overlap the new item is rejected.
RoomID ReservationID PreviousReservationID NextReservationID StartTimeDate EndTimeDate Priority UserID
The priority and UserID allow for schedules to have priorities (professor might have more clout than a student group) so that a new item can 'knock' the lower priority items out of the way during an insertion, and the UserID allows an email to be sent to the bumped meeting organizers.
You'll want to consider adding a table that points to the first meeting of each day so that searches can be optimized.
-Adam
If you are lucky (!) enough to be using Postgres, you can use a tstzrange
column, and apply a constraint to prevent overlaps. The bonus of using a range type is that it will inherently prevent start being greater than finish.
ALTER TABLE "booking"
ADD CONSTRAINT "overlapping_bookings"
EXCLUDE USING gist ("period" WITH &&, "room" WITH =);
You may need to CREATE EXTENSION IF NOT EXISTS btree_gist
, as creating a gist using && is not supported without that extension.