author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 01 Feb 2012 00:34:28 +0000 | |
changeset 97 | 2a7827f4dce2 |
parent 72 | d16c7d84d4a8 |
child 111 | 44ac233b2f83 |
permissions | -rw-r--r-- |
10 | 1 |
"{ Package: 'stx:goodies/xtreams/transforms' }" |
2 |
||
3 |
"{ NameSpace: Xtreams }" |
|
4 |
||
5 |
WriteStream subclass:#InterpretedWriteStream |
|
6 |
instanceVariableNames:'elementSize cache cacheSize operation contentsSpecies' |
|
7 |
classVariableNames:'' |
|
97 | 8 |
poolDictionaries:'Xtreams::XtreamsPool' |
27
2cc5a8a3ca14
added XtreamsPool to fix DefaultBufferSize; set proper category names
mkobetic
parents:
10
diff
changeset
|
9 |
category:'Xtreams-Transforms' |
10 | 10 |
! |
11 |
||
12 |
InterpretedWriteStream comment:'Converts consumed elements into bytes of pre-configured (primitive) CType, e.g. float, long etc. The type of the written elements must match the CType, the underlying destination must be binary. |
|
13 |
||
14 |
Instance Variables |
|
15 |
elementSize <Integer> byte size of elements of the pre-configured CType |
|
16 |
cache <InterpretedBytes> caches the bytes so that they can be bulk written |
|
17 |
cacheSize <SmallInteger> how many elements (not bytes) do we want to cache |
|
18 |
operation <BlockClosure> CType translation primitive used to write into the buffer |
|
19 |
contentsSpecies <Class> collection type to use to hold collections of elements |
|
20 |
||
21 |
' |
|
22 |
! |
|
23 |
||
24 |
||
25 |
!InterpretedWriteStream class methodsFor:'instance creation'! |
|
26 |
||
27 |
on: aWriteStream bytesPerElement: anInteger contentsSpecies: aClass operation: aBlock cacheSize: aSize |
|
28 |
^self new on: aWriteStream bytesPerElement: anInteger contentsSpecies: aClass operation: aBlock cacheSize: aSize |
|
29 |
! |
|
30 |
||
31 |
on: aWriteStream type: aType cacheSize: aSize |
|
32 |
| interpretation | |
|
33 |
interpretation := InterpretedBytes perform: aType. |
|
34 |
^self on: aWriteStream bytesPerElement: (interpretation at: 1) contentsSpecies: (interpretation at: 2) operation: (interpretation at: 4) cacheSize: aSize |
|
35 |
! ! |
|
36 |
||
37 |
!InterpretedWriteStream methodsFor:'accessing'! |
|
38 |
||
39 |
insert: anInteger from: aSequenceableCollection at: startIndex |
|
40 |
||
41 |
| count amount | |
|
42 |
count := 0. |
|
43 |
[count = anInteger] whileFalse: |
|
44 |
[amount := cacheSize min: anInteger - count. |
|
45 |
0 to: amount - 1 do: [:index | operation value: cache value: index * elementSize + 1 value: (aSequenceableCollection at: index + startIndex)]. |
|
46 |
destination insert: amount * elementSize from: cache at: 1. |
|
47 |
count := count + amount]. |
|
48 |
^anInteger |
|
49 |
! |
|
50 |
||
51 |
put: anObject |
|
52 |
||
53 |
operation value: cache value: 1 value: anObject. |
|
54 |
destination write: elementSize from: cache at: 1. |
|
55 |
^anObject |
|
56 |
! |
|
57 |
||
58 |
write: anInteger from: aSequenceableCollection at: startIndex |
|
59 |
||
60 |
| count amount | |
|
61 |
count := 0. |
|
62 |
[count = anInteger] whileFalse: |
|
63 |
[amount := cacheSize min: anInteger - count. |
|
64 |
0 to: amount - 1 do: [:index | operation value: cache value: index * elementSize + 1 value: (aSequenceableCollection at: count + index + startIndex)]. |
|
65 |
destination write: amount * elementSize from: cache at: 1. |
|
66 |
count := count + amount]. |
|
67 |
^anInteger |
|
68 |
! ! |
|
69 |
||
70 |
!InterpretedWriteStream methodsFor:'initialize-release'! |
|
71 |
||
72 |
close |
|
73 |
super close. |
|
74 |
cache recycle. |
|
75 |
cache := nil |
|
76 |
! |
|
77 |
||
78 |
contentsSpecies |
|
79 |
^contentsSpecies |
|
80 |
! |
|
81 |
||
82 |
on: aWriteStream bytesPerElement: anInteger contentsSpecies: aClass operation: aBlock cacheSize: aSize |
|
83 |
super on: aWriteStream. |
|
84 |
cacheSize := aSize. |
|
85 |
elementSize := anInteger. |
|
86 |
contentsSpecies := aClass. |
|
87 |
operation := aBlock. |
|
88 |
cache := InterpretedBytes newRecycled: ((elementSize * cacheSize) max: DefaultBufferSize) |
|
89 |
! ! |
|
90 |
||
91 |
!InterpretedWriteStream methodsFor:'seeking'! |
|
92 |
||
93 |
++ anInteger |
|
94 |
||
95 |
anInteger < 0 ifTrue: [ ^self -- anInteger negated ]. |
|
96 |
[destination ++ anInteger * elementSize] |
|
97 |
on: Incomplete do: [:exception | (Incomplete count: exception count / elementSize) raise]. |
|
98 |
^anInteger |
|
99 |
! |
|
100 |
||
101 |
-- anInteger |
|
102 |
anInteger < 0 ifTrue: [ ^self ++ anInteger negated ]. |
|
103 |
[destination -- anInteger * elementSize] |
|
104 |
on: Incomplete do: [:exception | (Incomplete count: exception count / elementSize) raise]. |
|
105 |
^anInteger |
|
106 |
! |
|
107 |
||
108 |
length |
|
109 |
^destination length / elementSize |
|
110 |
! |
|
111 |
||
112 |
position |
|
113 |
^destination position / elementSize |
|
114 |
! |
|
115 |
||
116 |
position: aPosition |
|
117 |
||
118 |
^([ destination position: aPosition * elementSize ] on: Incomplete do: [ :ex | ex count ]) // elementSize |
|
119 |
! ! |
|
120 |
||
121 |
!InterpretedWriteStream methodsFor:'testing'! |
|
122 |
||
123 |
isPositionable |
|
124 |
||
125 |
^destination isPositionable |
|
126 |
! ! |
|
127 |
||
128 |
!InterpretedWriteStream class methodsFor:'documentation'! |
|
129 |
||
130 |
version_SVN |
|
131 |
^ '$Id$' |
|
132 |
! ! |