hash value of brushes to prevent duplicates (WIP)

This commit is contained in:
Hugh Bord 2021-08-06 14:39:54 +10:00
parent 75b578bc77
commit 29f800c534
3 changed files with 42 additions and 11 deletions

View File

@ -582,4 +582,19 @@ export const checkForGetRequest = async () => {
}
}
// Hashing algo to detect duplicate brushes
export const cyrb53 = function (str, seed = 1337) {
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
};
export default createNewAscii;

View File

@ -24,17 +24,18 @@
</div>
<div v-if="tab === 0">
<div v-for="(value, key) in brushHistory" :key="key">
<BrushCanvas :blocks="decompressBlock(value)" />
<t-button type="button" @click="reuseBlocks(decompressBlock(value))">Reuse</t-button>
<t-button type="button" @click="saveToLibrary(decompressBlock(value))">Save to Library</t-button>
<div v-for="(brush, key) in brushHistory" :key="key">
<BrushCanvas :blocks="decompressBlock(brush.blocks)" />
<t-button type="button" @click="reuseBlocks(decompressBlock(brush.blocks))">Reuse</t-button>
<t-button type="button" @click="saveToLibrary(decompressBlock(brush.blocks))">Save to Library</t-button>
</div>
</div>
<div v-if="tab === 1">
<div v-for="(value, key) in brushLibrary" :key="key">
<BrushCanvas :blocks="decompressBlock(value)" />
<t-button type="button" @click="reuseBlocks(decompressBlock(value))">Reuse</t-button>
<div v-for="(brush, key) in brushLibrary" :key="key">
<BrushCanvas :blocks="decompressBlock(brush.blocks)" />
<t-button type="button" @click="reuseBlocks(decompressBlock(brush.blocks))">Reuse</t-button>
</div>
</div>
@ -100,7 +101,7 @@ export default {
return this.$store.getters.brushSizeType;
},
brushHistory() {
return this.$store.getters.brushHistory.slice(0,10);
return this.$store.getters.brushHistory;
},
brushLibrary() {
return this.$store.getters.brushLibrary;

View File

@ -4,7 +4,8 @@ import VuexPersistence from 'vuex-persist';
import LZString from 'lz-string';
import {
blockWidth,
blockHeight
blockHeight,
cyrb53
} from "../ascii";
Vue.use(Vuex);
@ -180,10 +181,24 @@ export default new Vuex.Store({
state.selectBlocks = LZString.compressToUTF16(JSON.stringify(payload));
},
pushBrushHistory(state, payload) {
state.brushHistory.unshift(LZString.compressToUTF16(JSON.stringify(payload)));
// Check and remove duplicate brushes based on hash value
let hashValue = cyrb53(JSON.stringify(payload))
state.brushHistory = state.brushHistory.filter(obj => obj.hash !== hashValue);
state.brushHistory.unshift({
blocks: LZString.compressToUTF16(JSON.stringify(payload)),
hash: hashValue
});
},
pushBrushLibrary(state, payload) {
state.brushLibrary.unshift(LZString.compressToUTF16(JSON.stringify(payload)));
// Check and remove duplicate brushes based on hash value
let hashValue = cyrb53(JSON.stringify(payload))
state.brushLibrary = state.brushLibrary.filter(obj => obj.hash !== hashValue);
state.brushLibrary.unshift({
blocks: LZString.compressToUTF16(JSON.stringify(payload)),
hash: hashValue
});
},
openModal(state, type) {
switch (type) {