From 04180bf8ffe62d77ee2aee2d2c513aa9ff42e408 Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Fri, 21 Jun 2019 11:36:20 -0700 Subject: [PATCH] Move FBLazyVector to github Summary: FBLazyVector is a simple utility to help typesafety/codegen of TurboModule specs. This is not used widely elsewhere at the moment. Reviewed By: hramos, cpojer Differential Revision: D15929956 fbshipit-source-id: 17226351738335a74e7b931812a1ca901f47963f --- Libraries/FBLazyVector/BUCK | 11 ++ .../FBLazyVector/FBLazyIterator.h | 125 ++++++++++++++++++ .../FBLazyVector/FBLazyVector/FBLazyVector.h | 99 ++++++++++++++ 3 files changed, 235 insertions(+) create mode 100644 Libraries/FBLazyVector/BUCK create mode 100644 Libraries/FBLazyVector/FBLazyVector/FBLazyIterator.h create mode 100644 Libraries/FBLazyVector/FBLazyVector/FBLazyVector.h diff --git a/Libraries/FBLazyVector/BUCK b/Libraries/FBLazyVector/BUCK new file mode 100644 index 00000000000..7989eb160bc --- /dev/null +++ b/Libraries/FBLazyVector/BUCK @@ -0,0 +1,11 @@ +load("//tools/build_defs/oss:rn_defs.bzl", "fb_apple_library") + +fb_apple_library( + name = "FBLazyVector", + autoglob = True, + contacts = ["oncall+react_native@xmail.facebook.com"], + enable_exceptions = False, + frameworks = [], + link_whole = False, + visibility = ["PUBLIC"], +) diff --git a/Libraries/FBLazyVector/FBLazyVector/FBLazyIterator.h b/Libraries/FBLazyVector/FBLazyVector/FBLazyIterator.h new file mode 100644 index 00000000000..b0968282a77 --- /dev/null +++ b/Libraries/FBLazyVector/FBLazyVector/FBLazyIterator.h @@ -0,0 +1,125 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#pragma once + +#import +#import + +namespace FB { + +template +class LazyIterator { + public: + using value_type = T; + using pointer = std::unique_ptr; + using reference = T; + using iterator_category = std::random_access_iterator_tag; + using difference_type = std::int32_t; + using size_type = std::int32_t; + using convert_type = std::function; + + public: + LazyIterator() = default; + + LazyIterator(U vector, convert_type convert, size_type i) + : _v(vector), _i(i), _convert(std::move(convert)) {} + + bool operator==(const LazyIterator &other) const { + return _i == other._i && _v == other._v; + } + + bool operator<(const LazyIterator &b) const { + return _i < b._i; + } + + value_type operator*() const { + return _convert(_v[_i]); + } + + std::unique_ptr operator->() const { + return std::make_unique(*this); + } + + LazyIterator operator+(difference_type n) const { + return LazyIterator(_v, _convert, _i + n); + } + + LazyIterator &operator+=(difference_type n) { + _i += n; + return *this; + } + + LazyIterator &operator-=(difference_type n) { + _i -= n; + return *this; + } + + LazyIterator operator-(difference_type n) const { + return LazyIterator(_v, _i - n); + } + + difference_type operator-(const LazyIterator &a) const { + return _i - a._i; + } + + LazyIterator &operator++() { + return *this += 1; + } + + LazyIterator operator++(int) { + auto tmp = *this; + ++*this; + return tmp; + } + + LazyIterator &operator--() { + return *this -= 1; + } + + LazyIterator operator--(int) { + auto tmp = *this; + --*this; + return tmp; + } + + value_type operator[](difference_type n) const { + return _convert(_v[_i + n]); + } + + private: + U _v; + size_type _i; + convert_type _convert; +}; + +template +LazyIterator operator+(typename LazyIterator::difference_type n, + const LazyIterator &i) { + return i + n; +} + +template +bool operator!=(const LazyIterator &a, + const LazyIterator &b) { + return !(a == b); +} + +template +bool operator<=(const LazyIterator &a, + const LazyIterator &b) { + return a < b || a == b; +} + +template +bool operator>(const LazyIterator &a, + const LazyIterator &b) { + return b < a; +} + +template +bool operator>=(const LazyIterator &a, + const LazyIterator &b) { + return a > b || a == b; +} + +} diff --git a/Libraries/FBLazyVector/FBLazyVector/FBLazyVector.h b/Libraries/FBLazyVector/FBLazyVector/FBLazyVector.h new file mode 100644 index 00000000000..e1c705576ee --- /dev/null +++ b/Libraries/FBLazyVector/FBLazyVector/FBLazyVector.h @@ -0,0 +1,99 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#import +#import +#import + +#import + +namespace FB { + +/** + * Presents a type-safe wrapper around an arbitrary object that represents an _immutable_ array of objects. + * Each item is constructed lazily on demand and reconstructed on each access; there is no caching. + */ +template +class LazyVector { + public: + using value_type = T; + using reference = T; + using const_reference = T; + using const_iterator = LazyIterator; + using iterator = const_iterator; + using size_type = std::int32_t; + using convert_type = std::function; + + static LazyVector fromUnsafeRawValue(U v, size_type size, convert_type convert) { + return {v, size, convert}; + } + + U unsafeRawValue() const { + return _v; + } + + bool empty() const { + return _size == 0; + } + + size_type size() const { + return _size; + } + + const_reference at(size_type pos) const { +#ifndef _LIBCPP_NO_EXCEPTIONS + if (!(pos < _size)) throw std::out_of_range("out of range"); +#else + assert(pos < _size || !"out of range"); +#endif + return _convert(_v[pos]); + } + + const_reference operator[](size_type pos) const { + assert(pos < _size); + return _convert(_v[pos]); + } + + const_reference front() const { + assert(_size); + return (*this)[0]; + } + + const_reference back() const { + assert(_size); + return (*this)[_size - 1]; + } + + const_iterator begin() const { + return const_iterator(_v, _convert, 0); + } + + const_iterator cbegin() const { + return begin(); + } + + const_iterator end() const { + return const_iterator(_v, _convert, _size); + } + + const_iterator cend() const { + return end(); + } + + private: + /** Wrapped vector */ + LazyVector(U vector, size_type size, convert_type convert) + : _v(vector), _size(size), _convert(convert) {} + + U _v; + size_type _size; + convert_type _convert; +}; + +}