Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 4,481 Bytes
a8fb296 | 1 2 3 4 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 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 62 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 103 104 | /**
* Splitting a state's boxes on the home box map by real speaker counts (#1087).
*
* Every figure here comes from the counts it is given: a state with no speakers gets no split, and the boxes always sum to
* the state's own box total (largest-remainder rounding). Nothing is estimated.
*/
/** The speaker sides the home map splits by, in the order their bands are drawn. */
export const STANCE_ORDER = ['against', 'for', 'question', 'neutral'] as const
export type Stance = (typeof STANCE_ORDER)[number]
export interface StanceCounts {
speakers_for: number
speakers_against: number
speakers_question: number
speakers_neutral: number
}
export const stanceCount = (s: StanceCounts, k: Stance): number =>
k === 'for' ? s.speakers_for : k === 'against' ? s.speakers_against : k === 'question' ? s.speakers_question : s.speakers_neutral
export const speakersOf = (s: StanceCounts | null | undefined): number =>
s ? STANCE_ORDER.reduce((n, k) => n + Math.max(0, stanceCount(s, k) || 0), 0) : 0
/**
* How many of `total` boxes each side gets, in proportion to its real count (largest remainder). A side with at least one
* speaker gets at least one box when there are enough boxes for every such side, taken from the side with the most boxes.
* Returns all zeros when there are no speakers or no boxes.
*/
export function splitBoxes(s: StanceCounts | null | undefined, total: number): Record<Stance, number> {
const out: Record<Stance, number> = { against: 0, for: 0, question: 0, neutral: 0 }
const sum = speakersOf(s)
if (!s || sum <= 0 || total <= 0) return out
const rem: Array<{ k: Stance; r: number; c: number }> = []
let used = 0
for (const k of STANCE_ORDER) {
const c = Math.max(0, stanceCount(s, k) || 0)
const q = (c / sum) * total
out[k] = Math.floor(q)
used += out[k]
rem.push({ k, r: q - out[k], c })
}
// Largest remainder first; ties go to the larger count, then the drawing order (stable).
rem.sort((a, b) => b.r - a.r || b.c - a.c)
for (let i = 0; used < total; i = (i + 1) % rem.length) {
out[rem[i]!.k] += 1
used += 1
}
const nonzero = STANCE_ORDER.filter((k) => (stanceCount(s, k) || 0) > 0)
if (total >= nonzero.length) {
for (const k of nonzero) {
if (out[k] > 0) continue
const donor = STANCE_ORDER.reduce((a, b) => (out[b] > out[a] ? b : a))
if (out[donor] <= 1) break
out[donor] -= 1
out[k] = 1
}
}
return out
}
/**
* The side of each box, in box order: `boxes` boxes filled band by band (against, for, question, neutral) from `splitBoxes`.
* Sort the boxes (e.g. by x) before mapping so each side forms one contiguous band. Null for every box when there's no split.
*/
export function bandSides(s: StanceCounts | null | undefined, boxes: number): Array<Stance | null> {
const split = splitBoxes(s, boxes)
const out: Array<Stance | null> = []
for (const k of STANCE_ORDER) for (let i = 0; i < split[k]; i++) out.push(k)
while (out.length < boxes) out.push(null)
return out
}
/**
* The real speaker sums across several places (e.g. a county's governments on the state drill-down, #1106). Missing or
* negative counts add nothing; with no places, every side is 0.
*/
export function sumStances(places: ReadonlyArray<Partial<StanceCounts>>): StanceCounts {
const n = (v: number | undefined) => Math.max(0, v || 0)
return places.reduce<StanceCounts>(
(a, p) => ({
speakers_for: a.speakers_for + n(p.speakers_for),
speakers_against: a.speakers_against + n(p.speakers_against),
speakers_question: a.speakers_question + n(p.speakers_question),
speakers_neutral: a.speakers_neutral + n(p.speakers_neutral),
}),
{ speakers_for: 0, speakers_against: 0, speakers_question: 0, speakers_neutral: 0 },
)
}
/**
* The side of each dot in one area, in the order given (#1106): the dots are banded left to right, then top to bottom (as the
* home map does), so each side is one contiguous band. Null for every dot when the area has no speakers on record.
*/
export function dotSides(dots: ReadonlyArray<{ x: number; y: number }>, s: StanceCounts | null | undefined): Array<Stance | null> {
const order = dots.map((_, i) => i).sort((a, b) => dots[a]!.x - dots[b]!.x || dots[a]!.y - dots[b]!.y)
const sides = bandSides(s, dots.length)
const out: Array<Stance | null> = new Array(dots.length).fill(null)
order.forEach((i, rank) => {
out[i] = sides[rank] ?? null
})
return out
}
|