| | | 1 | | namespace Punch.CLI; |
| | | 2 | | |
| | | 3 | | // Owns a day's booked blocks together with the 96-slot occupancy mask, keeping |
| | | 4 | | // the two in sync so callers never manipulate the mask directly. Slots are |
| | | 5 | | // quarter-hour indices 0..95. |
| | | 6 | | internal sealed class DaySchedule |
| | | 7 | | { |
| | | 8 | | private readonly List<TimeBlock> _blocks; |
| | 107 | 9 | | private readonly bool[] _occupied = new bool[96]; |
| | | 10 | | |
| | 107 | 11 | | public DaySchedule(IEnumerable<TimeBlock> blocks) |
| | 107 | 12 | | { |
| | 107 | 13 | | _blocks = blocks.ToList(); |
| | 487 | 14 | | foreach (var block in _blocks) |
| | 83 | 15 | | SetOccupied(block, true); |
| | 107 | 16 | | } |
| | | 17 | | |
| | 101 | 18 | | public IReadOnlyList<TimeBlock> Blocks => _blocks; |
| | | 19 | | |
| | 174 | 20 | | public int Count => _blocks.Count; |
| | | 21 | | |
| | 35 | 22 | | public bool IsFree(int slot) => slot is >= 0 and < 96 && !_occupied[slot]; |
| | | 23 | | |
| | | 24 | | public bool IsOverlapping(int start, int length) |
| | 12 | 25 | | { |
| | 80 | 26 | | for (var i = start; i < start + length && i < 96; i++) |
| | 31 | 27 | | if (_occupied[i]) |
| | 3 | 28 | | return true; |
| | 9 | 29 | | return false; |
| | 12 | 30 | | } |
| | | 31 | | |
| | | 32 | | public TimeBlock? FindAt(int slot) => |
| | 115 | 33 | | _blocks.FirstOrDefault(b => slot >= b.StartSlot && slot < b.StartSlot + b.Length); |
| | | 34 | | |
| | | 35 | | // True when the slot immediately after the block's current span is free, |
| | | 36 | | // i.e. the block can grow by one slot. |
| | | 37 | | public bool CanGrow(int startSlot, int currentLength) |
| | 5 | 38 | | { |
| | 5 | 39 | | var next = startSlot + currentLength; |
| | 5 | 40 | | return next < 96 && !_occupied[next]; |
| | 5 | 41 | | } |
| | | 42 | | |
| | | 43 | | public void Add(TimeBlock block) |
| | 3 | 44 | | { |
| | 3 | 45 | | _blocks.Add(block); |
| | 3 | 46 | | SetOccupied(block, true); |
| | 3 | 47 | | } |
| | | 48 | | |
| | | 49 | | public void Remove(TimeBlock block) |
| | 3 | 50 | | { |
| | 3 | 51 | | if (_blocks.Remove(block)) |
| | 2 | 52 | | SetOccupied(block, false); |
| | 3 | 53 | | } |
| | | 54 | | |
| | | 55 | | // Swaps oldBlock for newBlock in place, re-deriving occupancy from both spans. |
| | | 56 | | // Returns newBlock for convenient reassignment of the caller's reference. |
| | | 57 | | public TimeBlock Replace(TimeBlock oldBlock, TimeBlock newBlock) |
| | 5 | 58 | | { |
| | 5 | 59 | | var idx = _blocks.IndexOf(oldBlock); |
| | 5 | 60 | | if (idx < 0) |
| | 1 | 61 | | return oldBlock; |
| | 4 | 62 | | SetOccupied(oldBlock, false); |
| | 4 | 63 | | _blocks[idx] = newBlock; |
| | 4 | 64 | | SetOccupied(newBlock, true); |
| | 4 | 65 | | return newBlock; |
| | 5 | 66 | | } |
| | | 67 | | |
| | | 68 | | // Temporarily clears a block's occupancy without removing it from the list, |
| | | 69 | | // so an in-progress edit can preview a shrink/grow in the timeline. Pair with |
| | | 70 | | // FillSlots to restore (or call Replace when committing the edit). |
| | 10 | 71 | | public void FreeSlots(TimeBlock block) => SetOccupied(block, false); |
| | | 72 | | |
| | 2 | 73 | | public void FillSlots(TimeBlock block) => SetOccupied(block, true); |
| | | 74 | | |
| | | 75 | | // Finds where the cursor should land after booking a block at cursorSlot: |
| | | 76 | | // the next free slot, or slot 95 (selecting the block there) when the day is full. |
| | | 77 | | public (int cursorSlot, int selectionLength, TimeBlock? selectedBlock) AdvanceAfterAdd(int cursorSlot) |
| | 6 | 78 | | { |
| | 6 | 79 | | var selectionLength = 1; |
| | 6 | 80 | | TimeBlock? selectedBlock = null; |
| | | 81 | | |
| | 26 | 82 | | while (cursorSlot < 96 && _occupied[cursorSlot]) |
| | 20 | 83 | | cursorSlot++; |
| | 6 | 84 | | if (cursorSlot >= 96) |
| | 2 | 85 | | { |
| | 2 | 86 | | cursorSlot = 95; |
| | 2 | 87 | | if (_occupied[95]) |
| | 2 | 88 | | { |
| | 2 | 89 | | var adj = FindAt(95); |
| | 2 | 90 | | if (adj != null) |
| | 2 | 91 | | { |
| | 2 | 92 | | selectionLength = adj.Length; |
| | 2 | 93 | | selectedBlock = adj; |
| | 2 | 94 | | } |
| | 2 | 95 | | } |
| | 2 | 96 | | } |
| | | 97 | | |
| | 6 | 98 | | return (cursorSlot, selectionLength, selectedBlock); |
| | 6 | 99 | | } |
| | | 100 | | |
| | | 101 | | private void SetOccupied(TimeBlock block, bool value) |
| | 108 | 102 | | { |
| | 684 | 103 | | for (var s = block.StartSlot; s < block.StartSlot + block.Length; s++) |
| | 234 | 104 | | _occupied[s] = value; |
| | 108 | 105 | | } |
| | | 106 | | } |