mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-17 17:36:08 +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 paginate from './paginate';
|
||||
|
||||
type G<T> = Promise<[[number, T], () => G<T>] | []>;
|
||||
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]>;
|
||||
}
|
||||
type ranked = <T>(cursor: Cursor<T>, equ: ((a: T, b: T) => boolean), page?: number) => Promise<[[number, T][], number]>;
|
||||
|
||||
const ranked: ranked = async function rankedPage(cursor, equ = (a, b) => a === b, page) {
|
||||
if (!page) return await ranked.all(cursor, equ);
|
||||
const ranked: ranked = async (cursor, equ, page) => {
|
||||
let last = null;
|
||||
let r = (page - 1) * 100;
|
||||
let count = r;
|
||||
let r = page ? (page - 1) * 100 : 0;
|
||||
let count = page ? r : 0;
|
||||
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) {
|
||||
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];
|
||||
};
|
||||
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;
|
||||
export = ranked;
|
||||
@@ -189,7 +189,7 @@ const acm = buildContestRule({
|
||||
return [rows, udict, nPages];
|
||||
},
|
||||
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];
|
||||
},
|
||||
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];
|
||||
},
|
||||
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