# Path: DiscEvolution/io.py
# class Event_Controller(object):
#     """Handles booking keeping for events that occur at the specified times.
# 
#     Event types are specified through key word arguments, e.g.
#         Event_Controller(save=output_times, plot=plot_times)
#     where the values passed must be iterable lists of times.
#     """
#     def __init__(self, **events):
#         
#         self._events  = {}
#         self._event_number = {}
#         for key in events:
#             self._events[key] = sorted(events[key])
#             self._event_number[key] = 0
# 
#     def event_types(self):
#         return self._events.keys()
# 
#     def event_times(self, event_type):
#         """Return the times of the specified event type"""
#         return self._events[event_type]
# 
#     def next_event_time(self, event_type=None):
#         """Time of next event. 
# 
#         If no event type is specified, the next event of any time is returned
#         """
#         if event_type is not None:
#             return self._next_event_time(event_type)
#         else:
#             # All events:
#             t_next = np.inf
#             for event in self._events:
#                 t_next = min(t_next, self._next_event_time(event))
#             return t_next
# 
#     def _next_event_time(self, event):
#         try:
#             return self._events[event][0]
#         except IndexError:
#             return np.inf
# 
#     def next_event(self):
#         """The type of the next event"""
#         t_next = self.next_event_time()
#         for event in self._events:
#             if self._next_event_time(event) == t_next:
#                 return event    
#         return None
# 
#     def check_event(self, t, event):
#         """Has the next occurance of a specified event occured?"""
#         try:
#             return self._events[event][0] <= t
#         except IndexError:
#             return False
# 
#     def events_passed(self, t):
#         """Returns a list of event types that have passed since last pop"""
#         return [ e for e in self._events if self.check_event(t, e) ]
# 
#     def event_number(self, key):
#         """The number of times the specified event has occurred"""
#         return self._event_number[key]
# 
#     def pop_events(self, t, event_type=None):
#         """Remove events that have passed.
# 
#         If no event type is specified, pop all event types
#         """
#         if event_type is not None:
#             self._pop(t, event_type)
#         else:
#             for event in self._events:
#                 self._pop(t, event)
# 
#     def _pop(self, t, event):        
#         try:
#             while self.check_event(t, event):
#                 self._events[event].pop(0)
#                 self._event_number[event] += 1
#         except IndexError:
#             pass
#         
#     def finished(self):
#         """Returns True all events have been popped"""
#         for event in self._events:
#             if len(self._events[event]) > 0:
#                 return False
#         else:
#             return True


from DiscEvolution.io import  Event_Controller
# test_io.py
#
# Author: R. Booth
# Date: 22 - May - 2018
#
# Test input / output routines
###############################################################################

def test_event_controller():
    """Tests that event controller correctly generates events"""
    t0 = [0, 2, 4, 6, 8 ]
    t1 = [0, 3, 6, 9]

    # Label for the next possible events
    next_event = [ 'et', 'e', 'e', 't', 'e', 'et', 'et', 'e', 'e', 't']


# Path: DiscEvolution/io.py
# class Event_Controller(object):
#     """Handles booking keeping for events that occur at the specified times.
# 
#     Event types are specified through key word arguments, e.g.
#         Event_Controller(save=output_times, plot=plot_times)
#     where the values passed must be iterable lists of times.
#     """
#     def __init__(self, **events):
#         
#         self._events  = {}
#         self._event_number = {}
#         for key in events:
#             self._events[key] = sorted(events[key])
#             self._event_number[key] = 0
# 
#     def event_types(self):
#         return self._events.keys()
# 
#     def event_times(self, event_type):
#         """Return the times of the specified event type"""
#         return self._events[event_type]
# 
#     def next_event_time(self, event_type=None):
#         """Time of next event. 
# 
#         If no event type is specified, the next event of any time is returned
#         """
#         if event_type is not None:
#             return self._next_event_time(event_type)
#         else:
#             # All events:
#             t_next = np.inf
#             for event in self._events:
#                 t_next = min(t_next, self._next_event_time(event))
#             return t_next
# 
#     def _next_event_time(self, event):
#         try:
#             return self._events[event][0]
#         except IndexError:
#             return np.inf
# 
#     def next_event(self):
#         """The type of the next event"""
#         t_next = self.next_event_time()
#         for event in self._events:
#             if self._next_event_time(event) == t_next:
#                 return event    
#         return None
# 
#     def check_event(self, t, event):
#         """Has the next occurance of a specified event occured?"""
#         try:
#             return self._events[event][0] <= t
#         except IndexError:
#             return False
# 
#     def events_passed(self, t):
#         """Returns a list of event types that have passed since last pop"""
#         return [ e for e in self._events if self.check_event(t, e) ]
# 
#     def event_number(self, key):
#         """The number of times the specified event has occurred"""
#         return self._event_number[key]
# 
#     def pop_events(self, t, event_type=None):
#         """Remove events that have passed.
# 
#         If no event type is specified, pop all event types
#         """
#         if event_type is not None:
#             self._pop(t, event_type)
#         else:
#             for event in self._events:
#                 self._pop(t, event)
# 
#     def _pop(self, t, event):        
#         try:
#             while self.check_event(t, event):
#                 self._events[event].pop(0)
#                 self._event_number[event] += 1
#         except IndexError:
#             pass
#         
#     def finished(self):
#         """Returns True all events have been popped"""
#         for event in self._events:
#             if len(self._events[event]) > 0:
#                 return False
#         else:
#             return True


# Path: tests/test_io.py
from DiscEvolution.io import  Event_Controller

# test_io.py
#
# Author: R. Booth
# Date: 22 - May - 2018
#
# Test input / output routines
###############################################################################

def test_event_controller():
    """Tests that event controller correctly generates events"""
    t0 = [0, 2, 4, 6, 8 ]
    t1 = [0, 3, 6, 9]

    # Label for the next possible events
    next_event = [ 'et', 'e', 'e', 't', 'e', 'et', 'et', 'e', 'e', 't']


    EC = Event_Controller(evens=t0, threes=t1)