revolver 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.markdown +95 -0
- data/lib/revolver.rb +42 -0
- metadata +48 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Michael Kohl
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
20
|
+
|
data/README.markdown
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
Description
|
2
|
+
---
|
3
|
+
|
4
|
+
A fixed size LIFO data structure, optionally free of duplicates. They feel a lot like arrays, but differ in some important areas like indexing. This is e.g. useful for keeping a history of recent actions.
|
5
|
+
|
6
|
+
Usage
|
7
|
+
---
|
8
|
+
|
9
|
+
Create a new `Revolver`:
|
10
|
+
|
11
|
+
r = Revolver.new(3)
|
12
|
+
#=> #<Revolver: []>
|
13
|
+
|
14
|
+
Create a `Revolver` with unique elements:
|
15
|
+
|
16
|
+
r = Revolver.new(3, true)
|
17
|
+
#=> #<Revolver: []>
|
18
|
+
|
19
|
+
Alternatively, you can create a `Revolver` from an existing array:
|
20
|
+
|
21
|
+
Revolver.from_array([*1..3], 3)
|
22
|
+
#=> #<Revolver: [1, 2, 3]>
|
23
|
+
|
24
|
+
You can also create a unique `Revolver` that way:
|
25
|
+
|
26
|
+
Revolver.from_array([1, 1, 3], true)
|
27
|
+
#=> #<Revolver: [1, 3]>
|
28
|
+
|
29
|
+
Add elements:
|
30
|
+
|
31
|
+
r = Revolver.new(3).push(1).push(2).push(3)
|
32
|
+
#=> #<Revolver: [1, 2, 3]>
|
33
|
+
r.push(4)
|
34
|
+
#=> #<Revolver: [2, 3, 4]>
|
35
|
+
r << 5 << 6
|
36
|
+
#=> #<Revolver: [4, 5, 6]>
|
37
|
+
|
38
|
+
Remove an element from the end:
|
39
|
+
|
40
|
+
r.pop
|
41
|
+
#=> 6
|
42
|
+
|
43
|
+
Check a `Revolver`'s size:
|
44
|
+
|
45
|
+
r.size
|
46
|
+
#=> 2
|
47
|
+
|
48
|
+
A `Revolver` can be indexed like an array:
|
49
|
+
|
50
|
+
r = Revolver.from_array([*1..5])
|
51
|
+
#=> #<Revolver: [1, 2, 3, 4, 5]>
|
52
|
+
r[4]
|
53
|
+
#=> 5
|
54
|
+
|
55
|
+
However, indices wrap around:
|
56
|
+
|
57
|
+
r[10]
|
58
|
+
#=> 1
|
59
|
+
|
60
|
+
This also works for negative indices:
|
61
|
+
|
62
|
+
r[-1]
|
63
|
+
#=> 5
|
64
|
+
r[-6]
|
65
|
+
#=> 1
|
66
|
+
r[-8]
|
67
|
+
#=> 3
|
68
|
+
|
69
|
+
Finally you can get a plain array out of a `Revolver`:
|
70
|
+
|
71
|
+
r.to_a
|
72
|
+
#=> [1, 2, 3, 4, 5]
|
73
|
+
|
74
|
+
License
|
75
|
+
---
|
76
|
+
|
77
|
+
Copyright (c) 2012 Michael Kohl
|
78
|
+
|
79
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
80
|
+
of this software and associated documentation files (the "Software"), to deal
|
81
|
+
in the Software without restriction, including without limitation the rights
|
82
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
83
|
+
copies of the Software, and to permit persons to whom the Software is
|
84
|
+
furnished to do so, subject to the following conditions:
|
85
|
+
|
86
|
+
The above copyright notice and this permission notice shall be included in
|
87
|
+
all copies or substantial portions of the Software.
|
88
|
+
|
89
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
90
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
91
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
92
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
93
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
94
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
95
|
+
THE SOFTWARE.
|
data/lib/revolver.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
class Revolver
|
2
|
+
def self.from_array(array, unique = false)
|
3
|
+
rev = Revolver.new(array.size, unique)
|
4
|
+
array.inject(rev) { |r, e| r << e }
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(size, unique = false)
|
8
|
+
@array = []
|
9
|
+
@size = size
|
10
|
+
@unique = unique
|
11
|
+
end
|
12
|
+
|
13
|
+
def push(e)
|
14
|
+
@array.delete(e) if @unique
|
15
|
+
@array << e
|
16
|
+
@array.shift if @array.size > @size
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def pop
|
21
|
+
@array.pop
|
22
|
+
end
|
23
|
+
|
24
|
+
def [](i)
|
25
|
+
@array[i % @size]
|
26
|
+
end
|
27
|
+
|
28
|
+
def size
|
29
|
+
@array.size
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_a
|
33
|
+
@array
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s
|
37
|
+
"#<Revolver: #{@array.inspect}>"
|
38
|
+
end
|
39
|
+
|
40
|
+
alias :<< :push
|
41
|
+
alias :to_ary :to_a
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: revolver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Michael Kohl
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: citizen428@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/revolver.rb
|
21
|
+
- ./LICENSE
|
22
|
+
- ./README.markdown
|
23
|
+
homepage: http://citizen428.github.com/revolver/
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.23
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: A fixed-size LIFO data structure
|
47
|
+
test_files: []
|
48
|
+
has_rdoc: false
|