Skip to content

Commit

Permalink
add size function to index mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
mattldawson committed Feb 13, 2025
1 parent e0c3d21 commit 8280db6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions fortran/test/unit/util.F90
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ subroutine build_and_check_index_mapping_t(config)
source_data(2,:) = (/ 1.0_dk, 2.0_dk, 3.0_dk, 4.0_dk, 5.0_dk /)
target_data(2,:) = (/ 10.0_dk, 20.0_dk, 30.0_dk, 40.0_dk /)

ASSERT_EQ( index_mappings%size( ), 2 )
call index_mappings%copy_data( source_data(2,:), target_data(2,:) )
ASSERT_EQ( target_data( 2, 1 ), 5.0_dk * 0.82_dk )
ASSERT_EQ( target_data( 2, 2 ), 20.0_dk )
Expand Down
19 changes: 19 additions & 0 deletions fortran/util.F90
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ module musica_util
private
type(index_mappings_t_c) :: mappings_c_
contains
procedure :: size => index_mappings_size
procedure :: copy_data
final :: index_mappings_finalize
end type index_mappings_t
Expand Down Expand Up @@ -219,6 +220,12 @@ pure subroutine delete_index_mappings_c( mappings ) &
import :: index_mappings_t_c
type(index_mappings_t_c), intent(inout) :: mappings
end subroutine delete_index_mappings_c
function get_index_mappings_size_c( mappings ) result( size ) &
bind(c, name="GetIndexMappingsSize")
import :: index_mappings_t_c, c_size_t
type(index_mappings_t_c), value, intent(in) :: mappings
integer(c_size_t) :: size
end function get_index_mappings_size_c
subroutine copy_data_c(mappings, source, target) bind(c, name="CopyData")
import :: index_mappings_t_c, c_ptr
type(index_mappings_t_c), value, intent(in) :: mappings
Expand Down Expand Up @@ -740,6 +747,18 @@ function index_mappings_constructor( configuration, source, target, error ) &

end function index_mappings_constructor

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!> Returns the number of elements in an index_mappings_t object
function index_mappings_size( this ) result( size )

class(index_mappings_t), intent(in) :: this
integer :: size

size = get_index_mappings_size_c( this%mappings_c_ )

end function index_mappings_size

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!> Copy data from a source to a target array using index mappings
Expand Down
5 changes: 5 additions & 0 deletions include/musica/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ namespace musica
IndexMappings
CreateIndexMappings(const Configuration configuration, const Mappings source, const Mappings target, Error* error);

/// @brief Returns the number of elements in an array of IndexMappings
/// @param mappings The array of IndexMappings
/// @return The number of elements
std::size_t GetIndexMappingsSize(const IndexMappings mappings);

/// @brief Copies data from one array to another using IndexMappings
/// @param mappings The array of IndexMappings
/// @param source The source array
Expand Down
2 changes: 2 additions & 0 deletions src/test/unit/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ TEST(Util, IndexMappingFromString)
EXPECT_EQ(index_mappings.mappings_[1].target_, 0);
EXPECT_EQ(index_mappings.mappings_[1].scale_factor_, 0.82);
EXPECT_EQ(index_mappings.size_, 2);
EXPECT_EQ(GetIndexMappingsSize(index_mappings), 2);
double source[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
double target[] = { 10.0, 20.0, 30.0, 40.0 };
CopyData(index_mappings, source, target);
Expand Down Expand Up @@ -156,6 +157,7 @@ TEST(Util, IndexMappingFromFile)
EXPECT_EQ(index_mappings.mappings_[1].target_, 0);
EXPECT_EQ(index_mappings.mappings_[1].scale_factor_, 0.82);
EXPECT_EQ(index_mappings.size_, 2);
EXPECT_EQ(GetIndexMappingsSize(index_mappings), 2);
double source[] = { 1.0, 2.0, 3.0, 4.0, 5.0 };
double target[] = { 10.0, 20.0, 30.0, 40.0 };
CopyData(index_mappings, source, target);
Expand Down
5 changes: 5 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ namespace musica
return index_mappings;
}

std::size_t GetIndexMappingsSize(const IndexMappings mappings)
{
return mappings.size_;
}

void CopyData(const IndexMappings mappings, const double* source, double* target)
{
for (std::size_t i = 0; i < mappings.size_; i++)
Expand Down

0 comments on commit 8280db6

Please sign in to comment.