Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/wordRS-lib.rb

Direct Known Subclasses

StatArray

Instance Method Summary collapse

Instance Method Details

#ranksObject

return the (sorted) ranks of the elements



70
71
72
73
74
# File 'lib/wordRS-lib.rb', line 70

def ranks()
  h = Hash.new {|h,k| h[k]=[]}
  self.sort.each_with_index{|x,idx| h[x] << idx}
  self.map{|x| h[x].first + (h[x].size-1)/2.0}
end

#rep_perm(n) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wordRS-lib.rb', line 52

def rep_perm(n)
  if n < 0
  elsif n == 0
    yield([])
  else
    rep_perm(n - 1) do |x|
      each do |y|
          yield(x + [y])
        end
    end
  end
end

#shuffleObject



42
43
44
45
46
47
48
49
# File 'lib/wordRS-lib.rb', line 42

def shuffle()
  arr = self.dup
  arr.size.downto 2 do |j|
    r = Kernel::rand(j)
    arr[j-1], arr[r] = arr[r], arr[j-1]
  end
  arr
end

#threach(n = 1, &b) ⇒ Object

threaded each



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
# File 'lib/wordRS-lib.rb', line 5

def threach(n = 1, &b)
  return [] if n == 0 or size == 0
  result = Array.new(size)
  return self.send(:each,&b) if n == 1 # trying return here
  
  n = [n,size].min
  
  part_size, part_remainder = size/n, size % n
  threads = []

  pstart = 0
  n.times do |pi|
    pend = pstart + part_size - 1
    pend += 1 if pi<part_remainder
    threads << Thread.new(pstart,pend) do |a,b|
      for j in a..b
        yield(slice(j))
      end
    end
    pstart = pend+1
  end
  
  threads.each { |t| t.join }
  self
end

#to_statarrayObject



65
66
67
# File 'lib/wordRS-lib.rb', line 65

def to_statarray
  StatArray.new(self)
end