MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / partitionWork

Function partitionWork

src/orchestration/partition.ts:119–160  ·  view source on GitHub ↗
(
  items: WorkItem<T>[],
  opts: PartitionOptions = {},
)

Source from the content-addressed store, hash-verified

117 * coupled items kept together, total weight balanced via LPT.
118 */
119export function partitionWork<T = unknown>(
120 items: WorkItem<T>[],
121 opts: PartitionOptions = {},
122): Partition<T>[] {
123 if (!items || items.length === 0) return [];
124
125 const units = buildUnits(items);
126 const totalWeight = units.reduce((s, x) => s + x.weight, 0);
127 const k = chooseK(units, totalWeight, opts);
128 if (k <= 1) {
129 // Single partition: keep original input order.
130 const all = units.flatMap((un) => un.members);
131 return [{ index: 0, items: all, totalWeight }];
132 }
133
134 // LPT: heaviest unit first; tie-break by first-appearance order then key for stability.
135 const sorted = [...units].sort((a, b) =>
136 b.weight - a.weight || a.order - b.order || (a.key < b.key ? -1 : a.key > b.key ? 1 : 0),
137 );
138
139 const bins: Array<{ weight: number; units: Unit<T>[] }> = Array.from({ length: k }, () => ({
140 weight: 0,
141 units: [],
142 }));
143
144 for (const unit of sorted) {
145 // Assign to the currently-lightest bin; tie-break by lowest bin index.
146 let best = 0;
147 for (let i = 1; i < bins.length; i++) {
148 if (bins[i]!.weight < bins[best]!.weight) best = i;
149 }
150 bins[best]!.units.push(unit);
151 bins[best]!.weight += unit.weight;
152 }
153
154 // Flatten each bin back to items, restoring original input order within the partition.
155 return bins.map((bin, index) => {
156 const flat = bin.units.flatMap((un) => un.members);
157 flat.sort((a, b) => items.indexOf(a) - items.indexOf(b));
158 return { index, items: flat, totalWeight: bin.weight };
159 });
160}
161
162/**
163 * Convenience: build WorkItems from plain string ids plus optional parallel

Callers 2

partition.test.tsFile · 0.85
executeMethod · 0.85

Calls 3

buildUnitsFunction · 0.85
chooseKFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected