Skip to content

Commit

Permalink
Merge branch 'master' of github.com:hyoo-ru/mam_mol into offline2
Browse files Browse the repository at this point in the history
  • Loading branch information
zerkalica committed Jan 3, 2025
2 parents 667e459 + 5a2c887 commit e995539
Show file tree
Hide file tree
Showing 14 changed files with 508 additions and 7 deletions.
30 changes: 30 additions & 0 deletions bloom/bloom.test.ts
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 )

},

})
}
74 changes: 74 additions & 0 deletions bloom/bloom.ts
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 )
}

}

}
2 changes: 1 addition & 1 deletion book2/book2.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace $.$$ {
left: p.offsetLeft + p.offsetWidth - b.offsetWidth,
behavior: 'smooth',
})
new this.$.$mol_after_timeout( 1000, ()=> n.bring() )
// new this.$.$mol_after_timeout( 1000, ()=> n.bring() )
} )

break
Expand Down
2 changes: 1 addition & 1 deletion crypto/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace $ {

let sponge = new Uint32Array(80)

/** Fast small sync SHA-1 */
/** Fast small sync SHA-1 (20 bytes, 160 bits) */
export function $mol_crypto_hash( data: Uint8Array ) {

const bits = data.byteLength << 3
Expand Down
1 change: 1 addition & 0 deletions dom/capture/demo/demo.meta.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deploy \/mol/logo/logo.svg
5 changes: 5 additions & 0 deletions dom/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace $ {

export var $mol_dom = $mol_dom_context

}
106 changes: 106 additions & 0 deletions dom/point/point.ts
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 )

}

}

}
61 changes: 61 additions & 0 deletions dom/range/range.ts
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
}

}
}
2 changes: 1 addition & 1 deletion html/decode/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace $ {
export function $mol_html_decode( text : string ) {

return text
.replace( /&(?:#(\d+)|(lt|gt|quot));/gi , ( str , numb , name )=> {
.replace( /&(?:#(\d+)|(lt|gt|quot|amp));/gi , ( str , numb , name )=> {
if( numb ) return String.fromCharCode( numb )

const mapping = {
Expand Down
Loading

0 comments on commit e995539

Please sign in to comment.