Skip to content

core: Add Array template class to abstract a fixed-size contiguous buffer whose size is determined at runtime.#513

Merged
kirkrodrigues merged 5 commits into
y-scope:mainfrom
LinZhihao-723:array
Aug 18, 2024
Merged

core: Add Array template class to abstract a fixed-size contiguous buffer whose size is determined at runtime.#513
kirkrodrigues merged 5 commits into
y-scope:mainfrom
LinZhihao-723:array

Conversation

@LinZhihao-723

@LinZhihao-723 LinZhihao-723 commented Aug 11, 2024

Copy link
Copy Markdown
Member

Description

In the current CLP, we use unique pointers of C-style arrays across the code base whenever we need a fix-sized buffer. This violates clang-tidy suggestions which advocate deprecating C-style arrays. However, existing std containers have limitations to use.

  • For std::vector, it is hard to control the underlying storage. The storage size is controlled by its capacity but not the size. Also, the vector can't be made immutable to use the storage as a buffer to receive data (such as the use case in NetworkReader). This means there's no way to avoid reallocation/resizing by mistakes. Declaring a vector and using its underlying data() buffer will not provide any safety.
  • For std::array, the size must be given in the compile time.

Therefore, we introduce our own implementation of arrays, clp::Array, which is a fix-sized array where the size is determined during the run-time. We use std::unique_ptr to manage the underlying buffer storage (which is allocated in the heap), and a member to keep track of the size to avoid out-of-bound access. This PR implements the basic features of our array, including methods for random access and iterators. At this stage, the underlying element of the array must be default initializable so that we don't have to implement the initializer list constructor yet.
APIs of this class are designed to mirror std containers.

Validation performed

  • Ensure all workflows can be built.
  • Add unit tests to cover the basic usages against fundamental types and default constructible aggregated types, and ensure the iterator is compatible with std algorithms.

Comment thread components/core/src/clp/Array.hpp Outdated
Comment thread components/core/src/clp/Array.hpp Outdated
Comment thread components/core/src/clp/Array.hpp Outdated
Comment thread components/core/src/clp/Array.hpp Outdated
Comment thread components/core/src/clp/Array.hpp Outdated
Comment thread components/core/src/clp/Array.hpp Outdated
Comment thread components/core/tests/test-Array.cpp Outdated
Comment thread components/core/tests/test-Array.cpp Outdated
Comment thread components/core/tests/test-Array.cpp
LinZhihao-723 and others added 3 commits August 17, 2024 08:46
Co-authored-by: kirkrodrigues <2454684+kirkrodrigues@users.noreply.github.com>

@kirkrodrigues kirkrodrigues left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the PR title, how about:

core: Add Array template class to abstract a fixed-size contiguous buffer whose size is determined at runtime.

@kirkrodrigues

Copy link
Copy Markdown
Member

Workflow failure is unrelated and is being tracked in #519.

@LinZhihao-723 LinZhihao-723 changed the title core: Add Array template class for fix-sized continuous storage at run-time. core: Add Array template class to abstract a fixed-size contiguous buffer whose size is determined at runtime. Aug 18, 2024
@kirkrodrigues kirkrodrigues merged commit 86299ca into y-scope:main Aug 18, 2024
jackluo923 pushed a commit to jackluo923/clp that referenced this pull request Dec 4, 2024
@LinZhihao-723 LinZhihao-723 deleted the array branch June 5, 2025 14:59
junhaoliao pushed a commit to junhaoliao/clp that referenced this pull request May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants