File: sliding.rb

package info (click to toggle)
ruby-concurrent 1.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,136 kB
  • sloc: ruby: 30,875; java: 6,128; ansic: 265; makefile: 26; sh: 19
file content (55 lines) | stat: -rw-r--r-- 1,614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
50
51
52
53
54
55
require 'concurrent/channel/buffer/base'
require 'concurrent/channel/buffer/buffered'

module Concurrent
  class Channel
    module Buffer

      # A non-blocking, buffered buffer of fixed maximum capacity. When the
      # maximum capacity is reached subsequent {#put} and {#offer} operations
      # will complete and the item will be `put`, but the oldest elements in
      # the buffer will be discarded (not transferred).
      class Sliding < Buffered

        # @!method put(item)
        #   @!macro channel_buffer_put
        #
        #   When the buffer is full, this method will return `true`
        #   immediately and the item will be inserted, but the oldest
        #   elements in the buffer will be discarded (not transferred).

        # @!method offer(item)
        #   @!macro channel_buffer_offer
        #
        #   When the buffer is full, this method will return `true`
        #   immediately and the item will be inserted, but the oldest
        #   elements in the buffer will be discarded (not transferred).

        # @!method full?
        #   @!macro channel_buffer_full_question
        #
        #   Always returns `false`.

        # @!macro channel_buffer_blocking_question
        #
        # Always returns `false`.
        def blocking?
          false
        end

        private

        # @!macro channel_buffer_full_question
        def ns_full?
          false
        end

        # @!macro channel_buffer_put
        def ns_put_onto_buffer(item)
          buffer.shift if buffer.size == capacity
          buffer.push(item)
        end
      end
    end
  end
end