Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/gsl/dyn_array is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 ///////////////////////////////////////////////////////////////////////////////
0003 //
0004 // Copyright (c) 2026 Microsoft Corporation. All rights reserved.
0005 //
0006 // This code is licensed under the MIT License (MIT).
0007 //
0008 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0009 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0010 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0011 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0012 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0013 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0014 // THE SOFTWARE.
0015 //
0016 ///////////////////////////////////////////////////////////////////////////////
0017 
0018 #ifndef GSL_DYN_ARRAY_H
0019 #define GSL_DYN_ARRAY_H
0020 
0021 #include "./assert"
0022 #include "./narrow"
0023 #include "./util"
0024 
0025 #include <algorithm>
0026 #include <iterator>
0027 #include <memory>
0028 #include <type_traits>
0029 #include <vector>
0030 
0031 #if defined(__cpp_lib_ranges) && (__cpp_lib_ranges >= 201911L)
0032 #include <ranges>
0033 #endif /* __cpp_lib_ranges >= 201911L */
0034 
0035 namespace gsl
0036 {
0037 template <typename T, typename Allocator = std::allocator<T>>
0038 class dyn_array;
0039 
0040 namespace details
0041 {
0042     template <typename T, typename Allocator = std::allocator<T>>
0043     class dyn_array_base : public Allocator
0044     {
0045         using pointer = T*;
0046         using size_type = std::size_t;
0047 
0048         template <typename... Args>
0049         GSL_CONSTEXPR_SINCE_CPP20 void construct(pointer ptr, Args&&... args)
0050         {
0051             std::allocator_traits<Allocator>::construct(static_cast<Allocator&>(*this), ptr,
0052                                                         std::forward<Args>(args)...);
0053         }
0054 
0055         GSL_CONSTEXPR_SINCE_CPP20 void destroy(pointer ptr)
0056         {
0057             std::allocator_traits<Allocator>::destroy(static_cast<Allocator&>(*this), ptr);
0058         }
0059 
0060         GSL_CONSTEXPR_SINCE_CPP20 void destroy_range(pointer first, pointer last)
0061         {
0062             for (; first != last; ++first) { destroy(first); }
0063         }
0064 
0065         GSL_CONSTEXPR_SINCE_CPP20 void rollback_construction(pointer first, pointer last)
0066         {
0067             destroy_range(first, last);
0068             std::allocator_traits<Allocator>::deallocate(static_cast<Allocator&>(*this), _data,
0069                                                          _count);
0070             _data = nullptr;
0071             _count = 0;
0072         }
0073 
0074     protected:
0075         constexpr auto data() const { return _data; }
0076 
0077         constexpr auto count() const { return _count; }
0078 
0079         GSL_CONSTEXPR_SINCE_CPP20 void resize(size_type count)
0080         {
0081             // This should only be called when constructing a non-forward iterator.
0082             // It neither frees nor copies `_data`.
0083             Expects(_data == nullptr && _count == 0);
0084             if (count != 0)
0085             {
0086                 _data = std::allocator_traits<Allocator>::allocate(static_cast<Allocator&>(*this),
0087                                                                    count);
0088                 _count = count;
0089             }
0090         }
0091 
0092         GSL_CONSTEXPR_SINCE_CPP20 void fill(pointer first, size_type count, const T& value)
0093         {
0094             pointer current = first;
0095             try
0096             {
0097                 for (size_type i = 0; i < count; ++i, ++current) { construct(current, value); }
0098             } catch (...)
0099             {
0100                 rollback_construction(first, current);
0101                 throw;
0102             }
0103         }
0104 
0105         template <typename InputIt>
0106         GSL_CONSTEXPR_SINCE_CPP20 void copy(InputIt first, InputIt last, pointer output)
0107         {
0108             pointer current = output;
0109             try
0110             {
0111                 for (; first != last; ++first, ++current) { construct(current, *first); }
0112             } catch (...)
0113             {
0114                 rollback_construction(output, current);
0115                 throw;
0116             }
0117         }
0118 
0119         GSL_CONSTEXPR_SINCE_CPP20 void default_construct(pointer first, size_type count)
0120         {
0121             pointer current = first;
0122             try
0123             {
0124                 for (size_type i = 0; i < count; ++i, ++current) { construct(current); }
0125             } catch (...)
0126             {
0127                 rollback_construction(first, current);
0128                 throw;
0129             }
0130         }
0131 
0132     private:
0133         pointer _data;
0134         size_type _count;
0135 
0136     public:
0137         constexpr dyn_array_base(const Allocator& alloc)
0138             : Allocator{alloc}, _data{nullptr}, _count{0}
0139         {
0140             Ensures((_count == 0 && _data == nullptr) || (_count > 0 && _data != nullptr));
0141         }
0142 
0143         constexpr dyn_array_base(size_type count, const Allocator& alloc)
0144             : Allocator{alloc}
0145             , _data{count == 0 ? nullptr
0146                                : std::allocator_traits<Allocator>::allocate(
0147                                      static_cast<Allocator&>(*this), count)}
0148             , _count{count}
0149         {
0150             Ensures((_count == 0 && _data == nullptr) || (_count > 0 && _data != nullptr));
0151         }
0152 
0153         GSL_CONSTEXPR_SINCE_CPP20 ~dyn_array_base()
0154         {
0155             if (_data)
0156             {
0157                 if (!std::is_trivially_destructible<T>::value)
0158                 {
0159                     destroy_range(_data, _data + _count);
0160                 }
0161                 std::allocator_traits<Allocator>::deallocate(static_cast<Allocator&>(*this), _data,
0162                                                              _count);
0163             }
0164         }
0165     };
0166 
0167     template <typename T>
0168     class dyn_array_iterator
0169     {
0170         using size_type = std::size_t;
0171 
0172     public:
0173         using difference_type = std::ptrdiff_t;
0174         using value_type = T;
0175         using pointer = T*;
0176         using reference = T&;
0177         using const_reference = const T&;
0178         using iterator_category = std::random_access_iterator_tag;
0179 #if defined(__cpp_lib_ranges) || (defined(_MSVC_STL_VERSION) && defined(__cpp_lib_concepts))
0180         using iterator_concept = std::contiguous_iterator_tag;
0181 #endif /* __cpp_lib_ranges || (_MSVC_STL_VERSION && __cpp_lib_concepts) */
0182 
0183         constexpr dyn_array_iterator() = default;
0184 
0185         constexpr operator dyn_array_iterator<const T>() const { return {_ptr, _pos, _end_pos}; }
0186 
0187 #if defined(_MSC_VER) && defined(__cpp_lib_ranges) && (__cpp_lib_ranges >= 201911L)
0188         constexpr operator pointer() const { return _ptr + gsl::narrow<size_type>(_pos); }
0189 #endif /* defined(_MSC_VER) && __cpp_lib_ranges >= 201911L */
0190 
0191         constexpr auto operator==(const dyn_array_iterator& other) const
0192         {
0193             Expects(_ptr == other._ptr);
0194             Expects(_end_pos == other._end_pos);
0195             return _pos == other._pos;
0196         }
0197 
0198         constexpr auto operator!=(const dyn_array_iterator& other) const
0199         {
0200             return !(*this == other);
0201         }
0202 
0203         constexpr auto operator<(const dyn_array_iterator& other) const
0204         {
0205             Expects(_ptr == other._ptr);
0206             Expects(_end_pos == other._end_pos);
0207             return _pos < other._pos;
0208         }
0209 
0210         constexpr auto operator>(const dyn_array_iterator& other) const { return other < *this; }
0211 
0212         constexpr auto operator<=(const dyn_array_iterator& other) const
0213         {
0214             return !(other < *this);
0215         }
0216 
0217         constexpr auto operator>=(const dyn_array_iterator& other) const
0218         {
0219             return !(*this < other);
0220         }
0221 
0222         constexpr auto operator*() const -> reference
0223         {
0224             Expects(_ptr != nullptr);
0225             Expects(_pos < _end_pos);
0226             return _ptr[_pos];
0227         }
0228 
0229         constexpr auto operator->() const -> pointer
0230         {
0231             Expects(_ptr != nullptr);
0232             Expects(_pos < _end_pos);
0233             return _ptr + _pos;
0234         }
0235 
0236         constexpr auto operator++() -> dyn_array_iterator&
0237         {
0238             Expects(_pos < _end_pos);
0239             ++_pos;
0240             return *this;
0241         }
0242 
0243         constexpr auto operator++(int)
0244         {
0245             ++(*this);
0246             return dyn_array_iterator{_ptr, _pos - 1, _end_pos};
0247         }
0248 
0249         constexpr auto operator--() -> dyn_array_iterator&
0250         {
0251             Expects(_pos > 0);
0252             --_pos;
0253             return *this;
0254         }
0255 
0256         constexpr auto operator--(int)
0257         {
0258             --(*this);
0259             return dyn_array_iterator{_ptr, _pos + 1, _end_pos};
0260         }
0261 
0262         constexpr auto operator+=(difference_type diff) -> dyn_array_iterator&
0263         {
0264             auto new_pos = gsl::narrow<difference_type>(_pos) + diff;
0265             Expects(new_pos >= 0);
0266             Expects(new_pos <= gsl::narrow<difference_type>(_end_pos));
0267             _pos = gsl::narrow<size_type>(new_pos);
0268             return *this;
0269         }
0270 
0271         constexpr auto operator-=(difference_type diff) -> dyn_array_iterator&
0272         {
0273             auto new_pos = gsl::narrow<difference_type>(_pos) - diff;
0274             Expects(new_pos >= 0);
0275             Expects(new_pos <= gsl::narrow<difference_type>(_end_pos));
0276             _pos = gsl::narrow<size_type>(new_pos);
0277             return *this;
0278         }
0279 
0280         constexpr auto operator+(difference_type diff) const
0281         {
0282             auto new_pos = gsl::narrow<difference_type>(_pos) + diff;
0283             return dyn_array_iterator{_ptr, gsl::narrow<size_type>(new_pos), _end_pos};
0284         }
0285 
0286         friend constexpr auto operator+(difference_type diff, const dyn_array_iterator& it)
0287         {
0288             return it + diff;
0289         }
0290 
0291         constexpr auto operator-(difference_type diff) const { return *this + (-diff); }
0292 
0293         constexpr auto operator-(const dyn_array_iterator& other) const
0294         {
0295             Expects(_ptr == other._ptr);
0296             Expects(_end_pos == other._end_pos);
0297             return gsl::narrow<difference_type>(_pos) - gsl::narrow<difference_type>(other._pos);
0298         }
0299 
0300         constexpr auto operator[](difference_type pos) const -> reference { return *(*this + pos); }
0301 
0302     private:
0303         constexpr dyn_array_iterator(pointer ptr, size_type pos, size_type end_pos)
0304             : _ptr{ptr}, _pos{pos}, _end_pos{end_pos}
0305         {
0306             Ensures((_ptr != nullptr && _end_pos > 0) || (_ptr == nullptr && _end_pos == 0));
0307             Ensures(_pos <= _end_pos);
0308         }
0309 
0310         pointer _ptr{};
0311         size_type _pos{};
0312         size_type _end_pos{};
0313 
0314         template <typename>
0315         friend class dyn_array_iterator;
0316         template <typename, typename>
0317         friend class ::gsl::dyn_array;
0318         template <typename Ptr>
0319         friend struct std::pointer_traits;
0320     };
0321 } // namespace details
0322 
0323 template <typename T, typename Allocator>
0324 class dyn_array : private details::dyn_array_base<T, Allocator>
0325 {
0326     using base = details::dyn_array_base<T, Allocator>;
0327     using pointer = T*;
0328 
0329 public:
0330     using value_type = T;
0331     using reference = T&;
0332     using const_reference = const T&;
0333     using iterator = details::dyn_array_iterator<T>;
0334     using const_iterator = details::dyn_array_iterator<const T>;
0335     using reverse_iterator = std::reverse_iterator<iterator>;
0336     using const_reverse_iterator = std::reverse_iterator<const_iterator>;
0337     using difference_type = std::ptrdiff_t;
0338     using size_type = std::size_t;
0339 
0340     using allocator_type = Allocator;
0341 
0342     explicit constexpr dyn_array(const Allocator& alloc = {}) : base{alloc} {}
0343 
0344     constexpr dyn_array(size_type count, const T& value, const Allocator& alloc = {})
0345         : base{count, alloc}
0346     {
0347         base::fill(data(), size(), value);
0348     }
0349 
0350     template <typename InputIt,
0351               std::enable_if_t<details::is_fwd_iterator<InputIt>::value, bool> = true>
0352     constexpr dyn_array(InputIt first, InputIt last, const Allocator& alloc = {})
0353         : base{gsl::narrow<size_type>(std::distance(first, last)), alloc}
0354     {
0355         base::copy(first, last, data());
0356     }
0357 
0358     template <typename InputIt, std::enable_if_t<!details::is_fwd_iterator<InputIt>::value &&
0359                                                      details::is_iterator<InputIt>::value,
0360                                                  bool> = true>
0361     constexpr dyn_array(InputIt first, InputIt last, const Allocator& alloc = {}) : dyn_array{alloc}
0362     {
0363         std::vector<T> tmp(first, last);
0364         base::resize(tmp.size());
0365         base::copy(std::begin(tmp), std::end(tmp), data());
0366     }
0367 
0368 #if defined(__cpp_lib_containers_ranges) && (__cpp_lib_containers_ranges >= 202202L)
0369     template <typename InputRg>
0370         requires(std::ranges::input_range<InputRg>)
0371     constexpr dyn_array(std::from_range_t, InputRg&& rg, const Allocator& alloc = {})
0372         : base{gsl::narrow<size_type>(std::size(rg)), alloc}
0373     {
0374         base::copy(std::ranges::begin(rg), std::ranges::end(rg), data());
0375     }
0376 #endif /* __cpp_lib_containers_ranges >= 202202L */
0377 
0378     constexpr explicit dyn_array(size_type count, const Allocator& alloc = {}) : base{count, alloc}
0379     {
0380         base::default_construct(data(), size());
0381     }
0382 
0383     constexpr dyn_array(const dyn_array& other, const Allocator& alloc = {})
0384         : dyn_array(other.begin(), other.end(), alloc)
0385     {}
0386 
0387     constexpr dyn_array(std::initializer_list<T> init, const Allocator& alloc = {})
0388         : dyn_array(init.begin(), init.end(), alloc)
0389     {}
0390 
0391     constexpr dyn_array(dyn_array&&) = delete;
0392     dyn_array& operator=(dyn_array&&) = delete;
0393 
0394     constexpr auto operator==(const dyn_array& other) const
0395     {
0396         return size() == other.size() && std::equal(begin(), end(), other.begin(), other.end());
0397     }
0398 
0399     constexpr auto operator!=(const dyn_array& other) const { return !(*this == other); }
0400 
0401     constexpr auto size() const { return base::count(); }
0402 
0403     constexpr auto empty() const { return size() == 0; }
0404 
0405     constexpr auto max_size() const { return static_cast<size_type>(-1); }
0406 
0407     constexpr auto get_allocator() -> Allocator& { return *this; }
0408 
0409     constexpr auto operator[](size_type pos) -> reference
0410     {
0411         Expects(pos < size());
0412         return data()[pos];
0413     }
0414 
0415     constexpr auto operator[](size_type pos) const -> const_reference
0416     {
0417         return const_cast<dyn_array&>(*this)[pos];
0418     }
0419 
0420     constexpr auto data() { return base::data(); }
0421     constexpr auto data() const -> const T* { return const_cast<dyn_array&>(*this).data(); }
0422 
0423     constexpr auto begin() { return iterator{data(), 0, size()}; }
0424     constexpr auto begin() const { return const_iterator{data(), 0, size()}; }
0425     constexpr auto cbegin() const { return begin(); }
0426 
0427     constexpr auto rbegin() { return reverse_iterator{end()}; }
0428     constexpr auto rbegin() const { return const_reverse_iterator{end()}; }
0429     constexpr auto crbegin() const { return rbegin(); }
0430 
0431 #ifdef _MSC_VER
0432     constexpr auto _Unchecked_begin() { return data(); }
0433     constexpr auto _Unchecked_begin() const -> const T*
0434     {
0435         return const_cast<dyn_array&>(*this)._Unchecked_begin();
0436     }
0437 #endif /* _MSC_VER */
0438 
0439     constexpr auto end() { return iterator{data(), size(), size()}; }
0440     constexpr auto end() const { return const_iterator{data(), size(), size()}; }
0441     constexpr auto cend() const { return end(); }
0442 
0443     constexpr auto rend() { return reverse_iterator{begin()}; }
0444     constexpr auto rend() const { return const_reverse_iterator{begin()}; }
0445     constexpr auto crend() const { return rend(); }
0446 
0447 #ifdef _MSC_VER
0448     constexpr auto _Unchecked_end() { return data() + size(); }
0449     constexpr auto _Unchecked_end() const -> const T*
0450     {
0451         return const_cast<dyn_array&>(*this)._Unchecked_end();
0452     }
0453 #endif /* _MSC_VER */
0454 };
0455 
0456 #if defined(__cpp_deduction_guides) && (__cpp_deduction_guides >= 201703L)
0457 
0458 template <class InputIt,
0459           class Alloc = std::allocator<typename std::iterator_traits<InputIt>::value_type>>
0460 dyn_array(InputIt, InputIt, Alloc = {})
0461     -> dyn_array<typename std::iterator_traits<InputIt>::value_type, Alloc>;
0462 
0463 #if defined(__cpp_lib_containers_ranges) && (__cpp_lib_containers_ranges >= 202202L)
0464 template <std::ranges::input_range InputRg,
0465           class Alloc = std::allocator<std::ranges::range_value_t<InputRg>>>
0466 dyn_array(std::from_range_t, InputRg&&, Alloc = {})
0467     -> dyn_array<std::ranges::range_value_t<InputRg>, Alloc>;
0468 #endif /* __cpp_lib_containers_ranges >= 202202L */
0469 
0470 #endif /* __cpp_deduction_guides >= 201703L */
0471 } // namespace gsl
0472 
0473 namespace std
0474 {
0475 template <class Type>
0476 struct pointer_traits<::gsl::details::dyn_array_iterator<Type>>
0477 {
0478     using pointer = ::gsl::details::dyn_array_iterator<Type>;
0479     using element_type = Type;
0480     using difference_type = ptrdiff_t;
0481 
0482     static constexpr element_type* to_address(const pointer i) noexcept { return i._ptr + i._pos; }
0483 };
0484 } // namespace std
0485 
0486 #endif /* defined(GSL_DYN_ARRAY_H) */