summaryrefslogtreecommitdiff
path: root/fuzz/span.h
blob: de144efe603d355e78f8a7d98e5a69dbad1a24b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef __FUZZ_SPAN_H
#define __FUZZ_SPAN_H

#include <algorithm>
#include <unistd.h>

namespace fuzz
{

template <typename T> class span {
    public:
	typedef T value_type;

	constexpr span()
		: span<T>(nullptr, nullptr)
	{
	}
	constexpr span(T *begin, size_t size)
		: begin_(begin)
		, end_(begin + size)
	{
	}
	constexpr span(T *begin, T *end)
		: begin_(begin)
		, end_(end)
	{
	}

	template <class Container>
	constexpr span(Container &container)
		: begin_(container.begin())
		, end_(container.end()){};

	constexpr T *begin() const
	{
		return begin_;
	}
	constexpr T *end() const
	{
		return end_;
	}

	constexpr T *data() const
	{
		return begin_;
	}

	constexpr bool empty() const
	{
		return begin_ == end_;
	}
	constexpr size_t size() const
	{
		return end_ - begin_;
	}

    private:
	T *begin_;
	T *end_;
};

template <typename Source, typename Destination>
size_t CopyWithPadding(Source source, Destination destination,
		       typename Destination::value_type fill_value)
{
	if (source.size() >= destination.size()) {
		std::copy(source.begin(), source.begin() + destination.size(),
			  destination.begin());
		return destination.size();
	}
	std::copy(source.begin(), source.end(), destination.begin());
	std::fill(destination.begin() + source.size(), destination.end(),
		  fill_value);
	return source.size();
}

} // namespace fuzz

#endif // __FUZZ_SPAN_H