Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Unified Diff: tools/gn/functions.cc

Issue 2095043005: Add GN split_list function (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/gn/functions.cc
diff --git a/tools/gn/functions.cc b/tools/gn/functions.cc
index 9e8646f96b16855c3aac157ceb2bdb8d6d07f405..bfa5ede7959a33ac25c22961af072c896c882de4 100644
--- a/tools/gn/functions.cc
+++ b/tools/gn/functions.cc
@@ -14,6 +14,7 @@
#include "tools/gn/config_values_generator.h"
#include "tools/gn/err.h"
#include "tools/gn/input_file.h"
+#include "tools/gn/parse_node_value_adapter.h"
#include "tools/gn/parse_tree.h"
#include "tools/gn/pool.h"
#include "tools/gn/scheduler.h"
@@ -811,6 +812,76 @@ Value RunPrint(Scope* scope,
return Value();
}
+// split_list ------------------------------------------------------------------
+
+const char kSplitList[] = "split_list";
+const char kSplitList_HelpShort[] =
+ "split_list: Splits a list into N different sub-lists.";
+const char kSplitList_Help[] =
+ "split_list: Splits a list into N different sub-lists.\n"
+ "\n"
+ " result = split_list(input, n)\n"
+ "\n"
+ " Given a list and a number N, splits the list into N sub-lists of\n"
+ " approximately equal size. The return value is a list of the sub-lists.\n"
+ " The result will always be a list of size N. If N is greater than the\n"
+ " number of elements in the input, it will be padded with empty lists.\n"
+ "\n"
+ " The expected use is to divide source files into smaller uniform\n"
+ " chunks.\n"
+ "\n"
+ "Example\n"
+ "\n"
+ " The code:\n"
+ " mylist = [1, 2, 3, 4, 5, 6]\n"
+ " print(split_list(mylist, 3))\n"
+ "\n"
+ " Will print:\n"
+ " [[1, 2], [3, 4], [5, 6]\n";
+Value RunSplitList(Scope* scope,
+ const FunctionCallNode* function,
+ const ListNode* args_list,
+ Err* err) {
+ const auto& args_vector = args_list->contents();
+ if (args_vector.size() != 2) {
+ *err = Err(function, "Wrong number of arguments to split_list().",
+ "Expecting exactly two.");
+ return Value();
+ }
+
+ ParseNodeValueAdapter list_adapter;
+ if (!list_adapter.InitForType(scope, args_vector[0].get(), Value::LIST, err))
+ return Value();
+ const std::vector<Value>& input = list_adapter.get().list_value();
+
+ ParseNodeValueAdapter count_adapter;
+ if (!count_adapter.InitForType(scope, args_vector[1].get(), Value::INTEGER,
+ err))
+ return Value();
+ int64_t count = count_adapter.get().int_value();
+ if (count <= 0) {
+ *err = Err(function, "Negative result size.");
scottmg 2016/06/27 22:06:01 <= isn't quite negative, maybe "Should be greater
+ return Value();
+ }
+
+ Value result(function, Value::LIST);
+ result.list_value().resize(count);
+
+ int64_t items_per_result =
+ static_cast<int64_t>(input.size() + count - 1) / count;
+ for (int64_t i = 0; i < count; i++) {
+ result.list_value()[i] = Value(function, Value::LIST);
+
+ // Assign all remainders to the last one in the bucket.
+ auto begin_add = input.begin() + i * items_per_result;
+ auto end_add = (i == count - 1) ? input.end()
+ : begin_add + items_per_result;
scottmg 2016/06/27 22:06:01 This doesn't seem quite right. e.g. if there's 6 i
+ result.list_value()[i].list_value().assign(begin_add, end_add);
+ }
+
+ return result;
+}
+
// -----------------------------------------------------------------------------
FunctionInfo::FunctionInfo()
@@ -923,6 +994,7 @@ struct FunctionInfoInitializer {
INSERT_FUNCTION(SetDefaults, false)
INSERT_FUNCTION(SetDefaultToolchain, false)
INSERT_FUNCTION(SetSourcesAssignmentFilter, false)
+ INSERT_FUNCTION(SplitList, false)
INSERT_FUNCTION(Template, false)
INSERT_FUNCTION(Tool, false)
INSERT_FUNCTION(Toolchain, false)

Powered by Google App Engine
This is Rietveld 408576698