AK
Size: a a a
AK
L
L
L
AK
L
L
L
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
S
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
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
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
L
L
F
L
AK
F