项目作者: propan

项目描述 :
a persistent circular buffer
高级语言: Clojure
项目地址: git://github.com/propan/circular-buffer.git
创建时间: 2013-05-22T18:24:59Z
项目社区:https://github.com/propan/circular-buffer

开源协议:

下载


circular-buffer

An implementation of persistent circular buffer. Implements most of Clojure collections interfaces (and therefore supports
methods like conj, into, nth, etc) as well as Java collections interfaces.

The implementation provides the following options:

  • :type - defines the type of values, that will be stored in the buffer and therefore the type of the structure

    1. that backs the buffer (either **clojure.core.Vec** or **clojure.lang.PersistentVector**).
    2. Possible values are *:any :int :long :float :double :byte :short :char* or *:boolean*.
    3. **Default:** *:any*
  • :default - defines the default value in the buffer. If omitted depends on the type of values for which the

    1. buffer is configured.
    2. **Default:** *nil, 0, 0.0 or false*
  • :direction - defines the direction in which new elements are added into the buffer.

    1. **Default:** *:left*

Usage

Include the library in your leiningen project dependencies:

  1. [circular-buffer "0.1.0-SNAPSHOT"]

Examples

  1. (use 'hamakar.circular-buffer)
  2. ; defines a new circular buffer of size 5
  3. (def cb-left (cbuf 5)) ; #'hamakar.circular-buffer/cb-left
  4. cb-left ; [nil nil nil nil nil]
  5. (conj cb-left 4) ; [nil nil nil nil 4]
  6. (into cb-left [1 2 3 4 5 6 7 8]) ; [4 5 6 7 8]
  7. (subvec (into cb-left [1 2 3 4 5 6 7 8]) 2) ; [6 7 8]
  8. (def cb-left (cbuf 4 :type :int :default -1)) ; #'hamakar.circular-buffer/cb-left
  9. cb-left ; [-1 -1 -1 -1]
  10. (def cb-right (into (cbuf 4 :direction :right) [1 2 3 4])) ; #'hamakar.circular-buffer/cb-right
  11. cb-right ; [4 3 2 1]
  12. (pop cb-right) ; [3 2 1 nil]
  13. (peek cb-right) ; 4
  14. (= cb-right (list 4 3 2 1)) ; true
  15. (nth cb-right 0) ; 4

License

Copyright © 2013 Pavel Prokopenko

Distributed under the Eclipse Public License, the same as Clojure.