hyperactive 0.1.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +1 -0
- data/lib/hyperactive/list.rb +154 -0
- data/lib/hyperactive/record.rb +322 -297
- data/lib/hyperactive/tree.rb +175 -167
- data/lib/hyperactive.rb +1 -0
- data/tests/list_test.rb +76 -0
- data/tests/record_test.rb +17 -17
- data/tests/tree_benchmark.rb +8 -8
- data/tests/tree_test.rb +13 -13
- metadata +5 -2
data/README
CHANGED
@@ -16,6 +16,7 @@ Hyperactive::Tree:: A collection class that contains any number of Hyperactive::
|
|
16
16
|
To define a Record subclass that has the properties @active and @city that are indexed in two ways:
|
17
17
|
|
18
18
|
class MyClass < Hyperactive::Record
|
19
|
+
attr_accessor :active, :city
|
19
20
|
select(:active_records, Proc.new do |record|
|
20
21
|
record.active == true
|
21
22
|
end)
|
@@ -0,0 +1,154 @@
|
|
1
|
+
# Archipelago - a distributed computing toolkit for ruby
|
2
|
+
# Copyright (C) 2006 Martin Kihlgren <zond at troja dot ath dot cx>
|
3
|
+
#
|
4
|
+
# This program is free software; you can redistribute it and/or
|
5
|
+
# modify it under the terms of the GNU General Public License
|
6
|
+
# as published by the Free Software Foundation; either version 2
|
7
|
+
# of the License, or (at your option) any later version.
|
8
|
+
#
|
9
|
+
# This program is distributed in the hope that it will be useful,
|
10
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12
|
+
# GNU General Public License for more details.
|
13
|
+
#
|
14
|
+
# You should have received a copy of the GNU General Public License
|
15
|
+
# along with this program; if not, write to the Free Software
|
16
|
+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
17
|
+
|
18
|
+
|
19
|
+
require 'rubygems'
|
20
|
+
require 'archipelago'
|
21
|
+
|
22
|
+
module Hyperactive
|
23
|
+
|
24
|
+
#
|
25
|
+
# A package that simplifies putting dumb linked lists in the distributed database.
|
26
|
+
#
|
27
|
+
module List
|
28
|
+
|
29
|
+
#
|
30
|
+
# A List element.
|
31
|
+
#
|
32
|
+
class Element < Hyperactive::Record::Bass
|
33
|
+
attr_accessor :previous, :next, :value
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# A List head.
|
38
|
+
#
|
39
|
+
class Head < Hyperactive::Record::Bass
|
40
|
+
attr_reader :first_element, :last_element, :size
|
41
|
+
|
42
|
+
#
|
43
|
+
# Create a Head.
|
44
|
+
#
|
45
|
+
# NB: As usual, dont call this. Use Head.get_instance instead.
|
46
|
+
#
|
47
|
+
def initialize
|
48
|
+
@size = 0
|
49
|
+
@first_element = @last_element = nil
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Return the first value of the list.
|
54
|
+
#
|
55
|
+
def first
|
56
|
+
@first_element.value
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# Return the last value of the list.
|
61
|
+
#
|
62
|
+
def last
|
63
|
+
@last_element.value
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# Push +v+ onto the end of this list.
|
68
|
+
#
|
69
|
+
def <<(v)
|
70
|
+
if @first_element
|
71
|
+
new_element = Element.get_instance
|
72
|
+
new_element.value = v
|
73
|
+
new_element.previous = @last_element
|
74
|
+
@last_element.next = new_element
|
75
|
+
@last_element = new_element
|
76
|
+
else
|
77
|
+
start(v)
|
78
|
+
end
|
79
|
+
@size += 1
|
80
|
+
return v
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
# Push +v+ onto the beginning of this list.
|
85
|
+
#
|
86
|
+
def unshift(v)
|
87
|
+
if @first_element
|
88
|
+
new_element = Element.get_instance
|
89
|
+
new_element.value = v
|
90
|
+
new_element.next = @first_element
|
91
|
+
@first_element.previous = new_element
|
92
|
+
@first_element = new_element
|
93
|
+
else
|
94
|
+
start(v)
|
95
|
+
end
|
96
|
+
@size += 1
|
97
|
+
return v
|
98
|
+
end
|
99
|
+
|
100
|
+
#
|
101
|
+
# Remove the last value from this list and return it.
|
102
|
+
#
|
103
|
+
def pop
|
104
|
+
v = nil
|
105
|
+
if size > 1
|
106
|
+
element = @last_element
|
107
|
+
@last_element = element.previous
|
108
|
+
@last_element.next = nil
|
109
|
+
v = element.value
|
110
|
+
element.destroy
|
111
|
+
else
|
112
|
+
v = @first_element.value
|
113
|
+
@first_element.destroy
|
114
|
+
@first_element = @last_element = nil
|
115
|
+
end
|
116
|
+
@size -= 1
|
117
|
+
return v
|
118
|
+
end
|
119
|
+
|
120
|
+
#
|
121
|
+
# Remove the first value from this list and return it.
|
122
|
+
#
|
123
|
+
def shift
|
124
|
+
v = nil
|
125
|
+
if size > 1
|
126
|
+
element = @first_element
|
127
|
+
@first_element = element.next
|
128
|
+
@first_element.previous = nil
|
129
|
+
v = element.value
|
130
|
+
element.destroy
|
131
|
+
else
|
132
|
+
v = @first_element.value
|
133
|
+
@first_element.destroy
|
134
|
+
@first_element = @last_element = nil
|
135
|
+
end
|
136
|
+
@size -= 1
|
137
|
+
return v
|
138
|
+
end
|
139
|
+
|
140
|
+
private
|
141
|
+
|
142
|
+
#
|
143
|
+
# Start this list with +v+ when it has no other values.
|
144
|
+
#
|
145
|
+
def start(v)
|
146
|
+
@first_element = @last_element = Element.get_instance
|
147
|
+
@first_element.value = v
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|