Data structure for non-overlapping ranges within a single dimension

前端 未结 8 1283
再見小時候
再見小時候 2020-12-30 11:26

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

相关标签:
8条回答
  • 2020-12-30 12:18

    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

    0 讨论(0)
  • 2020-12-30 12:22

    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.

    0 讨论(0)
提交回复
热议问题