2013-09-11 18:36:05 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#pragma once
|
2013-09-13 10:35:29 +00:00
|
|
|
#include <utility>
|
2013-11-18 23:29:35 +00:00
|
|
|
#include <functional>
|
2013-09-13 10:35:29 +00:00
|
|
|
#include "util/list.h"
|
|
|
|
#include "util/buffer.h"
|
|
|
|
#include "util/pair.h"
|
2013-09-11 18:36:05 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Copy the values in the list \c l to the buffer \c r. */
|
2013-09-11 18:36:05 +00:00
|
|
|
template<typename T>
|
|
|
|
void to_buffer(list<T> const & l, buffer<T> & r) {
|
|
|
|
for (T const & v : l) {
|
|
|
|
r.push_back(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Copy the cells in the list \c l to the buffer \c r. */
|
2013-11-19 00:47:52 +00:00
|
|
|
template<typename T>
|
|
|
|
void to_buffer(list<T> const & l, buffer<typename list<T>::cell *> & r) {
|
|
|
|
typename list<T>::cell * it = l.raw();
|
|
|
|
while (it) {
|
|
|
|
r.push_back(it);
|
|
|
|
it = it->tail().raw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Create a list using the values in the cells from \c begin to \c end. */
|
2013-09-11 18:36:05 +00:00
|
|
|
template<typename T>
|
2013-11-19 01:11:59 +00:00
|
|
|
list<T> cells_to_list(typename list<T>::cell * const * begin, typename list<T>::cell * const * end) {
|
|
|
|
list<T> r;
|
|
|
|
auto it = end;
|
|
|
|
while (it != begin) {
|
|
|
|
--it;
|
|
|
|
r = cons((*it)->head(), r);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Create a list using the values in the cells from \c b. */
|
2013-11-19 01:11:59 +00:00
|
|
|
template<typename T>
|
|
|
|
list<T> buffer_to_list(buffer<typename list<T>::cell*> const & b) {
|
|
|
|
return cells_to_list<T>(b.begin(), b.end());
|
2013-09-11 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Return the reverse list. */
|
2013-09-11 18:36:05 +00:00
|
|
|
template<typename T>
|
|
|
|
list<T> reverse(list<T> const & l) {
|
2013-11-19 01:11:59 +00:00
|
|
|
if (is_nil(l)) {
|
|
|
|
return l;
|
|
|
|
} else {
|
|
|
|
buffer<typename list<T>::cell *> tmp;
|
|
|
|
to_buffer(l, tmp);
|
|
|
|
std::reverse(tmp.begin(), tmp.end());
|
|
|
|
return buffer_to_list<T>(tmp);
|
|
|
|
}
|
2013-09-11 18:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
\brief Return two lists \c l1 and \c l2 of approximately the same size s.t.
|
|
|
|
<tt>append(l1, l2) == l</tt>
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
std::pair<list<T>, list<T>> split(list<T> const & l) {
|
|
|
|
if (is_nil(l)) {
|
|
|
|
return mk_pair(l, l);
|
|
|
|
} else if (is_nil(cdr(l))) {
|
|
|
|
return mk_pair(l, list<T>());
|
|
|
|
} else {
|
2013-11-19 01:11:59 +00:00
|
|
|
buffer<typename list<T>::cell*> tmp;
|
2013-09-11 18:36:05 +00:00
|
|
|
to_buffer(l, tmp);
|
|
|
|
unsigned mid = tmp.size() / 2;
|
|
|
|
auto beg = tmp.begin();
|
|
|
|
lean_assert(beg + mid <= tmp.end());
|
2013-11-19 01:11:59 +00:00
|
|
|
return mk_pair(cells_to_list<T>(beg, beg + mid),
|
|
|
|
cells_to_list<T>(beg + mid, tmp.end()));
|
2013-09-11 18:36:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 21:10:39 +00:00
|
|
|
/**
|
|
|
|
\brief Return two lists \c l1 and \c l2 of approximately the same size s.t.
|
|
|
|
<tt>append(l1, reverse(l2)) == l</tt>
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
std::pair<list<T>, list<T>> split_reverse_second(list<T> const & l) {
|
|
|
|
if (is_nil(l)) {
|
|
|
|
return mk_pair(l, l);
|
|
|
|
} else if (is_nil(cdr(l))) {
|
|
|
|
return mk_pair(l, list<T>());
|
|
|
|
} else {
|
|
|
|
buffer<T> tmp;
|
|
|
|
to_buffer(l, tmp);
|
|
|
|
unsigned mid = tmp.size() / 2;
|
|
|
|
auto beg = tmp.begin();
|
|
|
|
lean_assert(beg + mid <= tmp.end());
|
|
|
|
return mk_pair(to_list(beg, beg + mid), reverse_to_list(beg+mid, tmp.end()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Append two lists */
|
2013-09-11 18:36:05 +00:00
|
|
|
template<typename T>
|
|
|
|
list<T> append(list<T> const & l1, list<T> const & l2) {
|
2013-09-27 01:03:34 +00:00
|
|
|
if (!l1) {
|
|
|
|
return l2;
|
|
|
|
} else if (!l2) {
|
|
|
|
return l1;
|
|
|
|
} else {
|
2013-11-19 01:11:59 +00:00
|
|
|
buffer<typename list<T>::cell*> tmp;
|
2013-09-27 01:03:34 +00:00
|
|
|
list<T> r = l2;
|
|
|
|
to_buffer(l1, tmp);
|
|
|
|
unsigned i = tmp.size();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
2013-11-19 01:11:59 +00:00
|
|
|
r = cons(tmp[i]->head(), r);
|
2013-09-27 01:03:34 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2013-09-11 18:36:05 +00:00
|
|
|
}
|
2013-09-13 01:25:38 +00:00
|
|
|
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Given list <tt>(a_0, ..., a_k)</tt>, return list <tt>(f(a_0), ..., f(a_k))</tt>. */
|
2014-05-18 02:18:18 +00:00
|
|
|
template<typename To, typename From, typename F>
|
|
|
|
list<To> map2(list<From> const & l, F && f) {
|
|
|
|
static_assert(std::is_same<typename std::result_of<F(From const &)>::type, To>::value,
|
2013-11-21 22:10:00 +00:00
|
|
|
"map: return type of f is not equal to input type");
|
2013-09-13 01:25:38 +00:00
|
|
|
if (is_nil(l)) {
|
2014-05-18 02:18:18 +00:00
|
|
|
return list<To>();
|
2013-09-13 01:25:38 +00:00
|
|
|
} else {
|
2014-05-18 02:18:18 +00:00
|
|
|
buffer<typename list<From>::cell*> tmp;
|
2013-11-19 00:47:52 +00:00
|
|
|
to_buffer(l, tmp);
|
|
|
|
unsigned i = tmp.size();
|
2014-05-18 02:18:18 +00:00
|
|
|
list<To> r;
|
2013-11-19 00:47:52 +00:00
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
r = cons(f(tmp[i]->head()), r);
|
|
|
|
}
|
2013-12-01 16:51:24 +00:00
|
|
|
return r;
|
2013-09-13 01:25:38 +00:00
|
|
|
}
|
2013-11-18 23:29:35 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Given list <tt>(a_0, ..., a_k)</tt>, return list <tt>(f(a_0), ..., f(a_k))</tt>. */
|
2014-05-18 02:18:18 +00:00
|
|
|
template<typename T, typename F>
|
|
|
|
list<T> map(list<T> const & l, F && f) {
|
|
|
|
return map2<T, T, F>(l, std::move(f));
|
|
|
|
}
|
|
|
|
|
2013-11-30 17:45:02 +00:00
|
|
|
/**
|
|
|
|
\brief Filter/Remove elements from the list
|
2013-12-15 04:15:37 +00:00
|
|
|
that do not satisfy the given predicate.
|
2013-11-30 17:45:02 +00:00
|
|
|
*/
|
|
|
|
template<typename T, typename P>
|
|
|
|
list<T> filter(list<T> const & l, P && p) {
|
|
|
|
if (is_nil(l)) {
|
|
|
|
return l;
|
|
|
|
} else {
|
|
|
|
buffer<typename list<T>::cell*> tmp;
|
|
|
|
to_buffer(l, tmp);
|
|
|
|
unsigned i = tmp.size();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
2013-12-15 04:15:37 +00:00
|
|
|
if (!p(tmp[i]->head())) {
|
|
|
|
list<T> r = tmp[i]->tail();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
if (p(tmp[i]->head()))
|
|
|
|
r = cons(tmp[i]->head(), r);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2013-11-30 17:45:02 +00:00
|
|
|
}
|
2013-12-15 04:15:37 +00:00
|
|
|
return l; // not element was removed
|
2013-11-30 17:45:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Remove the last element that satisfies \c p. */
|
2013-12-15 07:08:10 +00:00
|
|
|
template<typename T, typename P>
|
|
|
|
list<T> remove_last(list<T> const & l, P && p) {
|
|
|
|
if (!is_nil(l)) {
|
|
|
|
buffer<typename list<T>::cell*> tmp;
|
|
|
|
to_buffer(l, tmp);
|
|
|
|
unsigned i = tmp.size();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
if (p(tmp[i]->head())) {
|
|
|
|
list<T> r = tmp[i]->tail();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
r = cons(tmp[i]->head(), r);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2013-11-21 22:47:50 +00:00
|
|
|
/**
|
|
|
|
\brief Similar to \c map but \c f has signature
|
|
|
|
|
|
|
|
<tt>bool f(T const & in, T & out)</tt>
|
|
|
|
|
|
|
|
If \c out becomes part of the result iff \c f returns true.
|
|
|
|
*/
|
|
|
|
template<typename T, typename F>
|
|
|
|
list<T> map_filter(list<T> const & l, F && f) {
|
|
|
|
if (is_nil(l)) {
|
|
|
|
return l;
|
|
|
|
} else {
|
|
|
|
buffer<typename list<T>::cell*> tmp;
|
|
|
|
to_buffer(l, tmp);
|
|
|
|
unsigned i = tmp.size();
|
|
|
|
list<T> r;
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
T out;
|
|
|
|
if (f(tmp[i]->head(), out))
|
|
|
|
r = cons(out, r);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-30 18:01:23 +00:00
|
|
|
template<typename T, typename F>
|
|
|
|
list<T> map_append(list<T> const & l, F && f) {
|
|
|
|
if (is_nil(l)) {
|
|
|
|
return l;
|
|
|
|
} else {
|
|
|
|
buffer<T> tmp;
|
|
|
|
for (auto const & v : l)
|
|
|
|
to_buffer(f(v), tmp);
|
|
|
|
return to_list(tmp.begin(), tmp.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-18 23:29:35 +00:00
|
|
|
/**
|
|
|
|
\brief Semantically equivalent to \c map, but it tries to reuse
|
|
|
|
list cells. The elements are compared using the predicate \c eq.
|
|
|
|
*/
|
|
|
|
template<typename T, typename F, typename Eq = std::equal_to<T>>
|
2013-11-21 20:48:04 +00:00
|
|
|
list<T> map_reuse(list<T> const & l, F && f, Eq const & eq = Eq()) {
|
2013-11-18 23:29:35 +00:00
|
|
|
if (is_nil(l)) {
|
|
|
|
return l;
|
|
|
|
} else {
|
2013-11-19 01:11:59 +00:00
|
|
|
buffer<typename list<T>::cell*> tmp;
|
|
|
|
to_buffer(l, tmp);
|
|
|
|
auto it = tmp.end();
|
|
|
|
auto begin = tmp.begin();
|
|
|
|
while (it != begin) {
|
|
|
|
--it;
|
|
|
|
auto curr = *it;
|
|
|
|
auto new_v = f(curr->head());
|
|
|
|
if (!eq(new_v, curr->head())) {
|
|
|
|
list<T> r(new_v, curr->tail());
|
|
|
|
while (it != begin) {
|
|
|
|
--it;
|
|
|
|
auto curr = *it;
|
|
|
|
r = cons(f(curr->head()), r);
|
2013-11-18 23:29:35 +00:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
2013-09-13 01:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Given list <tt>(a_0, ..., a_k)</tt>, exec f(a_0); f(a_1); ... f(a_k)</tt>. */
|
2013-10-01 07:18:55 +00:00
|
|
|
template<typename T, typename F>
|
2013-11-21 20:48:04 +00:00
|
|
|
void for_each(list<T> const & l, F && f) {
|
2013-10-01 16:05:53 +00:00
|
|
|
static_assert(std::is_same<typename std::result_of<F(T const &)>::type, void>::value,
|
|
|
|
"for_each: return type of f is not void");
|
2013-11-18 23:52:31 +00:00
|
|
|
typedef typename list<T>::cell cell;
|
|
|
|
cell * it = l.raw();
|
|
|
|
while (it) {
|
|
|
|
f(it->head());
|
|
|
|
it = it->tail().raw();
|
2013-10-01 07:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-19 16:00:09 +00:00
|
|
|
/** \brief Compare two lists using the binary predicate p. */
|
2013-09-13 01:25:38 +00:00
|
|
|
template<typename T, typename P>
|
2013-11-21 20:48:04 +00:00
|
|
|
bool compare(list<T> const & l1, list<T> const & l2, P && p) {
|
2013-10-01 16:05:53 +00:00
|
|
|
static_assert(std::is_same<typename std::result_of<P(T const &, T const &)>::type, bool>::value,
|
|
|
|
"compare: return type of f is not bool");
|
2013-09-13 01:25:38 +00:00
|
|
|
auto it1 = l1.begin();
|
|
|
|
auto it2 = l2.begin();
|
|
|
|
auto end1 = l1.end();
|
|
|
|
auto end2 = l2.end();
|
|
|
|
for (; it1 != end1 && it2 != end2; ++it1, ++it2) {
|
|
|
|
if (!p(*it1, *it2))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return it1 == end1 && it2 == end2;
|
|
|
|
}
|
2014-05-19 16:00:09 +00:00
|
|
|
|
|
|
|
/** \brief Return the i-th element of the list */
|
|
|
|
template<typename T>
|
|
|
|
T const & get_ith(list<T> const & l, unsigned idx) {
|
|
|
|
return idx == 0 ? head(l) : get_ith(tail(l), idx - 1);
|
|
|
|
}
|
2013-09-11 18:36:05 +00:00
|
|
|
}
|