Implement --numbers in web client

This commit is contained in:
RunasSudo 2021-06-04 18:59:50 +10:00
parent b7f18a74ac
commit 441e266b17
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
4 changed files with 32 additions and 20 deletions

View File

@ -143,16 +143,18 @@
Numeric representation: Numeric representation:
</div> </div>
<div class="col-12"> <div class="col-12">
<!--<label> <label>
Numbers: Numbers:
<select id="selNumbers"> <select id="selNumbers">
<option value="native">Native</option> <!--<option value="native">Native</option>
<option value="rational">Rational</option> <option value="rational">Rational</option>
<option value="fixed" selected>Fixed</option> <option value="fixed" selected>Fixed</option>
<option value="gfixed">Fixed (guarded)</option> <option value="gfixed">Fixed (guarded)</option>-->
<option value="rational">Rational</option>
<option value="float64">Float (64-bit)</option>
</select> </select>
</label> </label>
<label> <!--<label>
Decimal places (if Numbers = Fixed): Decimal places (if Numbers = Fixed):
<input type="number" id="txtDP" value="5" min="0" style="width: 3em;"> <input type="number" id="txtDP" value="5" min="0" style="width: 3em;">
</label>--> </label>-->

View File

@ -61,6 +61,10 @@ worker.onmessage = function(evt) {
} }
} }
worker.onerror = function(evt) {
alert('An unknown error occurred while counting the votes. More details may be available in the browser\'s developer console.');
}
async function clickCount() { async function clickCount() {
if (document.getElementById('bltFile').files.length === 0) { if (document.getElementById('bltFile').files.length === 0) {
return; return;
@ -93,6 +97,7 @@ async function clickCount() {
'type': 'countElection', 'type': 'countElection',
'electionData': electionData, 'electionData': electionData,
'optsStr': optsStr, 'optsStr': optsStr,
'filePath': filePath 'filePath': filePath,
'numbers': document.getElementById('selNumbers').value,
}); });
} }

View File

@ -10,36 +10,43 @@ initWasm();
onmessage = function(evt) { onmessage = function(evt) {
if (evt.data.type === 'countElection') { if (evt.data.type === 'countElection') {
let numbers;
if (evt.data.numbers === 'rational') {
numbers = 'Rational';
} else if (evt.data.numbers === 'float64') {
numbers = 'NativeFloat64';
}
// Init election // Init election
let election = wasm.election_from_blt_Rational(evt.data.electionData); let election = wasm['election_from_blt_' + numbers](evt.data.electionData);
// Init results table // Init results table
postMessage({'type': 'initResultsTable', 'content': wasm.init_results_table_Rational(election)}); postMessage({'type': 'initResultsTable', 'content': wasm['init_results_table_' + numbers](election)});
// Init STV options // Init STV options
let opts = wasm.STVOptions.new.apply(null, evt.data.optsStr); let opts = wasm.STVOptions.new.apply(null, evt.data.optsStr);
// Describe count // Describe count
postMessage({'type': 'describeCount', 'content': wasm.describe_count_Rational(evt.data.filePath, election, opts)}); postMessage({'type': 'describeCount', 'content': wasm['describe_count_' + numbers](evt.data.filePath, election, opts)});
// Step election // Step election
let state = wasm.CountStateRational.new(election); let state = wasm['CountState' + numbers].new(election);
wasm.count_init_Rational(state, opts); wasm['count_init_' + numbers](state, opts);
postMessage({'type': 'updateResultsTable', 'result': wasm.update_results_table_Rational(1, state, opts)}); postMessage({'type': 'updateResultsTable', 'result': wasm['update_results_table_' + numbers](1, state, opts)});
postMessage({'type': 'updateStageComments', 'comment': wasm.update_stage_comments_Rational(state)}); postMessage({'type': 'updateStageComments', 'comment': wasm['update_stage_comments_' + numbers](state)});
for (let stageNum = 2;; stageNum++) { for (let stageNum = 2;; stageNum++) {
let isDone = wasm.count_one_stage_Rational(state, opts); let isDone = wasm['count_one_stage_' + numbers](state, opts);
if (isDone) { if (isDone) {
break; break;
} }
postMessage({'type': 'updateResultsTable', 'result': wasm.update_results_table_Rational(stageNum, state, opts)}); postMessage({'type': 'updateResultsTable', 'result': wasm['update_results_table_' + numbers](stageNum, state, opts)});
postMessage({'type': 'updateStageComments', 'comment': wasm.update_stage_comments_Rational(state)}); postMessage({'type': 'updateStageComments', 'comment': wasm['update_stage_comments_' + numbers](state)});
} }
postMessage({'type': 'updateResultsTable', 'result': wasm.finalise_results_table_Rational(state)}); postMessage({'type': 'updateResultsTable', 'result': wasm['finalise_results_table_' + numbers](state)});
postMessage({'type': 'finalResultSummary', 'summary': wasm.final_result_summary_Rational(state)}); postMessage({'type': 'finalResultSummary', 'summary': wasm['final_result_summary_' + numbers](state)});
} }
} }

View File

@ -208,9 +208,7 @@ impl ops::SubAssign<&NativeFloat64> for NativeFloat64 {
} }
impl ops::MulAssign<&NativeFloat64> for NativeFloat64 { impl ops::MulAssign<&NativeFloat64> for NativeFloat64 {
fn mul_assign(&mut self, _rhs: &NativeFloat64) { fn mul_assign(&mut self, rhs: &NativeFloat64) { self.0 *= &rhs.0 }
todo!()
}
} }
impl ops::DivAssign<&NativeFloat64> for NativeFloat64 { impl ops::DivAssign<&NativeFloat64> for NativeFloat64 {