Commit 4814a314 authored by Jörn Wilms's avatar Jörn Wilms

added ability to copy associative arrays to COPY

parent 39bf7e4d
Pipeline #6777 passed with stages
in 42 seconds
......@@ -7,6 +7,7 @@ define COPY() %{{{
% \usage{Struct_Type copy = COPY(Struct_Type copyme);}
%\altusage{Array_Type copy = COPY(Array_Type copyme);}
%\altusage{List_Type copy = COPY(List_Type copyme);}
%\altusage{Assoc_Type copy = COPY(Assoc_Type copyme);}
%\description
% \code{COPY} returns a properly dereferenced copy of an
% arbitrarily nested \code{Struct/Array or List_Type} with
......@@ -16,7 +17,7 @@ define COPY() %{{{
% iteratively calls the according sub-function
% (\code{struct_copy}, \code{array_copy} or \code{list_copy}).
% If the given Data_Type or one of its entries is not
% one of privouse mentioned ones, \code{COPY} returns
% one of previously mentioned ones, \code{COPY} returns
% @(Data_Type) or respectively @(entry).
%
% NOTE: * Ref_Type entries will not be dereferenced, which
......@@ -43,7 +44,7 @@ define COPY() %{{{
% C = struct_copy(S); C.f[0].a[[0]] = ["modified"];
% print(S.f[0].a);
%
%\seealso{struct_copy, array_copy, list_copy}
%\seealso{struct_copy, array_copy, list_copy, assoc_copy}
%!%-
{
variable copyme;
......@@ -58,6 +59,8 @@ define COPY() %{{{
return array_copy(copyme);
else if( type == List_Type )
return array_copy(copyme);
else if( type == Assoc_Type )
return assoc_copy(copyme);
else{
vmessage("WARNING: <%s>: Unsupported Data_Type=%S, retruning @copyme",
_function_name,type);
......@@ -76,14 +79,13 @@ define struct_copy() %{{{
% fields, which can be of any Type! If a field is an Array_Type[]
% \code{struct_copy} calls \code{array_copy} and if a field is
% \code{Struct_Type}, \code{struct_copy} calls itself.
% Further it copies Ref_Type, which allows
% !!! copiing XFIG-OBJECTS !!!
% Further it copies Ref_Type, which allows copying XFIG-Objects.
%\example
% s = Struct_Type[1]; s[0]=struct{ a=Array_Type[1,2] };
% s[0].a[[0]]=[0:9]; S = struct{ f=s };
% C = struct_copy(S); C.f[0].a[[0]] = ["modified"];
% print(S.f[0].a);
%\seealso{array_copy}
%\seealso{array_copy, list_copy, COPY, assoc_copy}
%!%-
{
variable table;
......@@ -103,6 +105,8 @@ define struct_copy() %{{{
set_struct_field(copy, f, array_copy(field) );
else if( ftype == List_Type )
set_struct_field(copy, f, list_copy(field) );
else if( ftype == Assoc_Type )
set_struct_field(copy, f, assoc_copy(field) );
else if ( (ftype == String_Type) || (ftype == Ref_Type) )
set_struct_field(copy, f, field );
else
......@@ -159,6 +163,7 @@ define array_copy() %{{{
return copy;
}
%}}}
define list_copy() %{{{
%!%+
%\function{list_copy}
......@@ -179,7 +184,7 @@ define list_copy() %{{{
% l = [{ array_copy(s), 1., [1:10] }, {"abs"}];
% copy = COPY(l); copy[0][0][0].a[[0]] = ["modified"];
% print(l[0][0][0].a);
%\seealso{COPY, struct_copy, array_copy}
%\seealso{COPY, struct_copy, array_copy, assoc_copy}
%!%-
{
variable list;
......@@ -193,6 +198,8 @@ define list_copy() %{{{
return struct_copy( list );
else if( type == Array_Type )
return array_copy( list );
else if( type == Assoc_Type )
return assoc_copy( list );
else if( type == Ref_Type || type == String_Type )
return list;
else
......@@ -229,3 +236,44 @@ define table_copy() %{{{
return struct_copy(table);
}
%}}}
define assoc_copy()
%!%+
%\function{assoc_copy}
%\synopsis{makes a copy of an associated array}
%\usage{Assoc_Type copy = assoc_copy(Assoc_Type list);}
%\description
% \code{assoc_copy} returns a properly dereferenced copy of an
% arbitrarily nested \code{Assoc_Type} with all its entries.
% Depending on the Data_Type of the entries, \code{assoc_copy}
% iteratively calls the according sub-function
% (\code{struct_copy}, \code{array_copy} or \code{list_copy}).
% If the Data_Type of one of its entries is not
% one of previously mentioned ones, \code{assoc_copy} returns
% @(Data_Type) or respectively @(entry).
%\example
% s = Struct_Type[1]; s[0]=struct{ a=Array_Type[1,2] };
% s[0].a[[0]]=[0:9];
% l = [{ array_copy(s), 1., [1:10] }, {"abs"}];
% copy = COPY(l); copy[0][0][0].a[[0]] = ["modified"];
% print(l[0][0][0].a);
%\seealso{COPY, struct_copy, array_copy, list_copy}
%!%-
{
variable copyme;
switch(_NARGS)
{ case 1: copyme = (); }
{ help(_function_name()); return; }
variable new=Assoc_Type[];
variable s=assoc_get_values(copyme);
variable key;
foreach key ( s ) {
new[key]=COPY(s[key]);
}
return new;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment