geoarrow_array/builder/coord/
combined.rs1use core::f64;
2
3use geo_traits::{CoordTrait, PointTrait};
4use geoarrow_schema::error::GeoArrowResult;
5use geoarrow_schema::{CoordType, Dimension};
6
7use crate::array::CoordBuffer;
8use crate::builder::{InterleavedCoordBufferBuilder, SeparatedCoordBufferBuilder};
9
10#[derive(Debug, Clone)]
14pub enum CoordBufferBuilder {
15 Interleaved(InterleavedCoordBufferBuilder),
17 Separated(SeparatedCoordBufferBuilder),
19}
20
21impl CoordBufferBuilder {
22 pub fn initialize(len: usize, coord_type: CoordType, dim: Dimension, value: f64) -> Self {
24 match coord_type {
25 CoordType::Interleaved => CoordBufferBuilder::Interleaved(
26 InterleavedCoordBufferBuilder::initialize(len, dim, value),
27 ),
28 CoordType::Separated => CoordBufferBuilder::Separated(
29 SeparatedCoordBufferBuilder::initialize(len, dim, value),
30 ),
31 }
32 }
33
34 pub fn with_capacity(len: usize, coord_type: CoordType, dim: Dimension) -> Self {
36 match coord_type {
37 CoordType::Interleaved => CoordBufferBuilder::Interleaved(
38 InterleavedCoordBufferBuilder::with_capacity(len, dim),
39 ),
40 CoordType::Separated => {
41 CoordBufferBuilder::Separated(SeparatedCoordBufferBuilder::with_capacity(len, dim))
42 }
43 }
44 }
45
46 pub fn reserve(&mut self, additional: usize) {
52 match self {
53 CoordBufferBuilder::Interleaved(cb) => cb.reserve(additional),
54 CoordBufferBuilder::Separated(cb) => cb.reserve(additional),
55 }
56 }
57
58 pub fn reserve_exact(&mut self, additional: usize) {
70 match self {
71 CoordBufferBuilder::Interleaved(cb) => cb.reserve_exact(additional),
72 CoordBufferBuilder::Separated(cb) => cb.reserve_exact(additional),
73 }
74 }
75
76 pub fn shrink_to_fit(&mut self) {
78 match self {
79 CoordBufferBuilder::Interleaved(cb) => cb.shrink_to_fit(),
80 CoordBufferBuilder::Separated(cb) => cb.shrink_to_fit(),
81 }
82 }
83
84 pub fn capacity(&self) -> usize {
86 match self {
87 CoordBufferBuilder::Interleaved(cb) => cb.capacity(),
88 CoordBufferBuilder::Separated(cb) => cb.capacity(),
89 }
90 }
91
92 pub fn len(&self) -> usize {
94 match self {
95 CoordBufferBuilder::Interleaved(cb) => cb.len(),
96 CoordBufferBuilder::Separated(cb) => cb.len(),
97 }
98 }
99
100 pub fn is_empty(&self) -> bool {
102 self.len() == 0
103 }
104
105 pub fn coord_type(&self) -> CoordType {
107 match self {
108 CoordBufferBuilder::Interleaved(_) => CoordType::Interleaved,
109 CoordBufferBuilder::Separated(_) => CoordType::Separated,
110 }
111 }
112
113 pub fn push_coord(&mut self, coord: &impl CoordTrait<T = f64>) {
119 match self {
120 CoordBufferBuilder::Interleaved(cb) => cb.push_coord(coord),
121 CoordBufferBuilder::Separated(cb) => cb.push_coord(coord),
122 }
123 }
124
125 pub fn try_push_coord(&mut self, coord: &impl CoordTrait<T = f64>) -> GeoArrowResult<()> {
131 match self {
132 CoordBufferBuilder::Interleaved(cb) => cb.try_push_coord(coord),
133 CoordBufferBuilder::Separated(cb) => cb.try_push_coord(coord),
134 }
135 }
136
137 pub(crate) fn push_constant(&mut self, value: f64) {
142 match self {
143 CoordBufferBuilder::Interleaved(cb) => cb.push_constant(value),
144 CoordBufferBuilder::Separated(cb) => cb.push_constant(value),
145 }
146 }
147
148 pub fn push_point(&mut self, point: &impl PointTrait<T = f64>) {
154 match self {
155 CoordBufferBuilder::Interleaved(cb) => cb.push_point(point),
156 CoordBufferBuilder::Separated(cb) => cb.push_point(point),
157 }
158 }
159
160 pub fn try_push_point(&mut self, point: &impl PointTrait<T = f64>) -> GeoArrowResult<()> {
166 match self {
167 CoordBufferBuilder::Interleaved(cb) => cb.try_push_point(point),
168 CoordBufferBuilder::Separated(cb) => cb.try_push_point(point),
169 }
170 }
171
172 pub fn finish(self) -> CoordBuffer {
174 match self {
175 CoordBufferBuilder::Interleaved(cb) => CoordBuffer::Interleaved(cb.finish()),
176 CoordBufferBuilder::Separated(cb) => CoordBuffer::Separated(cb.finish()),
177 }
178 }
179}