mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: simplify contest rank function
This commit is contained in:
1 parent
1397f91f1e
commit
2ffaf9201f
2 files changed
+8
-42
No files matched your search
@@ -1,20 +1,14 @@
|
|||||||
import { Cursor } from 'mongodb';
|
import { Cursor } from 'mongodb';
|
||||||
import paginate from './paginate';
|
import paginate from './paginate';
|
||||||
|
|
||||||
type G<T> = Promise<[[number, T], () => G<T>] | []>;
|
type ranked = <T>(cursor: Cursor<T>, equ: ((a: T, b: T) => boolean), page?: number) => Promise<[[number, T][], number]>;
|
||||||
interface ranked {
|
|
||||||
<T>(cursor: Cursor<T>, equ: (a: T, b: T) => boolean, page: number): Promise<[[number, T][], number]>;
|
|
||||||
iterate<T>(cursor: Cursor<T>, equ: (a: T, b: T) => boolean): G<T>;
|
|
||||||
all<T>(cursor: Cursor<T>, equ: (a: T, b: T) => boolean): Promise<[[number, T][], 1]>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ranked: ranked = async function rankedPage(cursor, equ = (a, b) => a === b, page) {
|
const ranked: ranked = async (cursor, equ, page) => {
|
||||||
if (!page) return await ranked.all(cursor, equ);
|
|
||||||
let last = null;
|
let last = null;
|
||||||
let r = (page - 1) * 100;
|
let r = page ? (page - 1) * 100 : 0;
|
||||||
let count = r;
|
let count = page ? r : 0;
|
||||||
const results = [];
|
const results = [];
|
||||||
const [docs, nPages] = await paginate(cursor, page, 100);
|
const [docs, nPages] = page ? await paginate(cursor, page, 100) : [await cursor.toArray(), 1];
|
||||||
for (const doc of docs) {
|
for (const doc of docs) {
|
||||||
count++;
|
count++;
|
||||||
if (!last || !equ(last, doc)) r = count;
|
if (!last || !equ(last, doc)) r = count;
|
||||||
@@ -23,34 +17,6 @@ const ranked: ranked = async function rankedPage(cursor, equ = (a, b) => a === b
|
|||||||
}
|
}
|
||||||
return [results, nPages];
|
return [results, nPages];
|
||||||
};
|
};
|
||||||
ranked.iterate = async function rankedIterate<T>(cursor: Cursor<T>, equ = (a, b) => a === b) {
|
|
||||||
let last = null;
|
|
||||||
let r = 0;
|
|
||||||
let count = 0;
|
|
||||||
async function getNext(): G<T> {
|
|
||||||
if (!await cursor.hasNext()) return [];
|
|
||||||
const doc = await cursor.next();
|
|
||||||
count++;
|
|
||||||
if (!last || !equ(last, doc)) r = count;
|
|
||||||
last = doc;
|
|
||||||
return [[r, doc], getNext];
|
|
||||||
}
|
|
||||||
return await getNext();
|
|
||||||
};
|
|
||||||
ranked.all = async function rankedAll(cursor, equ = (a, b) => a === b) {
|
|
||||||
let last = null;
|
|
||||||
let r = 0;
|
|
||||||
let count = 0;
|
|
||||||
const results = [];
|
|
||||||
const docs = await cursor.toArray();
|
|
||||||
for (const doc of docs) {
|
|
||||||
count++;
|
|
||||||
if (!last || !equ(last, doc)) r = count;
|
|
||||||
last = doc;
|
|
||||||
results.push([r, doc]);
|
|
||||||
}
|
|
||||||
return [results, 1];
|
|
||||||
};
|
|
||||||
|
|
||||||
global.Hydro.lib.rank = ranked;
|
global.Hydro.lib.rank = ranked;
|
||||||
export = ranked;
|
export = ranked;
|
||||||
@@ -189,7 +189,7 @@ const acm = buildContestRule({
|
|||||||
return [rows, udict, nPages];
|
return [rows, udict, nPages];
|
||||||
},
|
},
|
||||||
async ranked(tdoc, cursor) {
|
async ranked(tdoc, cursor) {
|
||||||
return await ranked.all(cursor, (a, b) => a.accept === b.accept && a.time === b.time);
|
return await ranked(cursor, (a, b) => a.accept === b.accept && a.time === b.time);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -310,7 +310,7 @@ const oi = buildContestRule({
|
|||||||
return [rows, udict, nPages];
|
return [rows, udict, nPages];
|
||||||
},
|
},
|
||||||
async ranked(tdoc, cursor) {
|
async ranked(tdoc, cursor) {
|
||||||
return await ranked.all(cursor, (a, b) => a.score === b.score);
|
return await ranked(cursor, (a, b) => a.score === b.score);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -467,7 +467,7 @@ const homework = buildContestRule({
|
|||||||
return [rows, udict, nPages];
|
return [rows, udict, nPages];
|
||||||
},
|
},
|
||||||
async ranked(tdoc, cursor) {
|
async ranked(tdoc, cursor) {
|
||||||
return await ranked.all(cursor, (a, b) => a.score === b.score);
|
return await ranked(cursor, (a, b) => a.score === b.score);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user