Class: Quicopt::Partition

Inherits:
Object
  • Object
show all
Defined in:
lib/quicopt/structured.rb

Overview

A partition declared by Model#partition: n_items items in n_parts parts, both numbered from 0. Read it with part_of and size_of.

teams = m.partition("teams", 6, 2)
teams.part_of(4)     # the team person 4 joins
teams.size_of(0)     # how many people team 0 has

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, n_items, n_parts, start) ⇒ Partition

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Declared with Model#partition, not built directly.



194
195
196
197
198
199
200
201
# File 'lib/quicopt/structured.rb', line 194

def initialize(model, name, n_items, n_parts, start)
  @model = model
  @name = name
  @n_items = n_items
  @n_parts = n_parts
  @start = start&.freeze
  @reads = {}
end

Instance Attribute Details

#model ⇒ Model (readonly)

Returns the model that declared this partition.

Returns:

  • (Model) —

    the model that declared this partition



180
181
182
# File 'lib/quicopt/structured.rb', line 180

def model
  @model
end

#n_items ⇒ Integer (readonly)

Returns the number of items.

Returns:

  • (Integer) —

    the number of items



184
185
186
# File 'lib/quicopt/structured.rb', line 184

def n_items
  @n_items
end

#n_parts ⇒ Integer (readonly)

Returns the number of parts.

Returns:

  • (Integer) —

    the number of parts



186
187
188
# File 'lib/quicopt/structured.rb', line 186

def n_parts
  @n_parts
end

#name ⇒ String (readonly)

Returns the partition's name; its assignment comes back keyed by it.

Returns:

  • (String) —

    the partition's name; its assignment comes back keyed by it



182
183
184
# File 'lib/quicopt/structured.rb', line 182

def name
  @name
end

#start ⇒ Array<Integer>? (readonly)

Returns the part each item starts in, or nil for the default (item i in part i % n_parts).

Returns:

  • (Array<Integer>, nil) —

    the part each item starts in, or nil for the default (item i in part i % n_parts)



189
190
191
# File 'lib/quicopt/structured.rb', line 189

def start
  @start
end

Instance Method Details

#inspect ⇒ String

Returns name, number of items and number of parts.

Returns:

  • (String) —

    name, number of items and number of parts



224
225
226
# File 'lib/quicopt/structured.rb', line 224

def inspect
  "#<Quicopt::Partition #{@name} (#{@n_items} items in #{@n_parts} parts)>"
end

#part_of(item) ⇒ Expression

The part item item is in, an expression whose value the service chooses. Index a LookupTable with it to read data about that part.

Parameters:

  • item (Integer) —

    from 0 to n_items - 1

Returns:

Raises:

  • (TypeError, ArgumentError) —

    if item is not one of the items



209
210
211
# File 'lib/quicopt/structured.rb', line 209

def part_of(item)
  read(:part_of, Positions.check(item, @n_items, "item", owner))
end

#size_of(part) ⇒ Expression

The number of items in part part, an expression whose value the service chooses: a count from 0 to n_items.

Parameters:

  • part (Integer) —

    from 0 to n_parts - 1

Returns:

Raises:

  • (TypeError, ArgumentError) —

    if part is not one of the parts



219
220
221
# File 'lib/quicopt/structured.rb', line 219

def size_of(part)
  read(:size_of, Positions.check(part, @n_parts, "part", owner))
end