-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappointments.rb
More file actions
67 lines (63 loc) · 2.03 KB
/
appointments.rb
File metadata and controls
67 lines (63 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require_relative "unitType"
def encodeAppointments(appointments)
#express appointments as intervals over time line
for appointment in appointments
i = appointments.index(appointment)
appointments[i] = [encode(appointment[0]), encode(appointment[1])]
end
appointments.sort_by! {|x| x[0]}
appointmentIntervals = []
alreadyProccessed = []
for appointment in appointments
i = appointments.index(appointment)
startBlock, endBlock = appointment[0], appointment[1]
if alreadyProccessed.include?([startBlock, endBlock])
next #prevent duplication
end
for otherStart, otherStop in appointments[i + 1, appointments.size - 1]
if otherStart <= endBlock and otherStart >= startBlock
# overlap occured
alreadyProccessed.push([otherStart, otherStop])
if endBlock < otherStop
#overlap not equal -> extend block
endBlock = otherStop
end
end
end
appointmentIntervals.push([startBlock, endBlock])
end
return appointmentIntervals
end
def encodeAvailability(appointments, fromThisTime, tillThisTime)
#express availability as intervals over time line
if appointments[0][0].is_a? String
appointments = encodeAppointments(appointments)
end
availability = []
last = fromThisTime
for start, close in appointments
#available from end of last appointment till begining of next appointment
availability.push([last, start])
last = close
end
availability.push([last, tillThisTime])
return availability
end
def encodeOpenings(availability)
#appointment openings are
appointmentOpenings = []
for start, stop in availability
#how many half hours segments are in open interval
segmentCount = Unit.new(stop) - Unit.new(start)
for offset in 0..(segmentCount.to_i - 1)
appointmentOpenings.push([start + offset, start + offset + 1])
end
end
return appointmentOpenings
end
def decodeOpenings(openings)
for opening in openings
i = openings.index(opening)
openings[i] = [decode(opening[0]), decode(opening[1])]
end
end