-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:hyoo-ru/mam_mol into offline2
- Loading branch information
Showing
14 changed files
with
508 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace $ { | ||
$mol_test({ | ||
|
||
'Add and check strings'() { | ||
|
||
const bloom = new $mol_bloom( 5 ) | ||
$mol_assert_equal( bloom.has_str( 'Hello' ), 0 ) | ||
$mol_assert_equal( bloom.has_str( 'World' ), 0 ) | ||
$mol_assert_equal( bloom.has_str( 'Hello World' ), 0 ) | ||
$mol_assert_equal( bloom.has_str( 'hello' ), 0 ) | ||
$mol_assert_equal( bloom.has_str( 'world' ), 0 ) | ||
|
||
bloom.add_str( 'Hello World' ) | ||
$mol_assert_equal( bloom.has_str( 'Hello' ), 0 ) | ||
$mol_assert_equal( bloom.has_str( 'World' ), 0 ) | ||
$mol_assert_equal( bloom.has_str( 'Hello World' ), 1 ) | ||
$mol_assert_equal( bloom.has_str( 'hello' ), 0 ) | ||
$mol_assert_equal( bloom.has_str( 'world' ), 0 ) | ||
|
||
bloom.add_str( 'Hello' ) | ||
bloom.add_str( 'World' ) | ||
$mol_assert_equal( bloom.has_str( 'Hello' ), 1 ) | ||
$mol_assert_equal( bloom.has_str( 'World' ), 1 ) | ||
$mol_assert_equal( bloom.has_str( 'hello' ), 0 ) | ||
$mol_assert_equal( bloom.has_str( 'world' ), 0 ) | ||
|
||
}, | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
namespace $ { | ||
|
||
/** | ||
* Bloom filter with automatic optimal parameters. | ||
* False negative is impossible. | ||
* False positive is controlled. | ||
*/ | ||
export class $mol_bloom extends Object { | ||
|
||
bitmap: Uint32Array | ||
hashes: number | ||
|
||
constructor( | ||
/** Max count of stored values. */ | ||
count: number, | ||
/** Chance of false positive. 1e-6 by default */ | ||
public risk = 1e-6, | ||
) { | ||
super() | ||
this.hashes = Math.ceil( -1.44 * Math.log( risk ) ) | ||
|
||
const length = Math.ceil( -.065 * count * Math.log( risk ) ) | ||
this.bitmap = new Uint32Array( 2 ** Math.ceil( Math.log2( length ) ) ) | ||
} | ||
|
||
add_str( word: string ) { | ||
this.add_bin( $mol_charset_encode( word ) ) | ||
} | ||
|
||
has_str( word: string ) { | ||
return this.has_bin( $mol_charset_encode( word ) ) | ||
} | ||
|
||
add_bin( bin: Uint8Array ) { | ||
for( const index of this.hash( bin ) ) { | ||
this.add_bit( index ) | ||
} | ||
} | ||
|
||
has_bin( bin: Uint8Array ) { | ||
let res = 1 | ||
for( const index of this.hash( bin ) ) { | ||
res &= this.has_bit( index ) | ||
} | ||
return res | ||
} | ||
|
||
hash( data: Uint8Array ) { | ||
const res = [] as number[] | ||
fill: while( true ) { | ||
data = $mol_crypto_hash( data ) | ||
for( const index of new Uint32Array( data.buffer ) ) { | ||
res.push( index ) | ||
if( res.length >= this.hashes ) break fill | ||
} | ||
} | ||
return res | ||
} | ||
|
||
add_bit( index: number ) { | ||
const int = Math.ceil( index / 32 ) % this.bitmap.length | ||
const bit = index & 0b11111 | ||
this.bitmap[ int ] |= 1 << bit | ||
} | ||
|
||
has_bit( index: number ) { | ||
const int = Math.ceil( index / 32 ) % this.bitmap.length | ||
const bit = index & 0b11111 | ||
return 1&( this.bitmap[ int ] >> bit ) | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
deploy \/mol/logo/logo.svg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace $ { | ||
|
||
export var $mol_dom = $mol_dom_context | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
namespace $ { | ||
|
||
export class $mol_dom_point extends Object { | ||
|
||
constructor( | ||
public node: Node, | ||
public pos: number, | ||
) { super() } | ||
|
||
static start( node: Node ) { | ||
return new this( node, 0 ) | ||
} | ||
|
||
static end( node: Node ) { | ||
const length = node.nodeValue?.length ?? node.childNodes.length | ||
return new this( node, length ) | ||
} | ||
|
||
to( point: $mol_dom_point ) { | ||
this.node = point.node | ||
this.pos = point.pos | ||
} | ||
|
||
to_start() { | ||
this.pos = 0 | ||
return this | ||
} | ||
|
||
to_end() { | ||
this.pos = this.node.nodeValue?.length ?? this.node.childNodes.length | ||
return this | ||
} | ||
|
||
is_start() { | ||
return this.pos <= 0 | ||
} | ||
|
||
is_end() { | ||
return this.pos >= ( this.node.nodeValue?.length ?? this.node.childNodes.length ) | ||
} | ||
|
||
char_backward( root: Element ): $mol_dom_point { | ||
return this.backward( ()=> { | ||
if( this.node === root && this.is_start() ) return true | ||
if( this.node.nodeType !== this.node.TEXT_NODE ) return false | ||
if( this.is_start() ) return false | ||
this.pos -= 1 | ||
return true | ||
} ) | ||
} | ||
|
||
char_forward( root: Element ): $mol_dom_point { | ||
return this.forward( ()=> { | ||
if( this.node === root && this.is_end() ) return true | ||
if( this.node.nodeType !== this.node.TEXT_NODE ) return false | ||
if( this.is_end() ) return false | ||
this.pos += 1 | ||
return true | ||
} ) | ||
} | ||
|
||
backward( check: ()=> boolean ): $mol_dom_point { | ||
|
||
if( check() ) return this | ||
|
||
if( !this.is_start() ) { | ||
const kid = this.node.childNodes[ this.pos - 1 ] | ||
this.node = kid | ||
this.to_end() | ||
return this.backward( check ) | ||
} | ||
|
||
const parent = this.node.parentElement | ||
if( !parent ) return this | ||
|
||
const offset = [ ... parent.childNodes ].indexOf( this.node as ChildNode ) | ||
this.node = parent | ||
this.pos = offset | ||
return this.backward( check ) | ||
|
||
} | ||
|
||
forward( check: ()=> boolean ): $mol_dom_point { | ||
|
||
if( check() ) return this | ||
|
||
if( !this.is_end() ) { | ||
const kid = this.node.childNodes[ this.pos ] | ||
this.node = kid | ||
this.to_start() | ||
return this.forward( check ) | ||
} | ||
|
||
const parent = this.node.parentElement | ||
if( !parent ) return this | ||
|
||
const offset = [ ... parent.childNodes ].indexOf( this.node as ChildNode ) + 1 | ||
this.node = parent | ||
this.pos = offset | ||
return this.forward( check ) | ||
|
||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
namespace $ { | ||
export class $mol_dom_range extends Object { | ||
|
||
constructor( | ||
readonly head: $mol_dom_point, | ||
readonly foot: $mol_dom_point, | ||
) { super() } | ||
|
||
static from_selection( sel = $mol_dom_context.getSelection()! ) { | ||
return this.from_native( sel?.getRangeAt(0) ) | ||
} | ||
|
||
static from_native( range: Range ) { | ||
return new this( | ||
new $mol_dom_point( range.startContainer, range.startOffset ), | ||
new $mol_dom_point( range.endContainer, range.endOffset ), | ||
) | ||
} | ||
|
||
static inside( node: Node ) { | ||
return new this( | ||
$mol_dom_point.start( node ), | ||
$mol_dom_point.end( node ), | ||
) | ||
} | ||
|
||
static around( node: Node ) { | ||
|
||
const parent = node.parentNode! | ||
const pos = [ ... parent.childNodes ].indexOf( node as ChildNode ) | ||
|
||
return new this( | ||
new $mol_dom_point( parent, pos ), | ||
new $mol_dom_point( parent, pos + 1 ), | ||
) | ||
|
||
} | ||
|
||
is_empty() { | ||
return this.head.node === this.foot.node && this.head.pos === this.foot.pos | ||
} | ||
|
||
clear() { | ||
this.native().deleteContents() | ||
} | ||
|
||
select() { | ||
const sel = $mol_dom_context.document.getSelection()! | ||
sel.removeAllRanges() | ||
sel.addRange( this.native() ) | ||
} | ||
|
||
native() { | ||
const range = $mol_dom_context.document.createRange() | ||
range.setEnd( this.foot.node, this.foot.pos ) | ||
range.setStart( this.head.node, this.head.pos ) | ||
return range | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.