Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/appswarm/tools.rb,
lib/appswarm/parallel_each.rb,
lib/appswarm/routing/routing.rb
Defined Under Namespace
Classes: Groups
Instance Method Summary collapse
- #createPermutationFromOrder ⇒ Object
- #group_by(&block) ⇒ Object
- #map2hash(&b) ⇒ Object
- #oldUniq ⇒ Object
-
#p_each(&block) ⇒ Object
run each in parallel (WARNING: creates infinite threads !!!).
-
#p_map(&block) ⇒ Object
run map in parallel (WARNING: creates infinite threads !!!).
- #shuffle ⇒ Object
- #shuffle! ⇒ Object
- #uniq(&block) ⇒ Object
- #uniq_count(&b) ⇒ Object
Instance Method Details
#createPermutationFromOrder ⇒ Object
13 14 15 16 |
# File 'lib/appswarm/routing/routing.rb', line 13 def createPermutationFromOrder a=(0...length).to_a a.sort_by{|x|self[x]} end |
#group_by(&block) ⇒ Object
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/appswarm/tools.rb', line 167 def group_by(&block) groups=Groups.new each{|e| g=block.call(e) groups[g]||=[] groups[g] << e } groups end |
#map2hash(&b) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/appswarm/tools.rb', line 148 def map2hash(&b) h={} each{|e| res=b.call(e) h[e]=res } h end |
#oldUniq ⇒ Object
124 |
# File 'lib/appswarm/tools.rb', line 124 alias :oldUniq :uniq |
#p_each(&block) ⇒ Object
run each in parallel (WARNING: creates infinite threads !!!)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/appswarm/parallel_each.rb', line 5 def p_each(&block) threads=[] self.each{|element| thread = Thread.new(element,block){|element,block| block.call(element) } threads << thread } (threads.uniq-[Thread.current]).each{|t| assert{Thread.current!=t} t.join } threads.each{|t|t.kill!} end |
#p_map(&block) ⇒ Object
run map in parallel (WARNING: creates infinite threads !!!)
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 |
# File 'lib/appswarm/parallel_each.rb', line 21 def p_map(&block) threads=[] results=[nil]*length mutex=Mutex.new i=0 self.each{|element| t=Thread.start(element,block,results,mutex,i){|element,block,results,mutex,mi| j=mi begin res=block.call(element) mutex.synchronize { results[j]=res } rescue Object=>e pp e,e.stacktrace end } threads<<t i+=1 } assert{threads.uniq.length==length} threads.each{|t|t.join} assert{results.length==length} results end |
#shuffle ⇒ Object
145 146 147 |
# File 'lib/appswarm/tools.rb', line 145 def shuffle dup.sort_by {|a,b|rand} end |
#shuffle! ⇒ Object
142 143 144 |
# File 'lib/appswarm/tools.rb', line 142 def shuffle! sort_by {|a,b|rand} end |
#uniq(&block) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/appswarm/tools.rb', line 125 def uniq(&block) if block.nil? return oldUniq else a=[] ks=[] each{|v| k=block.call(v) unless ks.member?(k) ks<<k a<<v end } a end end |
#uniq_count(&b) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/appswarm/tools.rb', line 157 def uniq_count(&b) h={} each{|e| v=b.call(e) h[v]||=0 h[v]+=1 } h end |