Class: CW::Stream

Inherits:
Object
  • Object
show all
Includes:
Element
Defined in:
lib/cw/stream.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Element

#check_last_element_success, #count, #element, #get_first, #get_last, #inc_first_element, #inc_last_element, #match_first_active_element, #match_last_active_element, #process_last_active_element

Constructor Details

#initializeStream

Returns a new instance of Stream.



11
12
13
14
# File 'lib/cw/stream.rb', line 11

def initialize
  @active_region = 6
  empty
end

Instance Attribute Details

#active_regionObject

Returns the value of attribute active_region.



8
9
10
# File 'lib/cw/stream.rb', line 8

def active_region
  @active_region
end

#streamObject

Returns the value of attribute stream.



9
10
11
# File 'lib/cw/stream.rb', line 9

def stream
  @stream
end

Instance Method Details

#add_char(char) ⇒ Object



26
27
28
29
30
# File 'lib/cw/stream.rb', line 26

def add_char char
  @stream[@last_element] = char
  @success[@last_element] = nil
  inc_last_element
end

#emptyObject



16
17
18
# File 'lib/cw/stream.rb', line 16

def empty
  @stream, @success, @first_element, @last_element = {},{}, 0, 0
end

#fail_unmarked_inactive_elementsObject



52
53
54
55
56
57
58
# File 'lib/cw/stream.rb', line 52

def fail_unmarked_inactive_elements
  if(( ! stream_empty?) && (count > @active_region))
    @first_element.upto(inactive_region) do |count|
      @success[count] = false unless @success[count] == true
    end
  end
end

#firstObject



60
61
62
# File 'lib/cw/stream.rb', line 60

def first
  @stream[@first_element]
end

#inactive_regionObject



48
49
50
# File 'lib/cw/stream.rb', line 48

def inactive_region
  stream_empty? ? nil : @last_element - @active_region - 1
end

#mark(element, type) ⇒ Object



32
33
34
# File 'lib/cw/stream.rb', line 32

def mark(element, type)
  @success[element] = type
end

#mark_fail(element) ⇒ Object



40
41
42
# File 'lib/cw/stream.rb', line 40

def mark_fail element
  mark element, false
end

#mark_success(element) ⇒ Object



36
37
38
# File 'lib/cw/stream.rb', line 36

def mark_success element
  mark element, true
end

#popObject



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cw/stream.rb', line 64

def pop
  unless stream_empty?
    ele = @first_element
    inc_first_element
    success = @success.delete(ele)
    success = success == nil ? false : success
    { :value => @stream.delete(ele),
      :success => success
    }
  else
    nil
  end
end

#pop_next_markedObject

def pop_marked

  return_val = {}
  fail_unmarked_inactive_elements
  @first_element.upto(@last_element) do |ele|
    unless stream_empty?
      if(ele < inactive_region)
        val = pop

        return_val[ele] = {pop => @success[ele]}
      elsif(@success[ele] == true || @success[ele] == false)
        return_val[ele] = {pop => @success[ele]}
      else
        break
      end
    end
  end
  return_val == {} ? nil : return_val
end


97
98
99
100
101
102
103
104
105
106
# File 'lib/cw/stream.rb', line 97

def pop_next_marked
  fail_unmarked_inactive_elements
  unless stream_empty?
    if(@first_element < inactive_region)
      pop
    elsif(@success[@first_element] == true || @success[@first_element] == false)
      pop
    end
  end
end

#push(word) ⇒ Object



20
21
22
23
24
# File 'lib/cw/stream.rb', line 20

def push word
  @stream[@last_element] = word.strip
  @success[@last_element] = nil
  inc_last_element
end

#stream_empty?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/cw/stream.rb', line 44

def stream_empty?
  @first_element == @last_element
end