public TimeEvent(TimeSpan _time, callback _funcCallBack) { this.time = _time; this.funcCB = _funcCallBack; this.dataCB = null; this.next = null; this.prev = null; }
private Timer() { this.active = null; this.inActive = null; this.initalized = false; this.totalNumEvents = 0; this.deltaGrow = 3; this.currTime = TimeSpan.Zero; }
public static TimeEvent Add(TimeSpan time, object obj, TimeEvent.callback callbackFunc) { TimeEvent te; te = Instance().privAddEvent(time, obj, callbackFunc); return te; }
// private: (reusable for other classes, just change types0: ------------------------------------ private void privInActiveAddToFront(TimeEvent node, ref TimeEvent head) { Debug.Assert(node != null); if (head == null) { head = node; node.next = null; node.prev = null; } else { node.next = head; head.prev = node; head = node; } }
private TimeEvent privInActiveGetNodeFromFront(ref TimeEvent head) { Debug.Assert(head != null); TimeEvent node = head; head = node.next; if (node.next != null) { node.next.prev = node.prev; } return node; }
private void privFillPool( int count ) { // add to the count this.totalNumEvents += count; // allocate timeEvents for (int i = 0; i < count; i++) { // create a new timeEvent // TimeEvent te = new TimeEvent(TimeZero, null); TimeEvent te = new TimeEvent(TimeSpan.Zero, null); // move it to InActive list privInActiveAddToFront(te, ref this.inActive); } }
// private: unique methods for this class: ------------------------------------------------------ private TimeEvent privAddEvent(TimeSpan _time, object obj, TimeEvent.callback _callbackFunc) { // Do we need to get more TimeEvents? if (this.inActive == null) { // fill pool with more nodes // OK now preload the timeEvents this.privFillPool( this.deltaGrow ); } // make sure we have TimeEvents Debug.Assert(this.inActive != null); // Detach 1 TimeEvent from the pool TimeEvent te; te = privInActiveGetNodeFromFront(ref this.inActive); // Initialize our TimeEvent te.next = null; te.prev = null; te.dataCB = obj; te.time = _time; te.funcCB = _callbackFunc; // need to add the TimeEvent to active; // insert in TimeSpan order (lowest closes to head) TimeEvent pEvent = this.active; if (this.active == null) { // nothing on the list, just insert this.active = te; } else { // walk the list and insert before TimeEvent LastEvent = pEvent; while ( pEvent != null ) { if (te.time <= pEvent.time) { // insert before pEvent break; } // goto next LastEvent = pEvent; pEvent = pEvent.next; } // add the new event to active list if (pEvent != null) { // install before if (pEvent.prev == null) { // 1st on list te.next = this.active; this.active.prev = te; this.active = te; } else { // middle of list LastEvent.next = te; te.prev = LastEvent; pEvent.prev = te; te.next = pEvent; } } else { // install after: (last on list) LastEvent.next = te; te.prev = LastEvent; } } return te; }
private void privActiveRemoveNode(TimeEvent node, ref TimeEvent head) { if (node.prev != null) { // middle or last node node.prev.next = node.next; } else { // first head = node.next; } if (node.next != null) { // middle node node.next.prev = node.prev; } }
public static void Remove( TimeEvent tE ) { Debug.Assert(tE != null); Timer pTimer = Instance(); pTimer.privActiveRemoveNode(tE, ref pTimer.active); }
// private: unique methods for this class: ------------------------------------------------------ private TimeEvent privAddEvent(TimeSpan _time, object obj, TimeEvent.callback _callbackFunc) { // Do we need to get more TimeEvents? if (this.inActive == null) { // fill pool with more nodes // OK now preload the timeEvents this.privFillPool(this.deltaGrow); } // make sure we have TimeEvents Debug.Assert(this.inActive != null); // Detach 1 TimeEvent from the pool TimeEvent te; te = privInActiveGetNodeFromFront(ref this.inActive); // Initialize our TimeEvent te.next = null; te.prev = null; te.dataCB = obj; te.time = _time; te.funcCB = _callbackFunc; // need to add the TimeEvent to active; // insert in TimeSpan order (lowest closes to head) TimeEvent pEvent = this.active; if (this.active == null) { // nothing on the list, just insert this.active = te; } else { // walk the list and insert before TimeEvent LastEvent = pEvent; while (pEvent != null) { if (te.time <= pEvent.time) { // insert before pEvent break; } // goto next LastEvent = pEvent; pEvent = pEvent.next; } // add the new event to active list if (pEvent != null) { // install before if (pEvent.prev == null) { // 1st on list te.next = this.active; this.active.prev = te; this.active = te; } else { // middle of list LastEvent.next = te; te.prev = LastEvent; pEvent.prev = te; te.next = pEvent; } } else { // install after: (last on list) LastEvent.next = te; te.prev = LastEvent; } } return(te); }