From b1eccb3f3847a371a2df47a67bbfd702f3fff0a9 Mon Sep 17 00:00:00 2001 From: tom Date: Fri, 21 Dec 2007 16:08:21 -0800 Subject: [PATCH] Add comments to Timeline --- lib/god/timeline.rb | 83 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 36 deletions(-) rewrite lib/god/timeline.rb (95%) diff --git a/lib/god/timeline.rb b/lib/god/timeline.rb dissimilarity index 95% index 12c1904..7ad12a6 100644 --- a/lib/god/timeline.rb +++ b/lib/god/timeline.rb @@ -1,36 +1,47 @@ -module God - - class Timeline < Array - def initialize(max_size) - super() - @max_size = max_size - end - - # Push a value onto the Timeline - # - # Implementation explanation: - # A performance optimization appears here to speed up the push time. - # In essence, the code does this: - # - # def push(val) - # super(val) - # shift if size > @max_size - # end - # - # But that's super slow due to the shift, so we resort to reverse! and pop - # which gives us a 2x speedup with 100 elements and a 6x speedup with 1000 - def push(val) - if (size + 1) > @max_size - reverse! - pop - reverse! - end - super(val) - end - - def <<(val) - push(val) - end - end - -end \ No newline at end of file +module God + + class Timeline < Array + # Instantiate a new Timeline + # +max_size+ is the maximum size to which the timeline should grow + # + # Returns Timeline + def initialize(max_size) + super() + @max_size = max_size + end + + # Push a value onto the Timeline + # +val+ is the value to push + # + # Implementation explanation: + # A performance optimization appears here to speed up the push time. + # In essence, the code does this: + # + # def push(val) + # super(val) + # shift if size > @max_size + # end + # + # But that's super slow due to the shift, so we resort to reverse! and pop + # which gives us a 2x speedup with 100 elements and a 6x speedup with 1000 + # + # Returns Timeline + def push(val) + if (size + 1) > @max_size + reverse! + pop + reverse! + end + super(val) + end + + # Push a value onto the timeline + # +val+ is the value to push + # + # Returns Timeline + def <<(val) + push(val) + end + end + +end \ No newline at end of file -- 2.11.4.GIT