@@ -8,11 +8,12 @@ open System.Collections.Generic
88module ArcTable =
99
1010 let encoder ( table : ArcTable ) =
11- let keyEncoder : Encoder < int * int > = Encode.tuple2 Encode.int Encode.int
11+ let valueMap = table.Values.ValueMap
12+ let cellEncoder : Encoder < int > = fun hash -> CompositeCell.encoder valueMap.[ hash]
1213 let columnEncoder ( col : ArcTableAux.ColumnValueRefs ) : IEncodable =
1314 match col with
14- | ArcTableAux.ColumnValueRefs.Constant hash -> Encode.int hash
15- | ArcTableAux.ColumnValueRefs.Sparse cells -> Encode.intDictionary Encode.int cells
15+ | ArcTableAux.ColumnValueRefs.Constant hash -> cellEncoder hash
16+ | ArcTableAux.ColumnValueRefs.Sparse cells -> Encode.intDictionary cellEncoder cells
1617
1718 Encode.object [
1819 " name" , Encode.string table.Name
@@ -21,7 +22,6 @@ module ArcTable =
2122 for h in table.Headers do yield CompositeHeader.encoder h
2223 ]
2324 if table.Values.RowCount <> 0 then
24- " cells" , Encode.intDictionary CompositeCell.encoder table.Values.ValueMap
2525 " columns" , Encode.intDictionary columnEncoder table.Values.Columns
2626 " rowCount" , Encode.int table.RowCount
2727 ]
@@ -44,20 +44,24 @@ module ArcTable =
4444 )
4545
4646 let decoder : Decoder < ArcTable > =
47-
47+ let valueMap = Dictionary< int, CompositeCell>()
48+ let cellDecoder : Decoder < int > =
49+ CompositeCell.decoder
50+ |> Decode.map ( fun cell ->
51+ ArcTableAux.ensureCellHashInValueMap cell valueMap
52+ )
4853 let columnDecoder : Decoder < ArcTableAux.ColumnValueRefs > =
49- Decode.oneOf [
50- Decode.int |> Decode.map ArcTableAux.ColumnValueRefs.Constant
51- Decode.dictionary Decode.int Decode.int |> Decode.map ArcTableAux.ColumnValueRefs.Sparse
54+ Decode.tryOneOf [
55+ cellDecoder |> Decode.map ArcTableAux.ColumnValueRefs.Constant
56+ Decode.intDictionary cellDecoder |> Decode.map ArcTableAux.ColumnValueRefs.Sparse
5257 ]
5358 let decoder : Decoder < ArcTable > =
5459 Decode.object( fun get ->
5560 let decodedHeader = get.Optional.Field " headers" ( Decode.resizeArray CompositeHeader.decoder) |> Option.defaultValue ( ResizeArray())
56- let decodedCells = get.Optional.Field " cells" ( Decode.dictionary Decode.int CompositeCell.decoder) |> Option.defaultValue ( Dictionary())
5761 let decodedColumns = get.Optional.Field " columns" ( Decode.dictionary Decode.int columnDecoder) |> Option.defaultValue ( Dictionary())
5862 let rowCount = get.Optional.Field " rowCount" Decode.int |> Option.defaultValue 0
5963
60- let values = ArcTableAux.ArcTableValues( decodedColumns, decodedCells , rowCount)
64+ let values = ArcTableAux.ArcTableValues( decodedColumns, valueMap , rowCount)
6165
6266 ArcTable.fromArcTableValues(
6367 get.Required.Field " name" Decode.string,
@@ -134,20 +138,20 @@ module ArcTable =
134138 open StringTable
135139
136140 let encoderCompressed ( stringTable : StringTableMap ) ( oaTable : OATableMap ) ( cellTable : CellTableMap ) ( table : ArcTable ) =
137- let keyEncoder : Encoder < int * int > = Encode.tuple2 Encode.int Encode.int
141+ let cellEncoder ( hash : int ) =
142+ CellTable.encodeCell cellTable ( table.Values.ValueMap.[ hash])
138143 let columnEncoder ( col : ArcTableAux.ColumnValueRefs ) : IEncodable =
139144 match col with
140- | ArcTableAux.ColumnValueRefs.Constant hash -> Encode.int hash
141- | ArcTableAux.ColumnValueRefs.Sparse cells -> Encode.intDictionary Encode.int cells
145+ | ArcTableAux.ColumnValueRefs.Constant hash -> cellEncoder hash
146+ | ArcTableAux.ColumnValueRefs.Sparse cells -> Encode.intDictionary cellEncoder cells
142147 Encode.object [
143148 " n" , StringTable.encodeString stringTable table.Name
144149 if table.Headers.Count <> 0 then
145150 " h" , Encode.list [
146151 for h in table.Headers do yield CompositeHeader.encoder h
147152 ]
148153 if table.Values.RowCount <> 0 then
149- " ce" , Encode.intDictionary ( CellTable.encodeCell cellTable) table.Values.ValueMap
150- " co" , Encode.intDictionary columnEncoder table.Values.Columns
154+ " c" , Encode.intDictionary columnEncoder table.Values.Columns
151155 " r" , Encode.int table.RowCount
152156 ]
153157
@@ -170,20 +174,26 @@ module ArcTable =
170174 )
171175
172176 let decoderCompressed ( stringTable : StringTableArray ) ( oaTable : OATableArray ) ( cellTable : CellTableArray ) : Decoder < ArcTable > =
173-
177+ let valueMap = Dictionary< int, CompositeCell>()
178+ let cellDecoder : Decoder < int > =
179+ Decode.int
180+ |> Decode.map ( fun i ->
181+ let cell = cellTable.[ i]
182+ ArcTableAux.ensureCellHashInValueMap cell valueMap
183+ )
174184 let columnDecoder : Decoder < ArcTableAux.ColumnValueRefs > =
175- Decode.oneOf [
176- Decode.int |> Decode.map ArcTableAux.ColumnValueRefs.Constant
177- Decode.dictionary Decode.int Decode.int |> Decode.map ArcTableAux.ColumnValueRefs.Sparse
185+ Decode.tryOneOf [
186+ cellDecoder |> Decode.map ArcTableAux.ColumnValueRefs.Constant
187+ Decode.intDictionary cellDecoder |> Decode.map ArcTableAux.ColumnValueRefs.Sparse
178188 ]
179189 let decoder : Decoder < ArcTable > =
180190 Decode.object( fun get ->
181191 let decodedHeader = get.Optional.Field " h" ( Decode.resizeArray CompositeHeader.decoder) |> Option.defaultValue ( ResizeArray())
182- let decodedCells = get.Optional.Field " ce " ( Decode.dictionary Decode.int ( CellTable.decodeCell cellTable )) |> Option.defaultValue ( Dictionary ())
183- let decodedColumns = get.Optional.Field " co " ( Decode.dictionary Decode.int columnDecoder) |> Option.defaultValue ( Dictionary())
192+
193+ let decodedColumns = get.Optional.Field " c " ( Decode.dictionary Decode.int columnDecoder) |> Option.defaultValue ( Dictionary())
184194 let rowCount = get.Optional.Field " r" Decode.int |> Option.defaultValue 0
185195
186- let values = ArcTableAux.ArcTableValues( decodedColumns, decodedCells , rowCount)
196+ let values = ArcTableAux.ArcTableValues( decodedColumns, valueMap , rowCount)
187197
188198 ArcTable.fromArcTableValues(
189199 get.Required.Field " n" ( StringTable.decodeString stringTable),
@@ -193,7 +203,7 @@ module ArcTable =
193203 )
194204 { new Decoder< ArcTable> with
195205 member this.Decode ( helper , column ) =
196- if helper.hasProperty " c " column then
206+ if helper.hasProperty " r " column |> not then
197207 // This is the old format, we need to decode it with the old decoder
198208 ( decoderCompressedV2Deprecated stringTable oaTable cellTable). Decode( helper, column)
199209 else
0 commit comments