2021-06-10 13:40:10 +03:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-11 12:33:00 +03:00
|
|
|
import { reorder } from "./arrays";
|
|
|
|
|
2021-06-10 13:40:10 +03:00
|
|
|
export const ALPHABET_START = 0x20;
|
|
|
|
export const ALPHABET_END = 0x7E;
|
|
|
|
export const ALPHABET = new Array(1 + ALPHABET_END - ALPHABET_START)
|
|
|
|
.fill(undefined)
|
|
|
|
.map((_, i) => String.fromCharCode(ALPHABET_START + i))
|
|
|
|
.join("");
|
|
|
|
|
|
|
|
export const baseToString = (base: number, alphabet = ALPHABET): string => {
|
|
|
|
base = Math.floor(base);
|
|
|
|
if (base < alphabet.length) return alphabet[base];
|
|
|
|
return baseToString(Math.floor(base / alphabet.length), alphabet) + alphabet[base % alphabet.length];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const stringToBase = (str: string, alphabet = ALPHABET): number => {
|
|
|
|
let result = 0;
|
|
|
|
for (let i = str.length - 1, j = 0; i >= 0; i--, j++) {
|
|
|
|
result += (str.charCodeAt(i) - alphabet.charCodeAt(0)) * (alphabet.length ** j);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
const pad = (str: string, length: number, alphabet = ALPHABET): string => str.padEnd(length, alphabet[0]);
|
|
|
|
|
2021-06-11 18:28:07 +03:00
|
|
|
export const midPointsBetweenStrings = (
|
|
|
|
a: string,
|
|
|
|
b: string,
|
|
|
|
count: number,
|
|
|
|
maxLen: number,
|
|
|
|
alphabet = ALPHABET,
|
|
|
|
): string[] => {
|
|
|
|
const n = Math.min(maxLen, Math.max(a.length, b.length));
|
|
|
|
const aPadded = pad(a, n, alphabet);
|
|
|
|
const bPadded = pad(b, n, alphabet);
|
|
|
|
const aBase = stringToBase(aPadded, alphabet);
|
|
|
|
const bBase = stringToBase(bPadded, alphabet);
|
|
|
|
if (bBase - aBase - 1 < count) {
|
|
|
|
if (n < maxLen) {
|
|
|
|
// this recurses once at most due to the new limit of n+1
|
|
|
|
return midPointsBetweenStrings(
|
|
|
|
pad(aPadded, n + 1, alphabet),
|
|
|
|
pad(bPadded, n + 1, alphabet),
|
|
|
|
count,
|
|
|
|
n + 1,
|
|
|
|
alphabet,
|
|
|
|
);
|
|
|
|
}
|
2021-06-10 13:40:10 +03:00
|
|
|
return [];
|
|
|
|
}
|
2021-06-11 18:28:07 +03:00
|
|
|
const step = (bBase - aBase) / (count + 1);
|
2021-06-10 13:40:10 +03:00
|
|
|
return Array(count).fill(undefined).map((_, i) => baseToString(aBase + step + (i * step), alphabet));
|
|
|
|
};
|
2021-06-11 12:33:00 +03:00
|
|
|
|
|
|
|
interface IEntry {
|
|
|
|
index: number;
|
|
|
|
order: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const reorderLexicographically = (
|
|
|
|
orders: Array<string | undefined>,
|
|
|
|
fromIndex: number,
|
|
|
|
toIndex: number,
|
2021-06-11 18:28:07 +03:00
|
|
|
maxLen = 50,
|
2021-06-11 12:33:00 +03:00
|
|
|
): IEntry[] => {
|
|
|
|
if (
|
|
|
|
fromIndex < 0 || toIndex < 0 ||
|
|
|
|
fromIndex > orders.length || toIndex > orders.length ||
|
|
|
|
fromIndex === toIndex
|
|
|
|
) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const ordersWithIndices: IEntry[] = orders.map((order, index) => ({ index, order }));
|
|
|
|
const newOrder = reorder(ordersWithIndices, fromIndex, toIndex);
|
|
|
|
|
|
|
|
const isMoveTowardsRight = toIndex > fromIndex;
|
|
|
|
const orderToLeftUndefined = newOrder[toIndex - 1]?.order === undefined;
|
|
|
|
|
|
|
|
let leftBoundIdx = toIndex;
|
|
|
|
let rightBoundIdx = toIndex;
|
|
|
|
|
|
|
|
const canDisplaceLeft = isMoveTowardsRight || orderToLeftUndefined || true; // TODO
|
|
|
|
if (canDisplaceLeft) {
|
|
|
|
const nextBase = newOrder[toIndex + 1]?.order !== undefined
|
|
|
|
? stringToBase(newOrder[toIndex + 1].order)
|
|
|
|
: Number.MAX_VALUE;
|
2021-06-11 18:28:07 +03:00
|
|
|
for (let i = toIndex - 1, j = 1; i >= 0; i--, j++) {
|
2021-06-11 12:33:00 +03:00
|
|
|
if (newOrder[i]?.order !== undefined && nextBase - stringToBase(newOrder[i].order) > j) break;
|
|
|
|
leftBoundIdx = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const canDisplaceRight = !orderToLeftUndefined;
|
|
|
|
// TODO check if there is enough space on the right hand side at all,
|
|
|
|
// I guess find the last set order and then compare it to prevBase + $requiredGap
|
|
|
|
if (canDisplaceRight) {
|
|
|
|
const prevBase = newOrder[toIndex - 1]?.order !== undefined
|
|
|
|
? stringToBase(newOrder[toIndex - 1]?.order)
|
|
|
|
: Number.MIN_VALUE;
|
2021-06-11 18:28:07 +03:00
|
|
|
for (let i = toIndex + 1, j = 1; i < newOrder.length; i++, j++) {
|
2021-06-11 12:33:00 +03:00
|
|
|
if (newOrder[i]?.order === undefined || stringToBase(newOrder[i].order) - prevBase > j) break; // TODO verify
|
|
|
|
rightBoundIdx = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const leftDiff = toIndex - leftBoundIdx;
|
|
|
|
const rightDiff = rightBoundIdx - toIndex;
|
|
|
|
|
|
|
|
if (orderToLeftUndefined || leftDiff < rightDiff) {
|
|
|
|
rightBoundIdx = toIndex;
|
|
|
|
} else {
|
|
|
|
leftBoundIdx = toIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
const prevOrder = newOrder[leftBoundIdx - 1]?.order
|
|
|
|
?? String.fromCharCode(ALPHABET_START).repeat(5); // TODO
|
|
|
|
const nextOrder = newOrder[rightBoundIdx + 1]?.order
|
|
|
|
?? String.fromCharCode(ALPHABET_END).repeat(prevOrder.length + 1); // TODO
|
|
|
|
|
2021-06-11 18:28:07 +03:00
|
|
|
const changes = midPointsBetweenStrings(prevOrder, nextOrder, 1 + rightBoundIdx - leftBoundIdx, maxLen);
|
2021-06-11 12:33:00 +03:00
|
|
|
// TODO If we exceed maxLen then reorder EVERYTHING
|
|
|
|
|
2021-06-11 18:28:07 +03:00
|
|
|
console.log("@@ test", { prevOrder, nextOrder, changes, leftBoundIdx, rightBoundIdx, orders, fromIndex, toIndex, newOrder, orderToLeftUndefined, leftDiff, rightDiff });
|
2021-06-11 12:33:00 +03:00
|
|
|
|
|
|
|
return changes.map((order, i) => {
|
|
|
|
const index = newOrder[leftBoundIdx + i].index;
|
|
|
|
|
|
|
|
return { index, order };
|
|
|
|
});
|
|
|
|
};
|