Search lands in PR-5.1 (Pagefind).

How-to Intermediate

Chapter 2 Updated

My Calendar II

Two layers: all bookings + known double-booked regions.

  • Full 5m
  • Revision 2m
  • Flow 1m

My Calendar II — control flow

flowchart TD
    S["<b>i · book(start, end)</b>"]
      --> L1{"<b>ii · next overlap [os, oe]?</b>"}
    L1 -->|"yes"| C1{"<b>iii · would triple?</b><br/>start &lt; oe and end &gt; os?"}
    C1 -->|"yes"| F["<b>iv · return false</b>"]
    C1 -->|"no"| L1
    L1 -->|"done"| L2{"<b>v · next booking [bs, be]?</b>"}
    L2 -->|"yes"| C2{"<b>vi · overlaps?</b><br/>start &lt; be and end &gt; bs?"}
    C2 -->|"yes"| R["<b>vii · record double</b><br/>overlaps.append((max, min))"]
    C2 -->|"no"| L2
    R --> L2
    L2 -->|"done"| A["<b>viii · append booking</b>"] --> T["<b>ix · return true</b>"]
 
    classDef start fill:#f5efe1,stroke:#6a8a4f,stroke-width:2px,color:#1a1915;
    classDef decision fill:#fdecd3,stroke:#c2410c,stroke-width:2px,color:#1a1915;
    classDef ok fill:#e7efd9,stroke:#587640,stroke-width:2px,color:#1a1915;
    classDef bad fill:#f5d4cf,stroke:#991b1b,stroke-width:2px,color:#1a1915;
    class S start
    class L1,L2,C1,C2 decision
    class R,A,T ok
    class F bad
  • Entry

    New booking `(start, end)`.

  • Iterate overlaps

    Loop the list of existing double-booked regions.

  • Triple-booking test

    `start < oe and end > os` — overlap with a double-booking would mean three active events.

  • Reject

    Return `false` without mutating any state.

  • Iterate bookings

    If we didn’t reject, walk the full bookings list.

  • Overlaps existing?

    Standard half-open overlap check.

  • Record

    Append `(max(start, bs), min(end, be))` — the newly-created double-booking region.

  • Append booking

    Only now do we actually accept the event.

  • Return

    Signal success with `true`.

Comments

Comments are disabled in this environment. Set PUBLIC_GISCUS_REPO, PUBLIC_GISCUS_REPO_ID, and PUBLIC_GISCUS_CATEGORY_ID to enable.