Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Seems like everything works, now with all files
- Loading branch information
David Fendrich
committed
Apr 12, 2012
1 parent
461e28c
commit 84058cc
Showing
4 changed files
with
187 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
#merge to intervals immediately | ||
import bisect | ||
|
||
class Period: | ||
def __init__(self): | ||
def __init__(self, cutoff): | ||
self.times = [] | ||
self.cutoff = cutoff | ||
|
||
def append(self, time): | ||
ltimes = len(self.times) | ||
def check_in(i): | ||
if self.times[i][0] <= time <= self.times[i][1]: | ||
self.times[i] = (self.times[i][0], max(time + self.cutoff, self.times[i][1])) | ||
return True | ||
return False | ||
|
||
def maybe_merge(i): | ||
if ltimes > i + 1: | ||
if self.times[i][1] >= self.times[i+1][0]: | ||
self.times[i] = (self.times[i][0], self.times[i+1][1]) | ||
self.times.pop(i+1) | ||
|
||
if ltimes == 0: | ||
self.times.append((time, time + self.cutoff)) | ||
return | ||
|
||
i = bisect.bisect(self.times, (time,)) | ||
if i >= 1 and check_in(i - 1): | ||
maybe_merge(i - 1) | ||
elif i < ltimes and check_in(i): | ||
maybe_merge(i) | ||
else: | ||
self.times.insert(i, (time, time + self.cutoff)) | ||
maybe_merge(i) | ||
|
||
|
||
def extend(self, times): | ||
self.times.extend(times) | ||
for time in times: | ||
self.append(time) | ||
|
||
def calc_total(self, cutoff): | ||
self.times.sort() | ||
tot = 0 | ||
def calc_total(self): | ||
return sum(t2 - t1 for t1, t2 in self.times) | ||
|
||
last = -cutoff | ||
for t in self.times: | ||
tot += max(min(t - last, cutoff), 0) | ||
last = t | ||
return tot |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.