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-14 23:32:11 +03:00
|
|
|
import { alphabetPad, baseToString, stringToBase, DEFAULT_ALPHABET } from "matrix-js-sdk/src/utils";
|
2021-06-14 23:28:32 +03:00
|
|
|
|
2021-06-16 11:23:06 +03:00
|
|
|
import { moveElement } from "./arrays";
|
2021-06-11 12:33:00 +03:00
|
|
|
|
2021-06-11 18:28:07 +03:00
|
|
|
export const midPointsBetweenStrings = (
|
|
|
|
a: string,
|
|
|
|
b: string,
|
|
|
|
count: number,
|
|
|
|
maxLen: number,
|
2021-06-14 23:32:11 +03:00
|
|
|
alphabet = DEFAULT_ALPHABET,
|
2021-06-11 18:28:07 +03:00
|
|
|
): string[] => {
|
2021-06-14 23:28:32 +03:00
|
|
|
const padN = Math.min(Math.max(a.length, b.length), maxLen);
|
|
|
|
const padA = alphabetPad(a, padN, alphabet);
|
|
|
|
const padB = alphabetPad(b, padN, alphabet);
|
|
|
|
const baseA = stringToBase(padA, alphabet);
|
|
|
|
const baseB = stringToBase(padB, alphabet);
|
|
|
|
|
|
|
|
if (baseB - baseA - BigInt(1) < count) {
|
|
|
|
if (padN < maxLen) {
|
2021-06-11 18:28:07 +03:00
|
|
|
// this recurses once at most due to the new limit of n+1
|
|
|
|
return midPointsBetweenStrings(
|
2021-06-14 23:28:32 +03:00
|
|
|
alphabetPad(padA, padN + 1, alphabet),
|
|
|
|
alphabetPad(padB, padN + 1, alphabet),
|
2021-06-11 18:28:07 +03:00
|
|
|
count,
|
2021-06-14 23:28:32 +03:00
|
|
|
padN + 1,
|
2021-06-11 18:28:07 +03:00
|
|
|
alphabet,
|
|
|
|
);
|
|
|
|
}
|
2021-06-10 13:40:10 +03:00
|
|
|
return [];
|
|
|
|
}
|
2021-06-14 23:28:32 +03:00
|
|
|
|
|
|
|
const step = (baseB - baseA) / BigInt(count + 1);
|
|
|
|
const start = BigInt(baseA + step);
|
2021-06-14 16:37:05 +03:00
|
|
|
return Array(count).fill(undefined).map((_, i) => baseToString(start + (BigInt(i) * step), alphabet));
|
2021-06-10 13:40:10 +03:00
|
|
|
};
|
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[] => {
|
2021-06-14 16:37:05 +03:00
|
|
|
// sanity check inputs
|
2021-06-11 12:33:00 +03:00
|
|
|
if (
|
|
|
|
fromIndex < 0 || toIndex < 0 ||
|
|
|
|
fromIndex > orders.length || toIndex > orders.length ||
|
|
|
|
fromIndex === toIndex
|
|
|
|
) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:37:05 +03:00
|
|
|
// zip orders with their indices to simplify later index wrangling
|
2021-06-11 12:33:00 +03:00
|
|
|
const ordersWithIndices: IEntry[] = orders.map((order, index) => ({ index, order }));
|
2021-06-14 16:37:05 +03:00
|
|
|
// apply the fundamental order update to the zipped array
|
2021-06-16 11:23:06 +03:00
|
|
|
const newOrder = moveElement(ordersWithIndices, fromIndex, toIndex);
|
2021-06-11 12:33:00 +03:00
|
|
|
|
2021-06-14 23:28:32 +03:00
|
|
|
// check if we have to fill undefined orders to complete placement
|
2021-06-11 12:33:00 +03:00
|
|
|
const orderToLeftUndefined = newOrder[toIndex - 1]?.order === undefined;
|
|
|
|
|
|
|
|
let leftBoundIdx = toIndex;
|
|
|
|
let rightBoundIdx = toIndex;
|
|
|
|
|
2021-06-14 16:37:05 +03:00
|
|
|
let canMoveLeft = true;
|
|
|
|
const nextBase = newOrder[toIndex + 1]?.order !== undefined
|
|
|
|
? stringToBase(newOrder[toIndex + 1].order)
|
|
|
|
: BigInt(Number.MAX_VALUE);
|
|
|
|
|
2021-06-14 23:28:32 +03:00
|
|
|
// check how far left we would have to mutate to fit in that direction
|
2021-06-14 16:37:05 +03:00
|
|
|
for (let i = toIndex - 1, j = 1; i >= 0; i--, j++) {
|
|
|
|
if (newOrder[i]?.order !== undefined && nextBase - stringToBase(newOrder[i].order) > j) break;
|
|
|
|
leftBoundIdx = i;
|
|
|
|
}
|
|
|
|
|
2021-06-14 23:28:32 +03:00
|
|
|
// verify the left move would be sufficient
|
|
|
|
const firstOrderBase = newOrder[0].order === undefined ? undefined : stringToBase(newOrder[0].order);
|
|
|
|
const bigToIndex = BigInt(toIndex);
|
2021-06-14 16:37:05 +03:00
|
|
|
if (leftBoundIdx === 0 &&
|
2021-06-14 23:28:32 +03:00
|
|
|
firstOrderBase !== undefined &&
|
|
|
|
nextBase - firstOrderBase <= bigToIndex &&
|
|
|
|
firstOrderBase <= bigToIndex
|
2021-06-14 16:37:05 +03:00
|
|
|
) {
|
|
|
|
canMoveLeft = false;
|
2021-06-11 12:33:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const canDisplaceRight = !orderToLeftUndefined;
|
2021-06-14 16:37:05 +03:00
|
|
|
let canMoveRight = canDisplaceRight;
|
2021-06-11 12:33:00 +03:00
|
|
|
if (canDisplaceRight) {
|
|
|
|
const prevBase = newOrder[toIndex - 1]?.order !== undefined
|
|
|
|
? stringToBase(newOrder[toIndex - 1]?.order)
|
2021-06-14 16:37:05 +03:00
|
|
|
: BigInt(Number.MIN_VALUE);
|
|
|
|
|
2021-06-14 23:28:32 +03:00
|
|
|
// check how far right we would have to mutate to fit in that direction
|
2021-06-11 18:28:07 +03:00
|
|
|
for (let i = toIndex + 1, j = 1; i < newOrder.length; i++, j++) {
|
2021-06-14 23:28:32 +03:00
|
|
|
if (newOrder[i]?.order === undefined || stringToBase(newOrder[i].order) - prevBase > j) break;
|
2021-06-11 12:33:00 +03:00
|
|
|
rightBoundIdx = i;
|
|
|
|
}
|
2021-06-14 16:37:05 +03:00
|
|
|
|
2021-06-14 23:28:32 +03:00
|
|
|
// verify the right move would be sufficient
|
2021-06-14 16:37:05 +03:00
|
|
|
if (rightBoundIdx === newOrder.length - 1 &&
|
|
|
|
(newOrder[rightBoundIdx]
|
|
|
|
? stringToBase(newOrder[rightBoundIdx].order)
|
|
|
|
: BigInt(Number.MAX_VALUE)) - prevBase <= (rightBoundIdx - toIndex)
|
|
|
|
) {
|
|
|
|
canMoveRight = false;
|
|
|
|
}
|
2021-06-11 12:33:00 +03:00
|
|
|
}
|
|
|
|
|
2021-06-14 23:28:32 +03:00
|
|
|
// pick the cheaper direction
|
2021-06-14 16:37:05 +03:00
|
|
|
const leftDiff = canMoveLeft ? toIndex - leftBoundIdx : Number.MAX_SAFE_INTEGER;
|
|
|
|
const rightDiff = canMoveRight ? rightBoundIdx - toIndex : Number.MAX_SAFE_INTEGER;
|
2021-06-11 12:33:00 +03:00
|
|
|
if (orderToLeftUndefined || leftDiff < rightDiff) {
|
|
|
|
rightBoundIdx = toIndex;
|
|
|
|
} else {
|
|
|
|
leftBoundIdx = toIndex;
|
|
|
|
}
|
|
|
|
|
2021-06-14 23:28:32 +03:00
|
|
|
const prevOrder = newOrder[leftBoundIdx - 1]?.order ?? "";
|
2021-06-11 12:33:00 +03:00
|
|
|
const nextOrder = newOrder[rightBoundIdx + 1]?.order
|
2021-06-14 23:32:11 +03:00
|
|
|
?? DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 1).repeat(prevOrder.length || 1);
|
2021-06-11 12:33:00 +03:00
|
|
|
|
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
|
|
|
|
2021-06-14 23:28:32 +03:00
|
|
|
return changes.map((order, i) => ({
|
|
|
|
index: newOrder[leftBoundIdx + i].index,
|
|
|
|
order,
|
|
|
|
}));
|
2021-06-11 12:33:00 +03:00
|
|
|
};
|