Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions cpp/collections_module/algorithm/collections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,30 @@ void Collections::Flatten(mgp_list *args, mgp_func_context *ctx, mgp_func_result
return;
}
}

void Collections::FrequenciesAsMap(mgp_list *args, mgp_func_context *ctx, mgp_func_result *res, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const auto arguments = mgp::List(args);
auto result = mgp::Result(res);

try {
const auto input_list = arguments[0].ValueList();
std::unordered_map<mgp::Value, int64_t> frequency_map;

for (const auto &element : input_list) {
frequency_map[element]++;
}

mgp::Map result_map;
for (const auto &[element, count] : frequency_map) {
const auto key = element.ToString();
result_map.Insert(key, mgp::Value(count));
}

result.SetValue(std::move(result_map));

} catch (const std::exception &e) {
result.SetErrorMessage(e.what());
return;
}
}
6 changes: 6 additions & 0 deletions cpp/collections_module/algorithm/collections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ constexpr std::string_view kArgumentSizePartition = "partition_size";
constexpr std::string_view kProcedureFlatten = "flatten";
constexpr std::string_view kArgumentListFlatten = "list";

/* frequencies_as_map constants */
constexpr std::string_view kProcedureFrequenciesAsMap = "frequencies_as_map";
constexpr std::string_view kArgumentListFrequenciesAsMap = "coll";

void SetResult(mgp::Result &result, const mgp::Value &value);

void SumLongs(mgp_list *args, mgp_func_context *ctx, mgp_func_result *res, mgp_memory *memory);
Expand Down Expand Up @@ -128,4 +132,6 @@ void Partition(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mg

void Flatten(mgp_list *args, mgp_func_context *ctx, mgp_func_result *res, mgp_memory *memory);

void FrequenciesAsMap(mgp_list *args, mgp_func_context *ctx, mgp_func_result *res, mgp_memory *memory);

} // namespace Collections
5 changes: 5 additions & 0 deletions cpp/collections_module/collections_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ extern "C" int mgp_init_module(struct mgp_module *module, struct mgp_memory *mem
{mgp::Parameter(Collections::kArgumentListToSet, {mgp::Type::List, mgp::Type::Any})}, module,
memory);

mgp::AddFunction(Collections::FrequenciesAsMap, Collections::kProcedureFrequenciesAsMap,
{mgp::Parameter(Collections::kArgumentListFrequenciesAsMap, {mgp::Type::List, mgp::Type::Any})},
module, memory);


AddProcedure(
Collections::Partition, std::string(Collections::kProcedurePartition).c_str(), mgp::ProcedureType::Read,
{mgp::Parameter(std::string(Collections::kArgumentListPartition).c_str(), {mgp::Type::List, mgp::Type::Any}),
Expand Down
Empty file.
5 changes: 5 additions & 0 deletions e2e/collections_test/test_frequencies_as_map1/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query: >
RETURN collections.frequencies_as_map([1,1,2,1,3,4,1]) AS result;

output:
- result: {"1": 4, "2": 1, "3": 1, "4": 1}
Empty file.
5 changes: 5 additions & 0 deletions e2e/collections_test/test_frequencies_as_map2/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query: >
RETURN collections.frequencies_as_map(['a','b','a','c','b','a']) AS result;

output:
- result: {"a": 3, "b": 2, "c": 1}
Empty file.
5 changes: 5 additions & 0 deletions e2e/collections_test/test_frequencies_as_map3/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query: >
RETURN collections.frequencies_as_map([]) AS result;

output:
- result: {}
Empty file.
5 changes: 5 additions & 0 deletions e2e/collections_test/test_frequencies_as_map4/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query: >
RETURN collections.frequencies_as_map([1, 1, null, null, 1]) AS result;

output:
- result: {"1": 3, "null": 2}
Loading