@@ -30,13 +30,43 @@ public DataInfo(string name, Type type, int[] dimension)
3030 [ Serializable ]
3131 protected struct DataContainer
3232 {
33- public DataContainer ( DataInfo info , int maxDataCount )
33+
34+ public DataInfo info ;
35+ public Array dataList ;
36+
37+ public DataContainer ( DataInfo info ) : this ( info , 8 )
3438 {
39+ }
40+
41+ public DataContainer ( DataInfo info , int reservedSize )
42+ {
43+ Debug . Assert ( reservedSize > 0 , "reservedSize needs to be larger than 0" ) ;
44+ reservedSize = Mathf . Max ( 1 , reservedSize ) ;
3545 this . info = info ;
36- dataList = Array . CreateInstance ( info . type , ( new int [ ] { maxDataCount } ) . Concat ( info . dimension ) . ToArray ( ) ) ;
46+ dataList = Array . CreateInstance ( info . type , ( new int [ ] { reservedSize } ) . Concat ( info . dimension ) . ToArray ( ) ) ;
3747 }
38- public DataInfo info ;
39- public Array dataList ;
48+
49+
50+
51+
52+
53+ public void IncreaseArraySize ( int sizeToAdd )
54+ {
55+ Debug . Assert ( sizeToAdd > 0 , "Size to add needs to be larger than 0" ) ;
56+ sizeToAdd = Mathf . Max ( 1 , sizeToAdd ) ;
57+
58+ var newArray = Array . CreateInstance ( info . type , ( new int [ ] { dataList . GetLength ( 0 ) + sizeToAdd } ) . Concat ( info . dimension ) . ToArray ( ) ) ;
59+ int typeSize = Marshal . SizeOf ( info . type ) ;
60+ Buffer . BlockCopy ( dataList , 0 , newArray , 0 , dataList . Length * info . unitLength * typeSize ) ;
61+ dataList = newArray ;
62+
63+ }
64+
65+ public int CurrentSize ( )
66+ {
67+ return dataList . GetLength ( 0 ) ;
68+ }
69+
4070 }
4171
4272
@@ -105,25 +135,38 @@ public void AddData(params ValueTuple<string, Array>[] data)
105135
106136 //feed the data.
107137
108- //add the episode to the buffer
138+ //calucate numbers for adding the episode to the buffer
109139 int numToAdd = size ;
110140 int spaceLeft = MaxCount - nextBufferPointer ;
141+ if ( MaxCount <= 0 )
142+ spaceLeft = Int32 . MaxValue ;
111143
112144 int appendSize = Mathf . Min ( spaceLeft , numToAdd ) ;
113145 int fromStartSize = Mathf . Max ( 0 , numToAdd - spaceLeft ) ;
114146
147+ CurrentCount += numToAdd ;
148+ if ( MaxCount > 0 )
149+ CurrentCount = Mathf . Clamp ( CurrentCount , 0 , MaxCount ) ;
150+
115151 foreach ( var k in data )
116152 {
153+ //resize the data container if needed
154+ while ( CurrentCount > dataset [ k . Item1 ] . CurrentSize ( ) )
155+ {
156+ dataset [ k . Item1 ] . IncreaseArraySize ( dataset [ k . Item1 ] . CurrentSize ( ) ) ;
157+ }
158+
117159 DataContainer dd = dataset [ k . Item1 ] ;
118160 int typeSize = Marshal . SizeOf ( dd . info . type ) ;
119161 //Debug.Log(k.Item1);
120162 //Debug.Log("add length " + k.Item2.Length + " copy length " + (appendSize * dd.info.unitLength).ToString());
121163 //Array.Copy(k.Item2, 0, dd.dataList, nextBufferPointer * dd.info.unitLength, appendSize * dd.info.unitLength);
122164 Buffer . BlockCopy ( k . Item2 , 0 , dd . dataList , nextBufferPointer * dd . info . unitLength * typeSize , appendSize * dd . info . unitLength * typeSize ) ;
123165 }
166+
167+
124168 nextBufferPointer += appendSize ;
125- CurrentCount += numToAdd ;
126- CurrentCount = Mathf . Clamp ( CurrentCount , 0 , MaxCount ) ;
169+
127170 if ( fromStartSize > 0 )
128171 {
129172 foreach ( var k in data )
0 commit comments