Skip to content

Instantly share code, notes, and snippets.

@peti
Last active February 7, 2020 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peti/bfbc01ea4a035e4566dccf6b3119b34a to your computer and use it in GitHub Desktop.
Save peti/bfbc01ea4a035e4566dccf6b3119b34a to your computer and use it in GitHub Desktop.
Experiment with bazel
# ignore generated files
/bazel-bazel-test
/bazel-bin
/bazel-out
/bazel-testlogs
/WORKSPACE
# BUILD
cc_binary(
name = "powerset",
srcs = ["powerset.cpp"],
deps = ["@boost//:archive"]
)
/*
* Copyright (c) 2013 Peter Simons <simons@cryp.to>
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the
* use of this software.
*
* Copying and distribution of this file, with or without modification, are
* permitted in any medium without royalty provided the copyright notice and
* this notice are preserved.
*/
#include <iostream>
#include <boost/array.hpp>
#include <boost/assert.hpp>
#include <boost/cstdint.hpp>
namespace boost
{
template <class T, std::size_t N>
inline std::ostream & operator<< (std::ostream & os, boost::array<T, N> const & arr)
{
std::copy(arr.begin(), arr.end(), std::ostream_iterator<T>(os, " "));
return os;
}
}
template <class IteratorType, class SizeType = uintmax_t>
class powerset
{
public:
typedef IteratorType iterator;
typedef SizeType size_type;
powerset(iterator const & begin, iterator const & end) : _begin(begin), _end(end), _max_state(1u << std::distance(begin, end)), _state(0u)
{
BOOST_ASSERT(_max_state >= 0);
}
size_type size() const { _max_state; }
bool more() const { return _state < _max_state; }
bool empty() const { return _state == _max_state; }
template <class OutputIterator>
void write_to(OutputIterator oi)
{
iterator i(_begin);
for (size_type state(_state); state != 0; state>>=1, ++i)
{
if (state & 1) *oi++ = *i;
}
}
void next() { ++_state; }
private:
iterator _begin, _end;
size_type _max_state;
size_type _state;
};
int main(int, char * [])
{
typedef char value_type;
typedef boost::array<value_type, 5u> array_type;
typedef powerset<array_type::const_iterator> powerset_type;
array_type const arr = {{ 'a', 'b', 'c', 'd', 'e' }};
for (powerset_type pset(arr.begin(), arr.end()); pset.more(); pset.next())
{
pset.write_to(std::ostream_iterator<value_type>(std::cout, " "));
std::cout << std::endl;
}
return 0;
}
# WORKSPACE
#
# Based on code posted in https://github.com/bazelbuild/bazel/issues/8846.
#
new_local_repository(
name = "boost",
path = "/usr/include",
build_file_content = """
package(default_visibility = ["//visibility:public"])
cc_library(
name = "devel",
hdrs = glob(["boost/**/*.hpp"])
)
"""
)
# WORKSPACE
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
git_repository(
name = "com_github_nelhage_rules_boost",
commit = "9f9fb8b2f0213989247c9d5c0e814a8451d18d7f",
remote = "https://github.com/nelhage/rules_boost",
shallow_since = "1570056263 -0700",
)
load("@com_github_nelhage_rules_boost//:boost/boost.bzl", "boost_deps")
boost_deps()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment