Skip to content

Commit

Permalink
feat: Add iteration utilities for iterating over native arrays (#111)
Browse files Browse the repository at this point in the history
This PR adds minimal STL iterators that can be used to iterate over
GeoArrow memory. The idea is to allow forward iteration from C++ (as
opposed to visitor-style iteration from C or C++). An example from the
tests:

```c
    geoarrow::ArrayReader reader(type);
    reader.SetArray(&array);

    geoarrow::array::MultiPolygonArray<XY> native_array;
    ASSERT_EQ(native_array.Init(reader.View().array_view()), GEOARROW_OK);

    std::vector<std::vector<std::vector<std::vector<XY>>>> multipolygons;
    for (const auto& multipolygon : native_array.value) {
      std::vector<std::vector<std::vector<XY>>> polygons;
      for (const auto& polygon : multipolygon) {
        std::vector<std::vector<XY>> rings;
        for (const auto& ring : polygon) {
          std::vector<XY> coords;
          for (const auto& coord : ring) {
            coords.push_back(coord);
          }
          rings.push_back(std::move(coords));
        }
        polygons.push_back(std::move(rings));
      }
      multipolygons.push_back(std::move(polygons));
    }
```

I think the iterators need a few more overloads to be truly random
access iterators, but the idea is there and they can be improved with
time.

All of these are currently "just structs" intended for very low-level
iteration (not necessary a pretty user-facing interface).
  • Loading branch information
paleolimbot authored Jan 5, 2025
1 parent 4bf8525 commit b926c18
Show file tree
Hide file tree
Showing 4 changed files with 1,152 additions and 7 deletions.
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ else()
set(GEOARROW_NAMESPACE_DEFINE "// #define GEOARROW_NAMESPACE YourNamespaceHere")
endif()

if(GEOARROW_BUILD_TESTS)
add_library(geoarrow_coverage_config INTERFACE)
endif()
add_library(geoarrow_coverage_config INTERFACE)

if(GEOARROW_CODE_COVERAGE)
target_compile_options(geoarrow_coverage_config INTERFACE -O0 -g --coverage)
Expand Down Expand Up @@ -276,6 +274,7 @@ if(GEOARROW_BUILD_TESTS)
add_executable(hpp_geometry_data_type_test src/geoarrow/hpp/geometry_data_type_test.cc)
add_executable(hpp_arrow_extension_type_test
src/geoarrow/hpp/arrow_extension_type_test.cc)
add_executable(hpp_array_util_test src/geoarrow/hpp/array_util_test.cc)

target_link_libraries(geoarrow_type_inline_test geoarrow gtest_main
nanoarrow::nanoarrow)
Expand Down Expand Up @@ -304,6 +303,11 @@ if(GEOARROW_BUILD_TESTS)
nanoarrow::nanoarrow)
target_link_libraries(hpp_arrow_extension_type_test geoarrow ${GEOARROW_ARROW_TARGET}
gtest_main)
target_link_libraries(hpp_array_util_test
geoarrow
gtest_main
gmock_main
nanoarrow::nanoarrow)

include(GoogleTest)
gtest_discover_tests(geoarrow_type_inline_test)
Expand All @@ -326,4 +330,5 @@ if(GEOARROW_BUILD_TESTS)
gtest_discover_tests(hpp_array_writer_test)
gtest_discover_tests(hpp_geometry_data_type_test)
gtest_discover_tests(hpp_arrow_extension_type_test)
gtest_discover_tests(hpp_array_util_test)
endif()
4 changes: 0 additions & 4 deletions src/geoarrow/hpp/array_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ class ArrayReader {
void SetArray(struct ArrowArray* array) {
struct GeoArrowError error {};
GEOARROW_THROW_NOT_OK(&error, GeoArrowArrayReaderSetArray(&reader_, array, &error));
if (array_.array.release != nullptr) {
array_.array.release(&array_.array);
}

std::memcpy(&array_, array, sizeof(struct ArrowArray));
array->release = nullptr;
}
Expand Down
Loading

0 comments on commit b926c18

Please sign in to comment.