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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::borrow::Borrow;

use crate::{
    array::{
        MutableArray, MutableBinaryArray, MutableBinaryValuesArray, MutableBooleanArray,
        MutableFixedSizeBinaryArray, MutablePrimitiveArray, MutableUtf8Array,
        MutableUtf8ValuesArray,
    },
    offset::Offset,
    types::NativeType,
};

/// Trait for arrays that can be indexed directly to extract a value.
pub trait Indexable {
    /// The type of the element at index `i`; may be a reference type or a value type.
    type Value<'a>: Borrow<Self::Type>
    where
        Self: 'a;

    type Type: ?Sized;

    /// Returns the element at index `i`.
    /// # Panic
    /// May panic if `i >= self.len()`.
    fn value_at(&self, index: usize) -> Self::Value<'_>;

    /// Returns the element at index `i`.
    /// # Safety
    /// Assumes that the `i < self.len`.
    #[inline]
    unsafe fn value_unchecked_at(&self, index: usize) -> Self::Value<'_> {
        self.value_at(index)
    }
}

pub trait AsIndexed<M: Indexable> {
    fn as_indexed(&self) -> &M::Type;
}

impl Indexable for MutableBooleanArray {
    type Value<'a> = bool;
    type Type = bool;

    #[inline]
    fn value_at(&self, i: usize) -> Self::Value<'_> {
        self.values().get(i)
    }
}

impl AsIndexed<MutableBooleanArray> for bool {
    #[inline]
    fn as_indexed(&self) -> &bool {
        self
    }
}

impl<O: Offset> Indexable for MutableBinaryArray<O> {
    type Value<'a> = &'a [u8];
    type Type = [u8];

    #[inline]
    fn value_at(&self, i: usize) -> Self::Value<'_> {
        // TODO: add .value() / .value_unchecked() to MutableBinaryArray?
        assert!(i < self.len());
        unsafe { self.value_unchecked_at(i) }
    }

    #[inline]
    unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {
        // TODO: add .value() / .value_unchecked() to MutableBinaryArray?
        // soundness: the invariant of the function
        let (start, end) = self.offsets().start_end_unchecked(i);
        // soundness: the invariant of the struct
        self.values().get_unchecked(start..end)
    }
}

impl<O: Offset> AsIndexed<MutableBinaryArray<O>> for &[u8] {
    #[inline]
    fn as_indexed(&self) -> &[u8] {
        self
    }
}

impl<O: Offset> Indexable for MutableBinaryValuesArray<O> {
    type Value<'a> = &'a [u8];
    type Type = [u8];

    #[inline]
    fn value_at(&self, i: usize) -> Self::Value<'_> {
        self.value(i)
    }

    #[inline]
    unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {
        self.value_unchecked(i)
    }
}

impl<O: Offset> AsIndexed<MutableBinaryValuesArray<O>> for &[u8] {
    #[inline]
    fn as_indexed(&self) -> &[u8] {
        self
    }
}

impl Indexable for MutableFixedSizeBinaryArray {
    type Value<'a> = &'a [u8];
    type Type = [u8];

    #[inline]
    fn value_at(&self, i: usize) -> Self::Value<'_> {
        self.value(i)
    }

    #[inline]
    unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {
        // soundness: the invariant of the struct
        self.value_unchecked(i)
    }
}

impl AsIndexed<MutableFixedSizeBinaryArray> for &[u8] {
    #[inline]
    fn as_indexed(&self) -> &[u8] {
        self
    }
}

// TODO: should NativeType derive from Hash?
impl<T: NativeType> Indexable for MutablePrimitiveArray<T> {
    type Value<'a> = T;
    type Type = T;

    #[inline]
    fn value_at(&self, i: usize) -> Self::Value<'_> {
        assert!(i < self.len());
        // TODO: add Length trait? (for both Array and MutableArray)
        unsafe { self.value_unchecked_at(i) }
    }

    #[inline]
    unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {
        *self.values().get_unchecked(i)
    }
}

impl<T: NativeType> AsIndexed<MutablePrimitiveArray<T>> for T {
    #[inline]
    fn as_indexed(&self) -> &T {
        self
    }
}

impl<O: Offset> Indexable for MutableUtf8Array<O> {
    type Value<'a> = &'a str;
    type Type = str;

    #[inline]
    fn value_at(&self, i: usize) -> Self::Value<'_> {
        self.value(i)
    }

    #[inline]
    unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {
        self.value_unchecked(i)
    }
}

impl<O: Offset, V: AsRef<str>> AsIndexed<MutableUtf8Array<O>> for V {
    #[inline]
    fn as_indexed(&self) -> &str {
        self.as_ref()
    }
}

impl<O: Offset> Indexable for MutableUtf8ValuesArray<O> {
    type Value<'a> = &'a str;
    type Type = str;

    #[inline]
    fn value_at(&self, i: usize) -> Self::Value<'_> {
        self.value(i)
    }

    #[inline]
    unsafe fn value_unchecked_at(&self, i: usize) -> Self::Value<'_> {
        self.value_unchecked(i)
    }
}

impl<O: Offset, V: AsRef<str>> AsIndexed<MutableUtf8ValuesArray<O>> for V {
    #[inline]
    fn as_indexed(&self) -> &str {
        self.as_ref()
    }
}