Skip to content

Commit

Permalink
Add size function to index mappings (#250)
Browse files Browse the repository at this point in the history
* add size function to index mappings

* address reviewer comments
  • Loading branch information
mattldawson authored Feb 13, 2025
1 parent e0c3d21 commit b443670
Show file tree
Hide file tree
Showing 5 changed files with 44 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
31 changes: 31 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 All @@ -161,34 +162,40 @@ function create_string_c( string ) bind(c, name="CreateString")
character(kind=c_char, len=1), intent(in) :: string(*)
type(string_t_c) :: create_string_c
end function create_string_c

pure subroutine delete_string_c( string ) bind(c, name="DeleteString")
import :: string_t_c
type(string_t_c), intent(inout) :: string
end subroutine delete_string_c

function load_configuration_from_string_c( string, error ) &
bind(c, name="LoadConfigurationFromString")
import :: configuration_t_c, c_char, error_t_c
character(kind=c_char, len=1), intent(in) :: string(*)
type(error_t_c), intent(inout) :: error
type(configuration_t_c) :: load_configuration_from_string_c
end function load_configuration_from_string_c

function load_configuration_from_file_c( file, error ) &
bind(c, name="LoadConfigurationFromFile")
import :: configuration_t_c, c_char, error_t_c
character(kind=c_char, len=1), intent(in) :: file(*)
type(error_t_c), intent(inout) :: error
type(configuration_t_c) :: load_configuration_from_file_c
end function load_configuration_from_file_c

pure subroutine delete_configuration_c( configuration ) &
bind(c, name="DeleteConfiguration")
import :: configuration_t_c
type(configuration_t_c), intent(inout) :: configuration
end subroutine delete_configuration_c

function create_mappings_c( size ) bind(c, name="CreateMappings")
import :: mappings_t_c, c_size_t
integer(c_size_t), value, intent(in) :: size
type(mappings_t_c) :: create_mappings_c
end function create_mappings_c

function create_index_mappings_c(configuration, source, target, error) &
bind(c, name="CreateIndexMappings")
import :: index_mappings_t_c, configuration_t_c, error_t_c, mappings_t_c
Expand All @@ -198,10 +205,12 @@ function create_index_mappings_c(configuration, source, target, error) &
type(error_t_c), intent(inout) :: error
type(index_mappings_t_c) :: create_index_mappings_c
end function create_index_mappings_c

pure subroutine delete_mapping_c( mapping ) bind(c, name="DeleteMapping")
import :: mapping_t_c
type(mapping_t_c), intent(inout) :: mapping
end subroutine delete_mapping_c

function find_mapping_index_c( mappings, name, error ) result( index ) &
bind(c, name="FindMappingIndex")
import :: mappings_t_c, error_t_c, c_char, c_size_t
Expand All @@ -210,15 +219,25 @@ function find_mapping_index_c( mappings, name, error ) result( index ) &
type(error_t_c), intent(inout) :: error
integer(c_size_t) :: index
end function find_mapping_index_c

pure subroutine delete_mappings_c( mappings ) bind(c, name="DeleteMappings")
import :: mappings_t_c
type(mappings_t_c), intent(inout) :: mappings
end subroutine delete_mappings_c

pure subroutine delete_index_mappings_c( mappings ) &
bind(c, name="DeleteIndexMappings")
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 +759,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 IndexMappings container
/// @param mappings The IndexMappings container
/// @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 b443670

Please sign in to comment.