-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Indexable ByteValue Indications
- Added indexable indication array for ByteValues within the viewport displays. - Created categorical byte indiciations values & CSS selectors. Closes #784
1 parent
f31364c
commit d7f7088
Showing
17 changed files
with
638 additions
and
252 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
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
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
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
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
114 changes: 114 additions & 0 deletions
114
src/svelte/src/utilities/ByteCategories/CategoryIndications.ts
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,114 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { CategoryOne, type ByteCategory, CategoryTwo } from './IByteCategory' | ||
|
||
class ByteIndicationCategories { | ||
private _categories: { | ||
[key: string]: ByteCategory | ||
} = {} | ||
private _bitsUtilized: number = 0 | ||
|
||
public addIndicationCategory(category: ByteCategory) { | ||
if (this._bitsUtilized + category.bitLength() > 8) | ||
throw new Error('Category addition would exceed bit limit') | ||
this._categories[category.name()] = category | ||
this._bitsUtilized += category.bitLength() | ||
|
||
return this | ||
} | ||
public category(name: string): ByteCategory { | ||
return this._categories[name] | ||
} | ||
public categoryOfIndication(indicationName: string): ByteCategory { | ||
for (const category in this._categories) | ||
if (this._categories[category].contains(indicationName)) | ||
return this._categories[category] | ||
|
||
throw new Error( | ||
`No ByteCategory found with ByteIndication named ${indicationName}` | ||
) | ||
} | ||
public categoryValueByIndication( | ||
category: ByteCategory, | ||
indicationName: string | ||
): number { | ||
return category.indexOf(indicationName) << this.categoryBitPos(category) | ||
} | ||
public clearIndication(data: Uint8Array, indicationName: string) { | ||
const category = this.categoryOfIndication(indicationName) | ||
data.forEach((byte, i, data) => { | ||
const categoryValueOfByte = byte & this.categoryMask(category) | ||
if ( | ||
categoryValueOfByte === | ||
this.categoryValueByIndication(category, indicationName) | ||
) | ||
data[i] &= this.categoryMask(category) ^ 0xff | ||
}) | ||
} | ||
public clearAndSetIf( | ||
data: Uint8Array, | ||
indication: string, | ||
predicate: (byte: number, index: number) => boolean | ||
) { | ||
const category = this.categoryOfIndication(indication) | ||
if (!category) { | ||
console.error(`No ByteCategory contains the indication: ${indication}`) | ||
return | ||
} | ||
|
||
data.forEach((byte, i, data) => { | ||
data[i] &= this.categoryMask(category) ^ 0xff | ||
if (predicate(byte, i)) | ||
data[i] |= this.categoryValueByIndication(category, indication) | ||
}) | ||
} | ||
public categoryCSSSelector( | ||
category: ByteCategory, | ||
byteIndicationValue: number | ||
) { | ||
const maskedByteValue = byteIndicationValue & this.categoryMask(category) | ||
const indicationIndex = maskedByteValue >> this.categoryBitPos(category) | ||
const indication = category.indicators()[indicationIndex] | ||
return indication != undefined | ||
? indication.selector() | ||
: category.indexOf('none') | ||
} | ||
private categoryMask(category: ByteCategory): number { | ||
const categoryBitPos = this.categoryBitPos(category) | ||
const categoryBitLength = category.bitLength() | ||
return (Math.pow(2, categoryBitLength) - 1) << categoryBitPos | ||
} | ||
private indexOf(category: ByteCategory) { | ||
let i = 0 | ||
for (let _category in this._categories) { | ||
if (category.name() === this._categories[_category].name()) { | ||
return i | ||
} | ||
i++ | ||
} | ||
return -1 | ||
} | ||
private categoryBitPos(category: ByteCategory): number { | ||
return this.indexOf(category) * category.bitLength() | ||
} | ||
} | ||
|
||
export const ViewportByteCategories = new ByteIndicationCategories() | ||
ViewportByteCategories.addIndicationCategory(CategoryOne).addIndicationCategory( | ||
CategoryTwo | ||
) |
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,84 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
NoIndication, | ||
type IByteIndication, | ||
ByteIndication, | ||
} from './IIndication' | ||
|
||
export interface IByteCategory { | ||
addIndication(selectorName: string): void | ||
bitLength(): number | ||
name(): string | ||
indicators(): readonly IByteIndication[] | ||
at(index: number): IByteIndication | ||
indexOf(selectorName: string): number | ||
contains(selectorName: string): boolean | ||
} | ||
|
||
export class ByteCategory implements IByteCategory { | ||
private _indicators: IByteIndication[] = [NoIndication] | ||
|
||
constructor( | ||
private _name: string, | ||
private _bitLength: number | ||
) { | ||
if (_bitLength > 8) | ||
throw new Error('Byte category indications cannot exceed 8 bits.') | ||
} | ||
addIndication(selectorName: string) { | ||
this._indicators.push(new ByteIndication(selectorName)) | ||
return this | ||
} | ||
bitLength() { | ||
return this._bitLength | ||
} | ||
name() { | ||
return this._name | ||
} | ||
indicators() { | ||
return this._indicators | ||
} | ||
at(index: number) { | ||
return index >= this._indicators.length || index < 0 | ||
? this._indicators[0] | ||
: this._indicators[index] | ||
} | ||
indexOf(selectorName: string) { | ||
const target = selectorName | ||
let ret = -1 | ||
|
||
this._indicators.forEach((categoryObj, i) => { | ||
if (categoryObj.selector() === target) ret = i | ||
}) | ||
if (ret < 0) | ||
throw new Error(`Indication category "${selectorName}" not found.`) | ||
else return ret | ||
} | ||
contains(selectorName: string): boolean { | ||
for (let i = 0; i < this._indicators.length; i++) | ||
if (this._indicators[i].selector() == selectorName) return true | ||
return false | ||
} | ||
} | ||
|
||
export const CategoryOne = new ByteCategory('one', 4) | ||
CategoryOne.addIndication('selected') | ||
|
||
export const CategoryTwo = new ByteCategory('two', 4) | ||
CategoryTwo.addIndication('searchresult').addIndication('replacement') |
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,42 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export interface IByteIndication { | ||
selector(): string | ||
equals(categoryArg: IByteIndication): boolean | ||
} | ||
|
||
class NullIndication implements IByteIndication { | ||
equals(categoryArg: IByteIndication): boolean { | ||
return this.selector() === categoryArg.selector() | ||
} | ||
selector(): string { | ||
return 'none' | ||
} | ||
} | ||
|
||
export class ByteIndication implements IByteIndication { | ||
constructor(private _selector: string) {} | ||
equals(categoryArg: IByteIndication): boolean { | ||
return this.selector() === categoryArg.selector() | ||
} | ||
selector(): string { | ||
return this._selector.toLowerCase() | ||
} | ||
} | ||
|
||
export const NoIndication: NullIndication = new NullIndication() |
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