Skip to content

snsinfu/cxx-array_view

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

array_view - non-owning range view of array

C++11 Build Status Boost License

This repository contains a single-file, header-only library of array_view class template. It is a non-owning view of an array similar to GSL's span but uses size_t for indexing.

#include <vector>
#include <array_view.hpp>

int main()
{
    std::vector<int> vector(100);
    cxx::array_view<int> view = vector;
    view[0] = 42;
}

Installation

This library has no dependency and single header-only. Just download array_view.hpp into your include directory and #include it.

curl -LO https://raw.githubusercontent.com/snsinfu/cxx-array_view/master/include/array_view.hpp

Reference

cxx::array_view<T> constructors:

array_view() noexcept;
array_view(array_view& other) noexcept;
array_view(array_view const& other) noexcept;
array_view(T* data, size_t size);
array_view(Container& container) noexcept;

cxx::array_view<T> member functions:

bool             empty() const noexcept;
size_t           size() const noexcept;
T*               data() const noexcept;

T&               front() const;
T&               back() const;
T&               operator[](size_t index) const;
T&               at(size_t index) const;

iterator         begin() const noexcept;
iterator         end() const noexcept;
reverse_iterator rbegin() const noexcept;
reverse_iterator rend() const noexcept;

const_array_view as_const() const noexcept
void             swap(array_view& other) noexcept;

array_view       subview(size_t start, size_t size) const;
array_view       first(size_t size) const;
array_view       last(size_t size) const;

Index type

array_view uses size_t for indexing. This is for better compatibility with existing libraries. I'm actually happy with signed index in languages like Go, but in C++ there are too many existing code that use size_t for indexing. So I stick with unsigned index until the situation changes.

See:

Testing

git clone https://github.com/snsinfu/cxx-array_view
cd cxx-array_view/tests
make

License

Boost Software License, Version 1.0.

Releases

No releases published

Packages

No packages published