Skip to content

Commit 04180bf

Browse files
fkgozalifacebook-github-bot
authored andcommitted
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
1 parent 1e0c30c commit 04180bf

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-0
lines changed

Libraries/FBLazyVector/BUCK

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load("//tools/build_defs/oss:rn_defs.bzl", "fb_apple_library")
2+
3+
fb_apple_library(
4+
name = "FBLazyVector",
5+
autoglob = True,
6+
contacts = ["oncall+react_native@xmail.facebook.com"],
7+
enable_exceptions = False,
8+
frameworks = [],
9+
link_whole = False,
10+
visibility = ["PUBLIC"],
11+
)
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// Copyright 2004-present Facebook. All Rights Reserved.
2+
3+
#pragma once
4+
5+
#import <functional>
6+
#import <iterator>
7+
8+
namespace FB {
9+
10+
template <typename T, typename U>
11+
class LazyIterator {
12+
public:
13+
using value_type = T;
14+
using pointer = std::unique_ptr<T>;
15+
using reference = T;
16+
using iterator_category = std::random_access_iterator_tag;
17+
using difference_type = std::int32_t;
18+
using size_type = std::int32_t;
19+
using convert_type = std::function<T(U)>;
20+
21+
public:
22+
LazyIterator() = default;
23+
24+
LazyIterator(U vector, convert_type convert, size_type i)
25+
: _v(vector), _i(i), _convert(std::move(convert)) {}
26+
27+
bool operator==(const LazyIterator &other) const {
28+
return _i == other._i && _v == other._v;
29+
}
30+
31+
bool operator<(const LazyIterator &b) const {
32+
return _i < b._i;
33+
}
34+
35+
value_type operator*() const {
36+
return _convert(_v[_i]);
37+
}
38+
39+
std::unique_ptr<value_type> operator->() const {
40+
return std::make_unique<value_type>(*this);
41+
}
42+
43+
LazyIterator operator+(difference_type n) const {
44+
return LazyIterator(_v, _convert, _i + n);
45+
}
46+
47+
LazyIterator &operator+=(difference_type n) {
48+
_i += n;
49+
return *this;
50+
}
51+
52+
LazyIterator &operator-=(difference_type n) {
53+
_i -= n;
54+
return *this;
55+
}
56+
57+
LazyIterator operator-(difference_type n) const {
58+
return LazyIterator(_v, _i - n);
59+
}
60+
61+
difference_type operator-(const LazyIterator &a) const {
62+
return _i - a._i;
63+
}
64+
65+
LazyIterator &operator++() {
66+
return *this += 1;
67+
}
68+
69+
LazyIterator operator++(int) {
70+
auto tmp = *this;
71+
++*this;
72+
return tmp;
73+
}
74+
75+
LazyIterator &operator--() {
76+
return *this -= 1;
77+
}
78+
79+
LazyIterator operator--(int) {
80+
auto tmp = *this;
81+
--*this;
82+
return tmp;
83+
}
84+
85+
value_type operator[](difference_type n) const {
86+
return _convert(_v[_i + n]);
87+
}
88+
89+
private:
90+
U _v;
91+
size_type _i;
92+
convert_type _convert;
93+
};
94+
95+
template <typename T, typename U>
96+
LazyIterator<T, U> operator+(typename LazyIterator<T, U>::difference_type n,
97+
const LazyIterator<T, U> &i) {
98+
return i + n;
99+
}
100+
101+
template <typename T, typename U>
102+
bool operator!=(const LazyIterator<T, U> &a,
103+
const LazyIterator<T, U> &b) {
104+
return !(a == b);
105+
}
106+
107+
template <typename T, typename U>
108+
bool operator<=(const LazyIterator<T, U> &a,
109+
const LazyIterator<T, U> &b) {
110+
return a < b || a == b;
111+
}
112+
113+
template <typename T, typename U>
114+
bool operator>(const LazyIterator<T, U> &a,
115+
const LazyIterator<T, U> &b) {
116+
return b < a;
117+
}
118+
119+
template <typename T, typename U>
120+
bool operator>=(const LazyIterator<T, U> &a,
121+
const LazyIterator<T, U> &b) {
122+
return a > b || a == b;
123+
}
124+
125+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#import <cassert>
11+
#import <functional>
12+
#import <iterator>
13+
14+
#import <FBLazyVector/FBLazyIterator.h>
15+
16+
namespace FB {
17+
18+
/**
19+
* Presents a type-safe wrapper around an arbitrary object that represents an _immutable_ array of objects.
20+
* Each item is constructed lazily on demand and reconstructed on each access; there is no caching.
21+
*/
22+
template <typename T, typename U>
23+
class LazyVector {
24+
public:
25+
using value_type = T;
26+
using reference = T;
27+
using const_reference = T;
28+
using const_iterator = LazyIterator<T, U>;
29+
using iterator = const_iterator;
30+
using size_type = std::int32_t;
31+
using convert_type = std::function<T(U)>;
32+
33+
static LazyVector<T, U> fromUnsafeRawValue(U v, size_type size, convert_type convert) {
34+
return {v, size, convert};
35+
}
36+
37+
U unsafeRawValue() const {
38+
return _v;
39+
}
40+
41+
bool empty() const {
42+
return _size == 0;
43+
}
44+
45+
size_type size() const {
46+
return _size;
47+
}
48+
49+
const_reference at(size_type pos) const {
50+
#ifndef _LIBCPP_NO_EXCEPTIONS
51+
if (!(pos < _size)) throw std::out_of_range("out of range");
52+
#else
53+
assert(pos < _size || !"out of range");
54+
#endif
55+
return _convert(_v[pos]);
56+
}
57+
58+
const_reference operator[](size_type pos) const {
59+
assert(pos < _size);
60+
return _convert(_v[pos]);
61+
}
62+
63+
const_reference front() const {
64+
assert(_size);
65+
return (*this)[0];
66+
}
67+
68+
const_reference back() const {
69+
assert(_size);
70+
return (*this)[_size - 1];
71+
}
72+
73+
const_iterator begin() const {
74+
return const_iterator(_v, _convert, 0);
75+
}
76+
77+
const_iterator cbegin() const {
78+
return begin();
79+
}
80+
81+
const_iterator end() const {
82+
return const_iterator(_v, _convert, _size);
83+
}
84+
85+
const_iterator cend() const {
86+
return end();
87+
}
88+
89+
private:
90+
/** Wrapped vector */
91+
LazyVector(U vector, size_type size, convert_type convert)
92+
: _v(vector), _size(size), _convert(convert) {}
93+
94+
U _v;
95+
size_type _size;
96+
convert_type _convert;
97+
};
98+
99+
}

0 commit comments

Comments
 (0)