Size: a a a

JS Liberty [ОЫ]

2020 June 20

AK

Aleksei Kowalski in JS Liberty [ОЫ]
я просто не понимаю всю сложность наверное)
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
Foma
массив фейкером и бинарный поиск лодеш
так ты в деле? с лодашем решиш?
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
Aleksei Kowalski
я просто не понимаю всю сложность наверное)
попробуй как можешь
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
я  тебе помогу
источник

AK

Aleksei Kowalski in JS Liberty [ОЫ]
покажи что уже сделал
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
Aleksei Kowalski
покажи что уже сделал
ну, изучил апи идб
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
сделал прототип дерева
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
class IDB {
 constructor(dbName, objectStoreName, keyPath, indexList = []) {
   this.dbName = dbName
   this.objectStoreName = objectStoreName
   this.keyPath = keyPath
   this.indexList = indexList

   this.db = null
 }
 
 async open() {
   return new Promise((d,f) => {

     const rqIndexDB = window.indexedDB.open(this.dbName, 3);
     rqIndexDB.onerror = event => {
       f(event)
     }
     rqIndexDB.onupgradeneeded = event => {
       const db = event.target.result;
       const objectStore = db.createObjectStore(this.objectStoreName, { keyPath: this.keyPath });
       this.indexList.map(i => {
         if ( typeof i === "string" )
           i = {indexName: i, keyPath: i, unique: false}
         objectStore.createIndex(i.indexName, i.keyPath, i)
       })
     }
     rqIndexDB.onsuccess = event => {
       this.db = event.target.result;
       d(this.db)
     }

   })
 }
 transaction(mode = "readwrite") {
   if ( !this.db )
     return null
   return this.db.transaction([this.objectStoreName], mode);
 }
 objectStore(mode = "readwrite") {
   const t = this.transaction(mode)
   if ( !t )
     return null
   
   return t.objectStore(this.objectStoreName)
 }

}
const idb = new IDB("morewords", "words", "id", ["value"])
await idb.open()
источник

F

Foma in JS Liberty [ОЫ]
идб 1 трабл тут и все
источник

S

Syntax Highlight Bot in JS Liberty [ОЫ]
Lupusregina[beta]
class IDB {
 constructor(dbName, objectStoreName, keyPath, indexList = []) {
   this.dbName = dbName
   this.objectStoreName = objectStoreName
   this.keyPath = keyPath
   this.indexList = indexList

   this.db = null
 }
 
 async open() {
   return new Promise((d,f) => {

     const rqIndexDB = window.indexedDB.open(this.dbName, 3);
     rqIndexDB.onerror = event => {
       f(event)
     }
     rqIndexDB.onupgradeneeded = event => {
       const db = event.target.result;
       const objectStore = db.createObjectStore(this.objectStoreName, { keyPath: this.keyPath });
       this.indexList.map(i => {
         if ( typeof i === "string" )
           i = {indexName: i, keyPath: i, unique: false}
         objectStore.createIndex(i.indexName, i.keyPath, i)
       })
     }
     rqIndexDB.onsuccess = event => {
       this.db = event.target.result;
       d(this.db)
     }

   })
 }
 transaction(mode = "readwrite") {
   if ( !this.db )
     return null
   return this.db.transaction([this.objectStoreName], mode);
 }
 objectStore(mode = "readwrite") {
   const t = this.transaction(mode)
   if ( !t )
     return null
   
   return t.objectStore(this.objectStoreName)
 }

}
const idb = new IDB("morewords", "words", "id", ["value"])
await idb.open()
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
class Node {
 constructor(char, parent, inc = true) {
   this.parent = parent
   this.char = char
   this.children = []
   this.leaf = false
   this.numLeafs = 1

   if ( inc )
     while(parent) {
       parent.numLeafs++
       parent = parent.parent
     }
 }
}
class Tree {
 constructor() {
   this.root = new Node("", null, false)
   this.root.numLeafs = 0
 }
 
 add(word, node = this.root) {
   for(let i = 0; i < word.length; i++) {
     const char = word[i]
     const childNode = node.children.find(n => n.char[0] === char)
     if ( childNode ) {
       if ( childNode.char.length === 1 ) {
         node = childNode
         continue
       } else {
         const word1 = childNode.char.slice(1)
         childNode.char = childNode.char[0]
         childNode.children.push( new Node(word1, childNode, false) )
         this.add(word.slice(i + 1), childNode)
         break
       }
     } else {
       node.children.push( new Node(word.slice(i), node, true) )
       break
     }
   }
 }
 getAllNumNodes(children = this.root.children) {
   return children.reduce((s, node) => s + 1 + this.getAllNumNodes(node.children), 0)
 }
 likeStringCount(pattern) {
   let node = this.root

   next:
   while(1) {
     if ( !pattern.length )
       return node.numLeafs

     let children = node.children
     for(const childNode of children) {
       if ( childNode.children.length ) {
         if ( childNode.char === pattern[0] ) {
           pattern = pattern.slice(1)
           node = childNode
           continue next;
         }
       } else {
         if ( childNode.char.indexOf(pattern) === 0 ) {
           pattern = ""
           node = childNode
           continue next;
         }
       }
     }
     
     return 0
   }
 }
}
const t = new Tree

function getStringRand(len = 100) {
 let s = ""
 while(len--)
   s += String.fromCharCode(97 + (Math.random()*25|0))
 return s
}

console.time('init')
for(let i = 0; i < 1e6; i++) {
 const s = getStringRand()
 t.add( s )
}
console.timeEnd('init')

function like(pattern) {
 let tn = Date.now()
   const count = t.likeStringCount(pattern)
 tn = Date.now() - tn
 console.log(`(time: ${ tn }msec) Like '${ pattern }.*' count: ` + count)
}
like('a')
like('ab')
like('abc')
like('abce')
источник

S

Syntax Highlight Bot in JS Liberty [ОЫ]
Lupusregina[beta]
class Node {
 constructor(char, parent, inc = true) {
   this.parent = parent
   this.char = char
   this.children = []
   this.leaf = false
   this.numLeafs = 1

   if ( inc )
     while(parent) {
       parent.numLeafs++
       parent = parent.parent
     }
 }
}
class Tree {
 constructor() {
   this.root = new Node("", null, false)
   this.root.numLeafs = 0
 }
 
 add(word, node = this.root) {
   for(let i = 0; i < word.length; i++) {
     const char = word[i]
     const childNode = node.children.find(n => n.char[0] === char)
     if ( childNode ) {
       if ( childNode.char.length === 1 ) {
         node = childNode
         continue
       } else {
         const word1 = childNode.char.slice(1)
         childNode.char = childNode.char[0]
         childNode.children.push( new Node(word1, childNode, false) )
         this.add(word.slice(i + 1), childNode)
         break
       }
     } else {
       node.children.push( new Node(word.slice(i), node, true) )
       break
     }
   }
 }
 getAllNumNodes(children = this.root.children) {
   return children.reduce((s, node) => s + 1 + this.getAllNumNodes(node.children), 0)
 }
 likeStringCount(pattern) {
   let node = this.root

   next:
   while(1) {
     if ( !pattern.length )
       return node.numLeafs

     let children = node.children
     for(const childNode of children) {
       if ( childNode.children.length ) {
         if ( childNode.char === pattern[0] ) {
           pattern = pattern.slice(1)
           node = childNode
           continue next;
         }
       } else {
         if ( childNode.char.indexOf(pattern) === 0 ) {
           pattern = ""
           node = childNode
           continue next;
         }
       }
     }
     
     return 0
   }
 }
}
const t = new Tree

function getStringRand(len = 100) {
 let s = ""
 while(len--)
   s += String.fromCharCode(97 + (Math.random()*25|0))
 return s
}

console.time('init')
for(let i = 0; i < 1e6; i++) {
 const s = getStringRand()
 t.add( s )
}
console.timeEnd('init')

function like(pattern) {
 let tn = Date.now()
   const count = t.likeStringCount(pattern)
 tn = Date.now() - tn
 console.log(`(time: ${ tn }msec) Like '${ pattern }.*' count: ` + count)
}
like('a')
like('ab')
like('abc')
like('abce')
источник

F

Foma in JS Liberty [ОЫ]
ваще нет желания разбираться с идб)
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
Foma
ваще нет желания разбираться с идб)
да возми любую обертку
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
там апи вида set(key, value) / get(key)
источник

F

Foma in JS Liberty [ОЫ]
та уже смотрел
источник

L

Lupusregina[beta] in JS Liberty [ОЫ]
я разбирался, что бы макс перфоманс получить. но не получил
источник

AK

Aleksei Kowalski in JS Liberty [ОЫ]
ну ты машина
источник

F

Foma in JS Liberty [ОЫ]
len = 100 подчерк питониста
источник