Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Molstar
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michal Malý
Molstar
Commits
308a6b57
Commit
308a6b57
authored
6 years ago
by
David Sehnal
Browse files
Options
Downloads
Patches
Plain Diff
Use encoding classifier for StringArray BinaryCIF encoding
parent
c3a586ad
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/mol-io/common/binary-cif/array-encoder.ts
+44
-40
44 additions, 40 deletions
src/mol-io/common/binary-cif/array-encoder.ts
with
44 additions
and
40 deletions
src/mol-io/common/binary-cif/array-encoder.ts
+
44
−
40
View file @
308a6b57
...
...
@@ -9,6 +9,7 @@
import
{
ChunkedArray
}
from
'
mol-data/util
'
import
{
Encoding
,
EncodedData
}
from
'
./encoding
'
import
{
classifyIntArray
}
from
'
./classifier
'
;
export
interface
ArrayEncoder
{
and
(
f
:
ArrayEncoding
.
Provider
):
ArrayEncoder
,
...
...
@@ -21,16 +22,16 @@ export class ArrayEncoderImpl implements ArrayEncoder {
}
encode
(
data
:
ArrayLike
<
any
>
):
EncodedData
{
le
t
encoding
:
Encoding
[]
=
[];
for
(
le
t
p
of
this
.
providers
)
{
le
t
t
=
p
(
data
);
cons
t
encoding
:
Encoding
[]
=
[];
for
(
cons
t
p
of
this
.
providers
)
{
cons
t
t
=
p
(
data
);
if
(
!
t
.
encodings
.
length
)
{
throw
new
Error
(
'
Encodings must be non-empty.
'
);
}
data
=
t
.
data
;
for
(
le
t
e
of
t
.
encodings
)
{
for
(
cons
t
e
of
t
.
encodings
)
{
encoding
.
push
(
e
);
}
}
...
...
@@ -101,14 +102,14 @@ export namespace ArrayEncoding {
}
export
function
byteArray
(
data
:
Encoding
.
TypedFloatArray
|
Encoding
.
TypedIntArray
)
{
le
t
type
=
Encoding
.
getDataType
(
data
);
cons
t
type
=
Encoding
.
getDataType
(
data
);
if
(
type
===
Encoding
.
IntDataType
.
Int8
)
return
int8
(
data
as
Int8Array
);
else
if
(
type
===
Encoding
.
IntDataType
.
Uint8
)
return
uint8
(
data
as
Uint8Array
);
le
t
result
=
new
Uint8Array
(
data
.
length
*
byteSizes
[
type
]);
le
t
w
=
writers
[
type
];
le
t
view
=
new
DataView
(
result
.
buffer
);
cons
t
result
=
new
Uint8Array
(
data
.
length
*
byteSizes
[
type
]);
cons
t
w
=
writers
[
type
];
cons
t
view
=
new
DataView
(
result
.
buffer
);
for
(
let
i
=
0
,
n
=
data
.
length
;
i
<
n
;
i
++
)
{
w
(
view
,
i
,
data
[
i
]);
}
...
...
@@ -119,8 +120,8 @@ export namespace ArrayEncoding {
}
function
_fixedPoint
(
data
:
Encoding
.
TypedFloatArray
,
factor
:
number
):
Result
{
le
t
srcType
=
Encoding
.
getDataType
(
data
)
as
Encoding
.
FloatDataType
;
le
t
result
=
new
Int32Array
(
data
.
length
);
cons
t
srcType
=
Encoding
.
getDataType
(
data
)
as
Encoding
.
FloatDataType
;
cons
t
result
=
new
Int32Array
(
data
.
length
);
for
(
let
i
=
0
,
n
=
data
.
length
;
i
<
n
;
i
++
)
{
result
[
i
]
=
Math
.
round
(
data
[
i
]
*
factor
);
}
...
...
@@ -132,7 +133,7 @@ export namespace ArrayEncoding {
export
function
fixedPoint
(
factor
:
number
):
Provider
{
return
data
=>
_fixedPoint
(
data
as
Encoding
.
TypedFloatArray
,
factor
);
}
function
_intervalQuantizaiton
(
data
:
Encoding
.
TypedFloatArray
,
min
:
number
,
max
:
number
,
numSteps
:
number
,
arrayType
:
new
(
size
:
number
)
=>
Encoding
.
TypedIntArray
):
Result
{
le
t
srcType
=
Encoding
.
getDataType
(
data
)
as
Encoding
.
FloatDataType
;
cons
t
srcType
=
Encoding
.
getDataType
(
data
)
as
Encoding
.
FloatDataType
;
if
(
!
data
.
length
)
{
return
{
encodings
:
[{
kind
:
'
IntervalQuantization
'
,
min
,
max
,
numSteps
,
srcType
}],
...
...
@@ -141,16 +142,16 @@ export namespace ArrayEncoding {
}
if
(
max
<
min
)
{
le
t
t
=
min
;
cons
t
t
=
min
;
min
=
max
;
max
=
t
;
}
le
t
delta
=
(
max
-
min
)
/
(
numSteps
-
1
);
cons
t
delta
=
(
max
-
min
)
/
(
numSteps
-
1
);
le
t
output
=
new
arrayType
(
data
.
length
);
cons
t
output
=
new
arrayType
(
data
.
length
);
for
(
let
i
=
0
,
n
=
data
.
length
;
i
<
n
;
i
++
)
{
le
t
v
=
data
[
i
];
cons
t
v
=
data
[
i
];
if
(
v
<=
min
)
output
[
i
]
=
0
;
else
if
(
v
>=
max
)
output
[
i
]
=
numSteps
;
else
output
[
i
]
=
(
Math
.
round
((
v
-
min
)
/
delta
))
|
0
;
...
...
@@ -186,7 +187,7 @@ export namespace ArrayEncoding {
fullLength
+=
2
;
}
}
le
t
output
=
new
Int32Array
(
fullLength
);
cons
t
output
=
new
Int32Array
(
fullLength
);
let
offset
=
0
;
let
runLength
=
1
;
for
(
let
i
=
1
,
il
=
data
.
length
;
i
<
il
;
i
++
)
{
...
...
@@ -224,8 +225,8 @@ export namespace ArrayEncoding {
};
}
le
t
output
=
new
(
data
as
any
).
constructor
(
data
.
length
);
le
t
origin
=
data
[
0
];
cons
t
output
=
new
(
data
as
any
).
constructor
(
data
.
length
);
cons
t
origin
=
data
[
0
];
output
[
0
]
=
data
[
0
];
for
(
let
i
=
1
,
n
=
data
.
length
;
i
<
n
;
i
++
)
{
output
[
i
]
=
data
[
i
]
-
data
[
i
-
1
];
...
...
@@ -245,10 +246,10 @@ export namespace ArrayEncoding {
}
function
packingSize
(
data
:
Int32Array
,
upperLimit
:
number
)
{
le
t
lowerLimit
=
-
upperLimit
-
1
;
cons
t
lowerLimit
=
-
upperLimit
-
1
;
let
size
=
0
;
for
(
let
i
=
0
,
n
=
data
.
length
;
i
<
n
;
i
++
)
{
le
t
value
=
data
[
i
];
cons
t
value
=
data
[
i
];
if
(
value
===
0
)
{
size
+=
1
;
}
else
if
(
value
>
0
)
{
...
...
@@ -263,9 +264,9 @@ export namespace ArrayEncoding {
}
function
determinePacking
(
data
:
Int32Array
):
{
isSigned
:
boolean
,
size
:
number
,
bytesPerElement
:
number
}
{
le
t
signed
=
isSigned
(
data
);
le
t
size8
=
signed
?
packingSize
(
data
,
0x7F
)
:
packingSize
(
data
,
0xFF
);
le
t
size16
=
signed
?
packingSize
(
data
,
0x7FFF
)
:
packingSize
(
data
,
0xFFFF
);
cons
t
signed
=
isSigned
(
data
);
cons
t
size8
=
signed
?
packingSize
(
data
,
0x7F
)
:
packingSize
(
data
,
0xFF
);
cons
t
size16
=
signed
?
packingSize
(
data
,
0x7FFF
)
:
packingSize
(
data
,
0xFFFF
);
if
(
data
.
length
*
4
<
size16
*
2
)
{
// 4 byte packing is the most effective
...
...
@@ -292,13 +293,13 @@ export namespace ArrayEncoding {
}
function
_integerPacking
(
data
:
Int32Array
,
packing
:
{
isSigned
:
boolean
,
size
:
number
,
bytesPerElement
:
number
}):
Result
{
le
t
upperLimit
=
packing
.
isSigned
cons
t
upperLimit
=
packing
.
isSigned
?
(
packing
.
bytesPerElement
===
1
?
0x7F
:
0x7FFF
)
:
(
packing
.
bytesPerElement
===
1
?
0xFF
:
0xFFFF
);
le
t
lowerLimit
=
-
upperLimit
-
1
;
le
t
n
=
data
.
length
;
le
t
packed
=
packing
.
isSigned
cons
t
lowerLimit
=
-
upperLimit
-
1
;
cons
t
n
=
data
.
length
;
cons
t
packed
=
packing
.
isSigned
?
packing
.
bytesPerElement
===
1
?
new
Int8Array
(
packing
.
size
)
:
new
Int16Array
(
packing
.
size
)
:
packing
.
bytesPerElement
===
1
?
new
Uint8Array
(
packing
.
size
)
:
new
Uint16Array
(
packing
.
size
);
let
j
=
0
;
...
...
@@ -321,7 +322,7 @@ export namespace ArrayEncoding {
++
j
;
}
le
t
result
=
byteArray
(
packed
);
cons
t
result
=
byteArray
(
packed
);
return
{
encodings
:
[{
kind
:
'
IntegerPacking
'
,
...
...
@@ -343,7 +344,7 @@ export namespace ArrayEncoding {
throw
new
Error
(
'
Integer packing can only be applied to Int32 data.
'
);
}
le
t
packing
=
determinePacking
(
data
);
cons
t
packing
=
determinePacking
(
data
);
if
(
packing
.
bytesPerElement
===
4
)
{
// no packing done, Int32 encoding will be used
...
...
@@ -354,16 +355,15 @@ export namespace ArrayEncoding {
}
export
function
stringArray
(
data
:
string
[]):
Result
{
let
map
:
any
=
Object
.
create
(
null
);
let
strings
:
string
[]
=
[];
let
accLength
=
0
;
let
offsets
=
ChunkedArray
.
create
<
number
>
(
Int32Array
,
1
,
Math
.
min
(
1024
,
data
.
length
<
32
?
data
.
length
+
1
:
Math
.
round
(
data
.
length
/
8
)
+
1
));
let
output
=
new
Int32Array
(
data
.
length
);
const
map
:
any
=
Object
.
create
(
null
);
const
strings
:
string
[]
=
[];
const
output
=
new
Int32Array
(
data
.
length
);
const
offsets
=
ChunkedArray
.
create
<
number
>
(
Int32Array
,
1
,
Math
.
min
(
1024
,
data
.
length
<
32
?
data
.
length
+
1
:
Math
.
round
(
data
.
length
/
8
)
+
1
));
ChunkedArray
.
add
(
offsets
,
0
);
let
accLength
=
0
;
let
i
=
0
;
for
(
le
t
s
of
data
)
{
for
(
cons
t
s
of
data
)
{
// handle null strings.
if
(
s
===
null
||
s
===
void
0
)
{
output
[
i
++
]
=
-
1
;
...
...
@@ -386,12 +386,16 @@ export namespace ArrayEncoding {
output
[
i
++
]
=
index
;
}
let
encOffsets
=
ArrayEncoder
.
by
(
delta
).
and
(
integerPacking
).
encode
(
ChunkedArray
.
compact
(
offsets
));
let
encOutput
=
ArrayEncoder
.
by
(
delta
).
and
(
runLength
).
and
(
integerPacking
).
encode
(
output
);
const
offsetArray
=
ChunkedArray
.
compact
(
offsets
);
const
offsetEncoding
=
classifyIntArray
(
offsetArray
);
const
encodedOddsets
=
offsetEncoding
.
encode
(
offsetArray
);
const
dataEncoding
=
classifyIntArray
(
output
);
const
encodedData
=
dataEncoding
.
encode
(
output
);
return
{
encodings
:
[{
kind
:
'
StringArray
'
,
dataEncoding
:
enc
Output
.
encoding
,
stringData
:
strings
.
join
(
''
),
offsetEncoding
:
enc
Off
sets
.
encoding
,
offsets
:
enc
Off
sets
.
data
}],
data
:
enc
Output
.
data
encodings
:
[{
kind
:
'
StringArray
'
,
dataEncoding
:
enc
odedData
.
encoding
,
stringData
:
strings
.
join
(
''
),
offsetEncoding
:
enc
odedOdd
sets
.
encoding
,
offsets
:
enc
odedOdd
sets
.
data
}],
data
:
enc
odedData
.
data
};
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment