Skip to content

Commit

Permalink
master: pod_vector.hpp: change byte_capacity_ -> capacity_, byte_leng…
Browse files Browse the repository at this point in the history
…th_ -> length_
  • Loading branch information
bradbell committed Dec 11, 2024
1 parent effc0da commit b04b45a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 46 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ IF( POLICY CMP0054 )
ENDIF( POLICY CMP0054 )
#
# cppad_version is used by version.sh to get the version number.
SET(cppad_version "20241210")
SET(cppad_version "20241211")
SET(cppad_url "https://coin-or.github.io/CppAD" )
SET(cppad_description "Differentiation of C++ Algorithms" )
IF( NOT DEFINED CMAKE_BUILD_TYPE)
Expand Down
94 changes: 50 additions & 44 deletions include/cppad/local/pod_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# define CPPAD_LOCAL_POD_VECTOR_HPP
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <[email protected]>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// SPDX-FileContributor: 2003-24 Bradley M. Bell
// ----------------------------------------------------------------------------

# if CPPAD_CSTDINT_HAS_8_TO_64
Expand All @@ -28,31 +28,31 @@ A vector class with that does not use element constructors or destructors
template <class Type>
class pod_vector {
private:
/// maximum number of bytes current allocation can hold
size_t byte_capacity_;

/// number of bytes currently in this vector
size_t byte_length_;
// number of elements in vector
size_t length_;
//
// maximum number of elements current allocation can hold
size_t capacity_;

/// pointer to the first type elements
/// (not defined and should not be used when byte_capacity_ = 0)
/// (not defined and should not be used when capacity_ = 0)
Type *data_;

/// do not use the copy constructor
explicit pod_vector(const pod_vector& )
{ CPPAD_ASSERT_UNKNOWN(false); }
public:
/// default constructor sets byte_capacity_ = byte_length_ = data_ = 0
/// default constructor
pod_vector(void)
: byte_capacity_(0), byte_length_(0), data_(nullptr)
: length_(0), capacity_(0), data_(nullptr)
{ CPPAD_ASSERT_UNKNOWN( is_pod<Type>() );
}

/// sizing constructor
pod_vector(
/// number of elements in this vector
size_t n )
: byte_capacity_(0), byte_length_(0), data_(nullptr)
: length_(0), capacity_(0), data_(nullptr)
{ CPPAD_ASSERT_UNKNOWN( is_pod<Type>() );
extend(n);
}
Expand All @@ -61,7 +61,7 @@ class pod_vector {
/// see extend and resize. If this is not plain old data,
/// the destructor for each element is called.
~pod_vector(void)
{ if( byte_capacity_ > 0 )
{ if( capacity_ > 0 )
{
void* v_ptr = reinterpret_cast<void*>( data_ );
thread_alloc::return_memory(v_ptr);
Expand All @@ -70,11 +70,11 @@ class pod_vector {

/// current number of elements in this vector.
size_t size(void) const
{ return byte_length_ / sizeof(Type); }
{ return length_; }

/// current capacity (amount of allocated storage) for this vector.
size_t capacity(void) const
{ return byte_capacity_ / sizeof(Type); }
{ return capacity_; }

/// current data pointer is no longer valid after any of the following:
/// extend, resize, erase, clear, assignment and destructor.
Expand All @@ -91,7 +91,7 @@ class pod_vector {
/// element index, must be less than length
size_t i
)
{ CPPAD_ASSERT_UNKNOWN( i < byte_length_ / sizeof(Type) );
{ CPPAD_ASSERT_UNKNOWN( i < length_ );
return data_[i];
}
/// non-constant element access; i.e., we can change this element value
Expand All @@ -100,7 +100,7 @@ class pod_vector {
/// element index, must be less than length and convertable to size_t
Index i
)
{ CPPAD_ASSERT_UNKNOWN( size_t(i) < byte_length_ / sizeof(Type) );
{ CPPAD_ASSERT_UNKNOWN( size_t(i) < length_ );
return (*this)[size_t(i)];
}
// ----------------------------------------------------------------------
Expand All @@ -109,7 +109,7 @@ class pod_vector {
/// element index, must be less than length
size_t i
) const
{ CPPAD_ASSERT_UNKNOWN( i < byte_length_ / sizeof(Type) );
{ CPPAD_ASSERT_UNKNOWN( i < length_ );
return data_[i];
}
/// constant element access; i.e., we cannot change this element value
Expand All @@ -118,7 +118,7 @@ class pod_vector {
/// element index, must be less than length and convertable to size_t
Index i
) const
{ CPPAD_ASSERT_UNKNOWN( size_t(i) < byte_length_ / sizeof(Type) );
{ CPPAD_ASSERT_UNKNOWN( size_t(i) < length_ );
return (*this)[size_t(i)];
}
// ----------------------------------------------------------------------
Expand All @@ -143,8 +143,9 @@ class pod_vector {
is the other vector that we are swapping this vector with.
*/
void swap(pod_vector& other)
{ std::swap(byte_capacity_, other.byte_capacity_);
std::swap(byte_length_, other.byte_length_);
{
std::swap(capacity_, other.capacity_);
std::swap(length_, other.length_);
std::swap(data_, other.data_);
}
// ----------------------------------------------------------------------
Expand All @@ -167,32 +168,35 @@ class pod_vector {
pod_vector. They uses thread_alloc for this allocation.
*/
size_t extend(size_t n)
{ size_t old_length = byte_length_;
byte_length_ += n * sizeof(Type);
{ size_t old_length = length_;
length_ += n;

// check if we can use current memory
if( byte_length_ <= byte_capacity_ )
return old_length / sizeof(Type);
if( length_ <= capacity_ )
return old_length;

// save more old information
size_t old_capacity = byte_capacity_;
size_t old_capacity = capacity_;
void* old_v_ptr = reinterpret_cast<void*>(data_);

// get new memory and set capacity
void* v_ptr = thread_alloc::get_memory(byte_length_, byte_capacity_);
data_ = reinterpret_cast<Type*>(v_ptr);
size_t byte_capacity;
size_t bytes = length_ * sizeof(Type);
void* v_ptr = thread_alloc::get_memory(bytes, byte_capacity);
capacity_ = byte_capacity / sizeof(Type);
data_ = reinterpret_cast<Type*>(v_ptr);

// copy old data to new
if( old_length > 0 )
std::memcpy(v_ptr, old_v_ptr, old_length);
std::memcpy(v_ptr, old_v_ptr, old_length * sizeof(Type));

// return old memory to available pool
if( old_capacity > 0 )
thread_alloc::return_memory(old_v_ptr);

// return value for extend(n) is the old length
CPPAD_ASSERT_UNKNOWN( byte_length_ <= byte_capacity_ );
return old_length / sizeof(Type);
CPPAD_ASSERT_UNKNOWN( length_ <= capacity_ );
return old_length;
}
// ----------------------------------------------------------------------
/*!
Expand All @@ -215,54 +219,56 @@ class pod_vector {
pod_vector. They uses thread_alloc for this allocation.
*/
void resize(size_t n)
{ byte_length_ = n * sizeof(Type);
{ length_ = n;

// check if we must allocate new memory
if( byte_capacity_ < byte_length_ )
if( capacity_ < length_ )
{ void* v_ptr;
//
if( byte_capacity_ > 0 )
if( capacity_ > 0 )
{ // return old memory to available pool
v_ptr = reinterpret_cast<void*>( data_ );
thread_alloc::return_memory(v_ptr);
}
//
// get new memory and set capacity
v_ptr = thread_alloc::get_memory(byte_length_, byte_capacity_);
data_ = reinterpret_cast<Type*>(v_ptr);
size_t byte_capacity;
size_t bytes = length_ * sizeof(Type);
v_ptr = thread_alloc::get_memory(bytes, byte_capacity);
capacity_ = byte_capacity / sizeof(Type);
data_ = reinterpret_cast<Type*>(v_ptr);
//
}
CPPAD_ASSERT_UNKNOWN( byte_length_ <= byte_capacity_ );
CPPAD_ASSERT_UNKNOWN( length_ <= capacity_ );
}
// ----------------------------------------------------------------------
/*!
Remove all the elements from this vector and free its memory.
*/
void clear(void)
{ if( byte_capacity_ > 0 )
{ if( capacity_ > 0 )
{
void* v_ptr = reinterpret_cast<void*>( data_ );
thread_alloc::return_memory(v_ptr);
}
data_ = nullptr;
byte_capacity_ = 0;
byte_length_ = 0;
capacity_ = 0;
length_ = 0;
}
// -----------------------------------------------------------------------
/// vector assignment operator
void operator=(
/// right hand size of the assingment operation
const pod_vector& x
)
{ CPPAD_ASSERT_UNKNOWN( x.byte_length_ % sizeof(Type) == 0 );
resize( x.byte_length_ / sizeof(Type) );
if( byte_length_ > 0 )
{
{
resize( x.length_ );
if( length_ > 0 )
{ size_t bytes = length_ * sizeof(Type);
void* v_ptr = reinterpret_cast<void*>( data_ );
void* v_ptr_x = reinterpret_cast<void*>( x.data_ );
std::memcpy(v_ptr, v_ptr_x, byte_length_);
std::memcpy(v_ptr, v_ptr_x, bytes);
}

}
};
// ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion user_guide.xrst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

{xrst_comment BEGIN: Before changing see new_release.sh and check_version.sh}

cppad-20241210: CppAD User's Manual
cppad-20241211: CppAD User's Manual
###################################
{xrst_comment The scripts bin/check_version.sh and bin/new_release.sh
edit version numbers in tables below.}
Expand Down

0 comments on commit b04b45a

Please sign in to comment.