From 6941af88ad016141f568279f065cb181074f1f9f Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Thu, 6 Mar 2014 15:13:34 +0100 Subject: erl_interface: Add ei encode/decode for maps --- lib/erl_interface/doc/src/ei.xml | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'lib/erl_interface/doc/src') diff --git a/lib/erl_interface/doc/src/ei.xml b/lib/erl_interface/doc/src/ei.xml index ab185c9179..1756ee8a7d 100644 --- a/lib/erl_interface/doc/src/ei.xml +++ b/lib/erl_interface/doc/src/ei.xml @@ -4,7 +4,7 @@
- 20012013 + 20012014 Ericsson AB. All Rights Reserved. @@ -416,6 +416,27 @@ ei_x_encode_empty_list(&x); tail of a list.

+ + intei_encode_map_header(char *buf, int *index, int arity) + intei_x_encode_map_header(ei_x_buff* x, int arity) + Encode a map + +

This function encodes a map header, with a specified arity. The next + arity terms encoded will be the keys of the map, and the next + arity terms after that will be the corresponding values in + same order.

+

E.g. to encode the map #{a => "Apple", b => "Banana"}:

+
+ei_x_encode_map_header(&x, 2);
+ei_x_encode_atom(&x, "a");
+ei_x_encode_atom(&x, "b");
+ei_x_encode_string(&x, "Apple");
+ei_x_encode_string(&x, "Banana");
+        
+

A correctly encoded map can not have duplicate keys, but no check + for duplicate keys is done by this function.

+
+
intei_get_type(const char *buf, const int *index, int *type, int *size) Fetch the type and size of an encoded term @@ -637,6 +658,19 @@ ei_x_encode_empty_list(&x); instead.

+ + intei_decode_map_header(const char *buf, int *index, int *arity) + Decode a map + +

This function decodes a map header from the binary + format. The number of key-value pairs is returned in + arity. Keys and values follows, first all keys and then all values, + which makes a total of arity*2 terms. + Keys and values are paired according to their order, the first key + with the first value and so on. If arity is zero, it's an empty map. + A correctly encoded map does not have duplicate keys.

+
+
intei_decode_ei_term(const char* buf, int* index, ei_term* term) Decode a term, without prior knowledge of type -- cgit v1.2.3 From c543d5bff7fb23c3f44cc4817c0654117de78919 Mon Sep 17 00:00:00 2001 From: Sverker Eriksson Date: Wed, 12 Mar 2014 20:11:10 +0100 Subject: erts: Change external format for maps to be: 116,Arity, K1,V1,K2,V2,...,Kn,Vn instead of: 116,Arity, K1,K2,...,Kn, V1,V2,....,Vn We think this will be better for future internal map structures like HAMT. Would be bad if we need to iterate twice over HAMT in term_to_binary, one for keys and one for values. --- lib/erl_interface/doc/src/ei.xml | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'lib/erl_interface/doc/src') diff --git a/lib/erl_interface/doc/src/ei.xml b/lib/erl_interface/doc/src/ei.xml index 1756ee8a7d..90495eebd6 100644 --- a/lib/erl_interface/doc/src/ei.xml +++ b/lib/erl_interface/doc/src/ei.xml @@ -422,19 +422,18 @@ ei_x_encode_empty_list(&x); Encode a map

This function encodes a map header, with a specified arity. The next - arity terms encoded will be the keys of the map, and the next - arity terms after that will be the corresponding values in - same order.

+ arity*2 terms encoded will be the keys and values of the map + encoded in the following order: K1, V1, K2, V2, ..., Kn, Vn. +

E.g. to encode the map #{a => "Apple", b => "Banana"}:

 ei_x_encode_map_header(&x, 2);
 ei_x_encode_atom(&x, "a");
-ei_x_encode_atom(&x, "b");
 ei_x_encode_string(&x, "Apple");
+ei_x_encode_atom(&x, "b");
 ei_x_encode_string(&x, "Banana");
         
-

A correctly encoded map can not have duplicate keys, but no check - for duplicate keys is done by this function.

+

A correctly encoded map can not have duplicate keys.

@@ -664,10 +663,9 @@ ei_x_encode_string(&x, "Banana");

This function decodes a map header from the binary format. The number of key-value pairs is returned in - arity. Keys and values follows, first all keys and then all values, - which makes a total of arity*2 terms. - Keys and values are paired according to their order, the first key - with the first value and so on. If arity is zero, it's an empty map. + *arity. Keys and values follow in the following order: + K1, V1, K2, V2, ..., Kn, Vn. This makes a total of + arity*2 terms. If arity is zero, it's an empty map. A correctly encoded map does not have duplicate keys.

-- cgit v1.2.3