Class: Partitions::SetPartitions

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/partitions/set_partitions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(n, p = nil) ⇒ SetPartitions

Returns a new instance of SetPartitions.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/partitions/set_partitions.rb', line 6

def initialize(n, p = nil)
  raise ArgumentError, "n must be a non-negative Integer" unless n.is_a?(Integer) && n >= 0
  if !p.nil? && (!p.is_a?(Integer) || p < 1 || p > n)
    raise ArgumentError, "p must be an Integer between 1 and n"
  end

  @n = n
  @k = Array.new(n, 0)
  @m = Array.new(n, 0)
  @p = p
  pinitialize
  @size = partition_size
end

Instance Attribute Details

#sizeObject

Returns the value of attribute size.



4
5
6
# File 'lib/partitions/set_partitions.rb', line 4

def size
  @size
end

Instance Method Details

#countObject



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/partitions/set_partitions.rb', line 116

def count
  if @p.nil?
    sum = 0
    (1..n).each do |k|
      sum += sterling_second(@n, k)
    end
    sum
  else
    sterling_second(@n, @p)
  end
end

#each_partition {|@k.clone| ... } ⇒ Object Also known as: each

Yields:

  • (@k.clone)


104
105
106
107
108
109
110
111
112
113
# File 'lib/partitions/set_partitions.rb', line 104

def each_partition
  return enum_for(:each_partition) unless block_given?

  reinitialize
  yield @k.clone
  (count - 1).times do
    yield next_partition
  end
  reinitialize
end

#next_partitionObject



20
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/partitions/set_partitions.rb', line 20

def next_partition
  if @p.nil?
    (@n - 1).downto(1) do |i|
      if @k[i] <= @m[i - 1]
        @k[i] = @k[i] + 1
        @m[i] = max(@m[i], @k[i])
        j = i + 1
        while j <= (n - 1) do
          @k[j] = @k[0]
          @m[j] = @m[i]
          j = j + 1
        end
        @size = partition_size
        return @k.clone
      end
    end
  else
    p = partition_size
    (@n - 1).downto(1) do |i|
      if (@k[i] < (p - 1)) && (@k[i] <= @m[i - 1])
        @k[i] = @k[i] + 1
        @m[i] = max(@m[i], @k[i])
        j = i + 1
        while j <= (@n - (p - @m[i])) do
          @k[j] = 0
          @m[j] = @m[i]
          j = j + 1
        end
        j = @n - (p - @m[i]) + 1
        while j <= (n - 1) do
          @k[j] = @m[j] = p - (@n - j)
          j = j + 1
        end
        @size = partition_size
        return @k.clone
      end
    end
  end

  @size = nil
  return 
end

#previous_partitionObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/partitions/set_partitions.rb', line 63

def previous_partition
  if @p.nil?
    (@n - 1).downto(1) do |i|
      if @k[i] > @k[0]
        @k[i] = @k[i] - 1
        @m[i] = @m[i - 1]
        j = i + 1
        while j <= n - 1 do
          @k[j] = @m[j] = @m[i] + j - i;
          j = j + 1
        end
        @size = partition_size
        return @k.clone
      end
    end
  else
    p = partition_size
    (@n - 1).downto(1).each do |i|
      if (@k[i] > 0) && ((p - @m[i - 1]) <= (n - i))
        @k[i] = @k[i] - 1
        @m[i] = @m[i - 1]
        j = i + 1
        while j <= (i + (p - @m[i]) - 1) do
          @k[j] = @m[j] = @m[i] + j - i
          j = j + 1
        end
        j = i + (p - @m[i])
        while j <= (@n - 1) do
          @k[j] = @m[j] = p - 1
          j = j + 1
        end
        @size = partition_size
        return @k.clone
      end
    end
  end

  @size = nil
  return
end