Class: FSArray

Inherits:
Array
  • Object
show all
Defined in:
lib/fsfifo.rb

Overview

Fixed Size FIFO, LIFO, and Array.

Direct Known Subclasses

FSFIFO, FSLIFO

Constant Summary collapse

FSARRAY_SIZE_DEFAULT =
1

Instance Method Summary collapse

Constructor Details

#initialize(_size = FSARRAY_SIZE_DEFAULT, size: FSARRAY_SIZE_DEFAULT, of_enable: false, of_proc: Proc.new{ raise "Overflow!" }, uf_enable: false, uf_proc: Proc.new{ raise "Underflow!" }, push_enable: false, push_proc: Proc.new{}, wm_size: 1, wm_enable: false, wm_proc: Proc.new{ raise "Reached WM!" }) ⇒ FSArray

FSArray#new( n ) FSArray#new( size: n, of_enable: true, of_proc: Proc.new… )



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/fsfifo.rb', line 18

def initialize( _size = FSARRAY_SIZE_DEFAULT,
                size: FSARRAY_SIZE_DEFAULT,
                of_enable: false, of_proc: Proc.new{ raise "Overflow!" },
                uf_enable: false, uf_proc: Proc.new{ raise "Underflow!" },
                push_enable: false, push_proc: Proc.new{},
                wm_size: 1,
                wm_enable: false, wm_proc: Proc.new{ raise "Reached WM!" }
              )

  raise "Watermark #{wm_size} is out of range[0,#{size}]" if
    wm_size < 0 or wm_size > size

  size = _size if _size != FSARRAY_SIZE_DEFAULT
  @opt = {
    :size    => size,
    :of_enable => of_enable,
    :of_proc => of_proc,
    :uf_enable => uf_enable,
    :uf_proc => uf_proc,
    :push_enable => push_enable, #TODO.
    :push_proc => push_proc,
    :wm_size => wm_size,
    :wm_enable => wm_enable,
    :wm_proc => wm_proc,
  }

  @shifted = nil  #TODO.

  #
  super( )

end

Instance Method Details

#fifosizeObject

FIFO and data size-related methods.



65
# File 'lib/fsfifo.rb', line 65

def fifosize; @opt[:size]; end

#push(obj) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fsfifo.rb', line 93

def push( obj )

  #
  push_org( obj )

  #
  of_called = false
  wm_called = false
  while self.size > @opt[:size]
    #
    shift_org

    # overflow callback.
    if @opt[:of_enable] and not(of_called)
      @opt[:of_proc].call
      of_called = true
    end

    # TODO.watermark callback.
    if @opt[:wm_enable] and not(wm_called)
      @opt[:wm_proc].call
      wm_called = true
    else
      #raise ""
    end
  end

  return self
end

#push_orgObject



78
# File 'lib/fsfifo.rb', line 78

alias :push_org :push

#resize(n) ⇒ Object

Array#size indicates the number of elements in fifo.



67
68
69
70
71
72
73
74
# File 'lib/fsfifo.rb', line 67

def resize( n )
  ret = []
  @opt[:wm_size] = n if @opt[:size] == @opt[:wm_size]
  @opt[:size] = n
  ret << self.shift_org while self.size > n

  return ret
end

#shiftObject

shift and push - main methods of FSFIFO.



83
84
85
86
87
88
89
90
91
# File 'lib/fsfifo.rb', line 83

def shift

  # callback method when under flow is enabled.
  @opt[:uf_proc].call if self.size == 0 and @opt[:uf_enable]

  ret = shift_org

  return ret
end

#shift_orgObject



77
# File 'lib/fsfifo.rb', line 77

alias :shift_org :shift