Class: CWG::CwStream

Inherits:
Object
  • Object
show all
Includes:
Element
Defined in:
/Users/martyn/cw/cw_clone/lib/cw/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

- (CwStream) initialize

Returns a new instance of CwStream



12
13
14
15
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 12

def initialize
  @active_region = 6
  empty
end

Instance Attribute Details

- (Object) active_region

Returns the value of attribute active_region



9
10
11
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 9

def active_region
  @active_region
end

- (Object) stream

Returns the value of attribute stream



10
11
12
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 10

def stream
  @stream
end

Instance Method Details

- (Object) add_char(char)



27
28
29
30
31
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 27

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

- (Object) add_word(word)



21
22
23
24
25
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 21

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

- (Object) empty



17
18
19
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 17

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

- (Object) fail_unmarked_inactive_elements



53
54
55
56
57
58
59
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 53

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

- (Object) first



61
62
63
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 61

def first
  @stream[@first_element]
end

- (Object) inactive_region



49
50
51
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 49

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

- (Object) mark(element, type)



33
34
35
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 33

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

- (Object) mark_fail(element)



41
42
43
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 41

def mark_fail element
  mark element, false
end

- (Object) mark_success(element)



37
38
39
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 37

def mark_success element
  mark element, true
end

- (Object) pop



65
66
67
68
69
70
71
72
73
74
75
76
77
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 65

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

- (Object) pop_next_marked

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


98
99
100
101
102
103
104
105
106
107
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 98

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

- (Boolean) stream_empty?

Returns:

  • (Boolean)


45
46
47
# File '/Users/martyn/cw/cw_clone/lib/cw/cw_stream.rb', line 45

def stream_empty?
  @first_element == @last_element
end