Skip to content

Commit

Permalink
added strict=true to zip
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietro Max Marsella committed Jan 11, 2025
1 parent 3cc3407 commit a17c76e
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/utils/include/utils/containers/zip.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
#include <tuple>
#include <utility>
#include <vector>
#include "utils/exception.h"
#include "fmt/format.h"

namespace FlexFlow {

template <typename L, typename R>
std::vector<std::pair<L, R>> zip(std::vector<L> const &l,
std::vector<R> const &r) {
std::vector<R> const &r,
bool strict = false) {
if (strict && l.size() != r.size()) {
throw mk_runtime_error(fmt::format(
"When strict = true, vector sizes must match. Got vectors of length {} and {}",
l.size(), r.size()));
}

std::vector<std::pair<L, R>> result;
for (int i = 0; i < std::min(l.size(), r.size()); i++) {
result.push_back(std::make_pair(l.at(i), r.at(i)));
Expand All @@ -19,8 +28,15 @@ std::vector<std::pair<L, R>> zip(std::vector<L> const &l,

template <typename A, typename B, typename C>
std::vector<std::tuple<A, B, C>> zip(std::vector<A> const &a,
std::vector<B> const &b,
std::vector<C> const &c) {
std::vector<B> const &b,
std::vector<C> const &c,
bool strict = false) {
if (strict && (a.size() != b.size() || b.size() != c.size())) {
throw std::runtime_error(fmt::format(
"When strict = true, vectors sizes must match. Got vectors of length {}, {} and {}",
a.size(), b.size(), c.size()));
}

std::vector<std::tuple<A, B, C>> result;
for (int i = 0; i < std::min({a.size(), b.size(), c.size()}); i++) {
result.push_back(std::make_tuple(a.at(i), b.at(i), c.at(i)));
Expand Down

0 comments on commit a17c76e

Please sign in to comment.