mirror of
https://github.com/multipleof4/lynchmark.git
synced 2026-01-14 08:37:56 +00:00
Docs: Update benchmark results
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
async function findConvexHull(points) {
|
||||
const { sortBy, uniqWith } = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.js');
|
||||
const _ = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js').then(m => m.default);
|
||||
|
||||
if (points.length < 3) return points;
|
||||
|
||||
const sorted = sortBy(uniqWith(points, (a, b) => a.x === b.x && a.y === b.y), ['x', 'y']);
|
||||
const sorted = _.sortBy(_.uniqWith(points, _.isEqual), ['x', 'y']);
|
||||
|
||||
if (sorted.length < 3) return sorted;
|
||||
|
||||
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
||||
|
||||
const buildHull = arr => {
|
||||
const buildHull = pts => {
|
||||
const hull = [];
|
||||
for (const p of arr) {
|
||||
while (hull.length > 1 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) {
|
||||
for (const p of pts) {
|
||||
while (hull.length >= 2 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) {
|
||||
hull.pop();
|
||||
}
|
||||
hull.push(p);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
async function findConvexHull(points) {
|
||||
const { sortBy, uniqWith } = await import('https://cdn.skypack.dev/lodash');
|
||||
const _ = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js').then(m => m.default);
|
||||
|
||||
if (points.length < 3) return points;
|
||||
|
||||
const sorted = sortBy(uniqWith(points, (a, b) => a.x === b.x && a.y === b.y), ['x', 'y']);
|
||||
const sorted = _.sortBy(_.uniqWith(points, _.isEqual), ['x', 'y']);
|
||||
|
||||
if (sorted.length < 3) return sorted;
|
||||
|
||||
@@ -12,7 +12,7 @@ async function findConvexHull(points) {
|
||||
const buildHull = arr => {
|
||||
const hull = [];
|
||||
for (const p of arr) {
|
||||
while (hull.length > 1 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) {
|
||||
while (hull.length >= 2 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) {
|
||||
hull.pop();
|
||||
}
|
||||
hull.push(p);
|
||||
@@ -21,6 +21,9 @@ async function findConvexHull(points) {
|
||||
return hull;
|
||||
};
|
||||
|
||||
return [...buildHull(sorted), ...buildHull(sorted.slice().reverse())];
|
||||
const lower = buildHull(sorted);
|
||||
const upper = buildHull([...sorted].reverse());
|
||||
|
||||
return [...lower, ...upper].reverse();
|
||||
}
|
||||
export default findConvexHull;
|
||||
@@ -1,15 +1,15 @@
|
||||
async function findConvexHull(points) {
|
||||
const { sortBy, uniqWith, isEqual } = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js');
|
||||
const findConvexHull = async (points) => {
|
||||
const { sortBy, uniqWith } = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.min.js');
|
||||
|
||||
if (points.length < 3) return points;
|
||||
|
||||
const sorted = sortBy(uniqWith(points, isEqual), ['x', 'y']);
|
||||
const sorted = sortBy(uniqWith(points, (a, b) => a.x === b.x && a.y === b.y), ['x', 'y']);
|
||||
|
||||
if (sorted.length < 3) return sorted;
|
||||
|
||||
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
||||
|
||||
const buildHull = (pts) => {
|
||||
const buildHalf = (pts) => {
|
||||
const hull = [];
|
||||
for (const p of pts) {
|
||||
while (hull.length >= 2 && cross(hull[hull.length - 2], hull[hull.length - 1], p) <= 0) {
|
||||
@@ -21,9 +21,9 @@ async function findConvexHull(points) {
|
||||
return hull;
|
||||
};
|
||||
|
||||
const lower = buildHull(sorted);
|
||||
const upper = buildHull([...sorted].reverse());
|
||||
const lower = buildHalf(sorted);
|
||||
const upper = buildHalf([...sorted].reverse());
|
||||
|
||||
return [...lower, ...upper].reverse();
|
||||
}
|
||||
};
|
||||
export default findConvexHull;
|
||||
@@ -1,33 +0,0 @@
|
||||
async function findConvexHull(points) {
|
||||
const _ = await import('https://cdn.jsdelivr.net/npm/lodash@4.17.21/+esm');
|
||||
|
||||
const uniquePoints = _.uniqWith(points, _.isEqual);
|
||||
|
||||
if (uniquePoints.length < 3) {
|
||||
return uniquePoints;
|
||||
}
|
||||
|
||||
const sorted = _.sortBy(uniquePoints, ['x', 'y']);
|
||||
|
||||
const crossProduct = (p1, p2, p3) =>
|
||||
(p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
|
||||
|
||||
const buildHalfHull = (pts) => {
|
||||
const hull = [];
|
||||
for (const p of pts) {
|
||||
while (hull.length >= 2 && crossProduct(hull.at(-2), hull.at(-1), p) <= 0) {
|
||||
hull.pop();
|
||||
}
|
||||
hull.push(p);
|
||||
}
|
||||
return hull;
|
||||
};
|
||||
|
||||
const lower = buildHalfHull(sorted);
|
||||
const upper = buildHalfHull([...sorted].reverse());
|
||||
|
||||
const hull = [...lower.slice(0, -1), ...upper.slice(0, -1)];
|
||||
|
||||
return [hull[0], ...hull.slice(1).reverse()];
|
||||
}
|
||||
export default findConvexHull;
|
||||
@@ -1,34 +1,29 @@
|
||||
async function findConvexHull(points) {
|
||||
const _ = await import('https://cdn.jsdelivr.net/npm/lodash@4.17.21/+esm');
|
||||
const _ = await import('https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js');
|
||||
|
||||
const uniqueSortedPoints = _.sortBy(
|
||||
_.uniqBy(points, p => `${p.x}:${p.y}`),
|
||||
['x', 'y']
|
||||
);
|
||||
const sortedUniquePoints = _.uniqWith(_.sortBy(points, ['x', 'y']), _.isEqual);
|
||||
|
||||
if (uniqueSortedPoints.length < 3) {
|
||||
return uniqueSortedPoints;
|
||||
if (sortedUniquePoints.length < 3) {
|
||||
return sortedUniquePoints;
|
||||
}
|
||||
|
||||
const crossProduct = (p1, p2, p3) =>
|
||||
(p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
|
||||
|
||||
const buildHalfHull = pts => {
|
||||
const half = [];
|
||||
const buildHalfHull = (pts) => {
|
||||
const hull = [];
|
||||
for (const p of pts) {
|
||||
while (half.length >= 2 && crossProduct(half.at(-2), half.at(-1), p) <= 0) {
|
||||
half.pop();
|
||||
while (hull.length >= 2 && crossProduct(hull.at(-2), hull.at(-1), p) <= 0) {
|
||||
hull.pop();
|
||||
}
|
||||
half.push(p);
|
||||
hull.push(p);
|
||||
}
|
||||
return half;
|
||||
return hull;
|
||||
};
|
||||
|
||||
const lower = buildHalfHull(uniqueSortedPoints);
|
||||
const upper = buildHalfHull([...uniqueSortedPoints].reverse());
|
||||
const lowerHull = buildHalfHull(sortedUniquePoints);
|
||||
const upperHull = buildHalfHull([...sortedUniquePoints].reverse());
|
||||
|
||||
const ccwHull = lower.slice(0, -1).concat(upper.slice(0, -1));
|
||||
|
||||
return [ccwHull[0], ...ccwHull.slice(1).reverse()];
|
||||
return [...upperHull.slice(0, -1), ...lowerHull.slice(0, -1)];
|
||||
}
|
||||
export default findConvexHull;
|
||||
@@ -1,35 +0,0 @@
|
||||
const findConvexHull = async (points) => {
|
||||
const _ = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/+esm');
|
||||
|
||||
const sortedPoints = _.sortBy(points, ['x', 'y']);
|
||||
const uniquePoints = _.uniqWith(sortedPoints, _.isEqual);
|
||||
|
||||
if (uniquePoints.length < 3) {
|
||||
return uniquePoints;
|
||||
}
|
||||
|
||||
const crossProduct = (p1, p2, p3) =>
|
||||
(p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x);
|
||||
|
||||
const lower = [];
|
||||
for (const p of uniquePoints) {
|
||||
while (lower.length >= 2 && crossProduct(lower.at(-2), lower.at(-1), p) <= 0) {
|
||||
lower.pop();
|
||||
}
|
||||
lower.push(p);
|
||||
}
|
||||
|
||||
const upper = [];
|
||||
for (let i = uniquePoints.length - 1; i >= 0; i--) {
|
||||
const p = uniquePoints[i];
|
||||
while (upper.length >= 2 && crossProduct(upper.at(-2), upper.at(-1), p) <= 0) {
|
||||
upper.pop();
|
||||
}
|
||||
upper.push(p);
|
||||
}
|
||||
|
||||
const hull = [...lower.slice(0, -1), ...upper.slice(0, -1)];
|
||||
|
||||
return hull.reverse();
|
||||
};
|
||||
export default findConvexHull;
|
||||
@@ -1,23 +1,19 @@
|
||||
const lod=import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.js');
|
||||
|
||||
export async function findConvexHull(input){
|
||||
if(!Array.isArray(input)) return [];
|
||||
const {uniqBy:uniq,sortBy:sort}=await lod;
|
||||
const pts=sort(uniq(input.filter(p=>p&&Number.isFinite(p.x)&&Number.isFinite(p.y)).map(p=>({x:+p.x,y:+p.y})),p=>`${p.x}:${p.y}`),['x','y']);
|
||||
if(pts.length<2) return pts;
|
||||
const area=(a,b,c)=>(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
|
||||
const walk=list=>{
|
||||
const res=[];
|
||||
for(const p of list){
|
||||
while(res.length>1&&area(res[res.length-2],res[res.length-1],p)<=0)res.pop();
|
||||
res.push(p);
|
||||
}
|
||||
return res;
|
||||
};
|
||||
const low=walk(pts);
|
||||
const up=walk(pts.slice().reverse());
|
||||
const hull=low.slice(0,-1).concat(up.slice(0,-1));
|
||||
const out=hull.length?hull:pts.slice(0,2);
|
||||
return out.length<3?out:out.reverse();
|
||||
async function findConvexHull(points){
|
||||
const {default:_}=await import('https://cdn.skypack.dev/lodash-es');
|
||||
const pts=_.uniqWith(_.sortBy(points,['x','y']),(a,b)=>a.x===b.x&&a.y===b.y);
|
||||
if(pts.length<3) return pts.slice();
|
||||
const c=(o,a,b)=>(a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
|
||||
const l=[],u=[];
|
||||
for(const p of pts){
|
||||
while(l.length>1&&c(l.at(-2),l.at(-1),p)<=0)l.pop();
|
||||
l.push(p);
|
||||
}
|
||||
for(let i=pts.length;i--;){
|
||||
const p=pts[i];
|
||||
while(u.length>1&&c(u.at(-2),u.at(-1),p)<=0)u.pop();
|
||||
u.push(p);
|
||||
}
|
||||
l.pop();u.pop();
|
||||
return [...l,...u].reverse();
|
||||
}
|
||||
export default findConvexHull;
|
||||
29
tests/2_convex_hull/outputs/openrouter_polaris-alpha.js
Normal file
29
tests/2_convex_hull/outputs/openrouter_polaris-alpha.js
Normal file
@@ -0,0 +1,29 @@
|
||||
async function findConvexHull(points) {
|
||||
if (!Array.isArray(points) || points.length < 3) return points || [];
|
||||
const { default: _ } = await import('https://cdn.jsdelivr.net/npm/lodash-es@4.17.21/lodash.default.min.js');
|
||||
const ps = _.uniqWith(points, (a, b) => a.x === b.x && a.y === b.y);
|
||||
if (ps.length < 3) return ps;
|
||||
const pts = _.sortBy(ps, ['x', 'y']);
|
||||
const cross = (o, a, b) => (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
|
||||
const lower = [];
|
||||
for (let p of pts) {
|
||||
for (; lower.length >= 2 && cross(lower[lower.length - 2], lower[lower.length - 1], p) <= 0;) lower.pop();
|
||||
lower.push(p);
|
||||
}
|
||||
const upper = [];
|
||||
for (let i = pts.length - 1; i >= 0; i--) {
|
||||
const p = pts[i];
|
||||
for (; upper.length >= 2 && cross(upper[upper.length - 2], upper[upper.length - 1], p) <= 0;) upper.pop();
|
||||
upper.push(p);
|
||||
}
|
||||
const hull = lower.slice(0, -1).concat(upper.slice(0, -1));
|
||||
if (hull.length <= 2) return hull;
|
||||
let area = 0;
|
||||
for (let i = 0; i < hull.length; i++) {
|
||||
const j = (i + 1) % hull.length;
|
||||
area += hull[i].x * hull[j].y - hull[j].x * hull[i].y;
|
||||
}
|
||||
if (area > 0) hull.reverse();
|
||||
return hull;
|
||||
}
|
||||
export default findConvexHull;
|
||||
Reference in New Issue
Block a user