You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by ro...@apache.org on 2013/03/22 19:02:08 UTC

git commit: THRIFT-1855 deep-copy for collections of primitives should use copy-constructor Patch: Vitali Lovich

Updated Branches:
  refs/heads/master a51186b7f -> 948a2d4fb


THRIFT-1855 deep-copy for collections of primitives should use copy-constructor
Patch: Vitali Lovich


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/948a2d4f
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/948a2d4f
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/948a2d4f

Branch: refs/heads/master
Commit: 948a2d4fb8d37793974206e4c766df2b26520aba
Parents: a51186b
Author: Roger Meier <ro...@apache.org>
Authored: Fri Mar 22 19:01:25 2013 +0100
Committer: Roger Meier <ro...@apache.org>
Committed: Fri Mar 22 19:01:25 2013 +0100

----------------------------------------------------------------------
 compiler/cpp/src/generate/t_java_generator.cc |   23 +++++++++++++++++++-
 1 files changed, 22 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/948a2d4f/compiler/cpp/src/generate/t_java_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/generate/t_java_generator.cc b/compiler/cpp/src/generate/t_java_generator.cc
index 005cba4..7f66a0b 100644
--- a/compiler/cpp/src/generate/t_java_generator.cc
+++ b/compiler/cpp/src/generate/t_java_generator.cc
@@ -3594,8 +3594,29 @@ void t_java_generator::generate_deep_copy_container(ofstream &out, std::string s
     source_name = source_name_p1;
   else
     source_name = source_name_p1 + "." + source_name_p2;
+  
+  bool copy_construct_container;
+  if (container->is_map()) {
+    t_map *tmap = (t_map *)container;
+    copy_construct_container = tmap->get_key_type()->is_base_type() && tmap->get_val_type()->is_base_type();
+  } else {
+    t_type* elem_type = container->is_list() ? ((t_list *) container)->get_elem_type() :
+                                               ((t_set *) container)->get_elem_type();
+    copy_construct_container = elem_type->is_base_type();
+  }
 
-  indent(out) << type_name(type, true, false) << " " << result_name << " = new " << type_name(container, false, true) << "();" << endl;
+  if (copy_construct_container) {
+    // deep copy of base types can be done much more efficiently than iterating over all the elements manually
+    indent(out) << type_name(type, true, false) << " " << result_name << " = new " << type_name(container, false, true) << "(" << source_name << ");" << endl;
+    return;
+  }
+
+  std::string capacity;
+  if (!(sorted_containers_ && (container->is_map() || container->is_set()))) {
+    // unsorted containers accept a capacity value
+    capacity = source_name + ".size()";
+  }
+  indent(out) << type_name(type, true, false) << " " << result_name << " = new " << type_name(container, false, true) << "(" << capacity << ");" << endl;
 
   std::string iterator_element_name = source_name_p1 + "_element";
   std::string result_element_name = result_name + "_copy";