Factor out sorted list insertion into utility function
authorclinton <clinton@unknownlamer.org>
Sat, 15 Nov 2008 21:03:11 +0000 (21:03 +0000)
committerclinton <clinton@unknownlamer.org>
Sat, 15 Nov 2008 21:03:11 +0000 (21:03 +0000)
source/BotInterp.C
source/Utils.H

index 9a07be6..8b58d3e 100644 (file)
@@ -150,13 +150,7 @@ BotInterp::AddTimer(int delay, SCM function)
   scm_gc_protect_object(function);
 
   Timer *timer = new Timer (++counter, when, function);
-  TimerList::iterator it = std::find_if (timers.begin (), timers.end (),
-                                        std::bind1st (timer_sort_p, timer));
-  
-  if (it != timers.end ())
-    timers.insert (it, timer);
-  else
-    timers.push_back (timer);
+  Utils::push_sorted (timers, timer, timer_sort_p);
 
   return scm_from_int (counter);
 
index a68eabe..46e3762 100644 (file)
@@ -25,6 +25,8 @@
 #endif
 
 #include <ctime>
+#include <algorithm>
+#include <list>
 #include <string>
 
 class Bot;
@@ -80,6 +82,18 @@ namespace Utils {
     typedef T* second_argument_type;
     typedef bool result_type;
   };
+
+  template<typename T, typename C> 
+  void push_sorted (std::list<T> & storage, T item, C compare)
+  {
+    typename std::list<T>::iterator it = 
+      std::find_if (storage.begin (), storage.end (), 
+                   std::bind1st (compare, item));
+    if (it != storage.end ())
+      storage.insert (it, item);
+    else
+      storage.push_back (item);
+  }
 }
 
 #endif