packages feed

cardano-c-clib-0.1.1.0: vendor/src/transaction_body/account_balance_intervals_map.c

/**
 * \file account_balance_intervals_map.c
 *
 * \author angel.castillo
 * \date   Jul 16, 2026
 *
 * Copyright 2026 Biglup Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* INCLUDES ******************************************************************/

#include <cardano/object.h>
#include <cardano/transaction_body/account_balance_intervals_map.h>

#include "../allocators.h"
#include "../cbor/cbor_validation.h"
#include "../collections/array.h"
#include "../string_safe.h"

#include <assert.h>
#include <string.h>

/* STRUCTURES ****************************************************************/

/**
 * \brief Represents a map of reward addresses to account balance intervals.
 */
typedef struct cardano_account_balance_intervals_map_t
{
    cardano_object_t base;
    cardano_array_t* array;
} cardano_account_balance_intervals_map_t;

/**
 * \brief Represents a reward address to account balance interval key value pair.
 */
typedef struct cardano_account_balance_intervals_map_kvp_t
{
    cardano_object_t                    base;
    cardano_reward_address_t*           key;
    cardano_account_balance_interval_t* value;
} cardano_account_balance_intervals_map_kvp_t;

/* STATIC FUNCTIONS **********************************************************/

/**
 * \brief Deallocates an account balance intervals map object.
 *
 * This function is responsible for properly deallocating an account balance intervals map object (`cardano_account_balance_intervals_map_t`)
 * and its associated resources.
 *
 * \param object A void pointer to the account_balance_intervals_map object to be deallocated. The function casts this
 *               pointer to the appropriate type (`cardano_account_balance_intervals_map_t*`).
 *
 * \note It is assumed that this function is called only when the reference count of the account_balance_intervals_map
 *       object reaches zero, as part of the reference counting mechanism implemented for managing the
 *       lifecycle of these objects.
 */
static void
cardano_account_balance_intervals_map_deallocate(void* object)
{
  assert(object != NULL);

  cardano_account_balance_intervals_map_t* map = (cardano_account_balance_intervals_map_t*)object;

  if (map->array != NULL)
  {
    cardano_array_unref(&map->array);
  }

  _cardano_free(map);
}

/**
 * \brief Deallocates an account balance intervals map key value pair object.
 *
 * This function is responsible for properly deallocating an account balance intervals map key value pair object
 * (`cardano_account_balance_intervals_map_kvp_t`) and its associated resources.
 *
 * \param object A void pointer to the account_balance_intervals_map object to be deallocated. The function casts this
 *               pointer to the appropriate type (`cardano_account_balance_intervals_map_kvp_t*`).
 *
 * \note It is assumed that this function is called only when the reference count of the account_balance_intervals_map
 *       object reaches zero, as part of the reference counting mechanism implemented for managing the
 *       lifecycle of these objects.
 */
static void
cardano_account_balance_intervals_map_kvp_deallocate(void* object)
{
  assert(object != NULL);

  cardano_account_balance_intervals_map_kvp_t* map = (cardano_account_balance_intervals_map_kvp_t*)object;

  if (map->key != NULL)
  {
    cardano_reward_address_unref(&map->key);
  }

  if (map->value != NULL)
  {
    cardano_account_balance_interval_unref(&map->value);
  }

  _cardano_free(map);
}

/**
 * \brief Compares two reward addresses based on their serialized bytes.
 *
 * \param[in] lhs Pointer to the first reward address object.
 * \param[in] rhs Pointer to the second reward address object.
 *
 * \return true if the addresses are equal, false otherwise.
 */
static bool
reward_address_equals(const cardano_reward_address_t* lhs, const cardano_reward_address_t* rhs)
{
  if (lhs == NULL)
  {
    return false;
  }

  if (rhs == NULL)
  {
    return false;
  }

  const size_t lhs_size = cardano_reward_address_get_bytes_size(lhs);
  const size_t rhs_size = cardano_reward_address_get_bytes_size(rhs);

  if (lhs_size != rhs_size)
  {
    return false;
  }

  const uint8_t* lhs_bytes = cardano_reward_address_get_bytes(lhs);
  const uint8_t* rhs_bytes = cardano_reward_address_get_bytes(rhs);

  return memcmp(lhs_bytes, rhs_bytes, lhs_size) == 0;
}

/**
 * \brief Compares two cardano_account_balance_intervals_map_kvp_t objects based on their reward_address.
 *
 * \param[in] lhs Pointer to the first cardano_object_t object.
 * \param[in] rhs Pointer to the second cardano_object_t object.
 * \param[in] context Unused.
 *
 * \return A negative value if the address of lhs is less than the address of rhs, zero if they are equal,
 *         and a positive value if the address of lhs is greater than the address of rhs.
 */
static int32_t
compare_by_bytes(const cardano_object_t* lhs, const cardano_object_t* rhs, void* context)
{
  assert(lhs != NULL);
  assert(rhs != NULL);

  CARDANO_UNUSED(context);

  const cardano_account_balance_intervals_map_kvp_t* lhs_kvp = (const cardano_account_balance_intervals_map_kvp_t*)((const void*)lhs);
  const cardano_account_balance_intervals_map_kvp_t* rhs_kvp = (const cardano_account_balance_intervals_map_kvp_t*)((const void*)rhs);

  const size_t lhs_size = cardano_reward_address_get_bytes_size(lhs_kvp->key);
  const size_t rhs_size = cardano_reward_address_get_bytes_size(rhs_kvp->key);

  if (lhs_size != rhs_size)
  {
    return (lhs_size < rhs_size) ? -1 : 1;
  }

  const uint8_t* lhs_bytes = cardano_reward_address_get_bytes(lhs_kvp->key);
  const uint8_t* rhs_bytes = cardano_reward_address_get_bytes(rhs_kvp->key);

  return memcmp(lhs_bytes, rhs_bytes, lhs_size);
}

/* DEFINITIONS ****************************************************************/

cardano_error_t
cardano_account_balance_intervals_map_new(cardano_account_balance_intervals_map_t** account_balance_intervals_map)
{
  if (account_balance_intervals_map == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_account_balance_intervals_map_t* map = _cardano_malloc(sizeof(cardano_account_balance_intervals_map_t));

  if (map == NULL)
  {
    return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
  }

  map->base.ref_count     = 1;
  map->base.last_error[0] = '\0';
  map->base.deallocator   = cardano_account_balance_intervals_map_deallocate;

  map->array = cardano_array_new(32);

  if (map->array == NULL)
  {
    _cardano_free(map);
    return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
  }

  *account_balance_intervals_map = map;

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_account_balance_intervals_map_from_cbor(cardano_cbor_reader_t* reader, cardano_account_balance_intervals_map_t** account_balance_intervals_map)
{
  if (reader == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (account_balance_intervals_map == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_account_balance_intervals_map_t* map    = NULL;
  cardano_error_t                          result = cardano_account_balance_intervals_map_new(&map);

  if (result != CARDANO_SUCCESS)
  {
    return result;
  }

  int64_t length = 0;

  result = cardano_cbor_reader_read_start_map(reader, &length);

  if (result != CARDANO_SUCCESS)
  {
    cardano_account_balance_intervals_map_unref(&map);
    return result;
  }

  cardano_cbor_reader_state_t state = CARDANO_CBOR_READER_STATE_UNDEFINED;

  result = cardano_cbor_reader_peek_state(reader, &state);

  if (result != CARDANO_SUCCESS)
  {
    cardano_account_balance_intervals_map_unref(&map);
    return result;
  }

  if (state == CARDANO_CBOR_READER_STATE_END_MAP)
  {
    cardano_cbor_reader_set_last_error(reader, "There was an error decoding 'account_balance_intervals_map', the map must not be empty.");
    cardano_account_balance_intervals_map_unref(&map);

    return CARDANO_ERROR_INVALID_CBOR_MAP_SIZE;
  }

  while (state != CARDANO_CBOR_READER_STATE_END_MAP)
  {
    result = cardano_cbor_reader_peek_state(reader, &state);

    if (result != CARDANO_SUCCESS)
    {
      cardano_account_balance_intervals_map_unref(&map);
      return result;
    }

    if (state == CARDANO_CBOR_READER_STATE_END_MAP)
    {
      break;
    }

    cardano_reward_address_t*           key   = NULL;
    cardano_account_balance_interval_t* value = NULL;

    cardano_buffer_t* reward_address_bytes = NULL;

    result = cardano_cbor_reader_read_bytestring(reader, &reward_address_bytes);

    if (result != CARDANO_SUCCESS)
    {
      cardano_account_balance_intervals_map_unref(&map);
      return result;
    }

    result = cardano_reward_address_from_bytes(cardano_buffer_get_data(reward_address_bytes), cardano_buffer_get_size(reward_address_bytes), &key);

    cardano_buffer_unref(&reward_address_bytes);

    if (result != CARDANO_SUCCESS)
    {
      cardano_account_balance_intervals_map_unref(&map);
      return result;
    }

    result = cardano_account_balance_interval_from_cbor(reader, &value);

    if (result != CARDANO_SUCCESS)
    {
      cardano_reward_address_unref(&key);
      cardano_account_balance_intervals_map_unref(&map);
      return result;
    }

    result = cardano_account_balance_intervals_map_insert(map, key, value);

    cardano_reward_address_unref(&key);
    cardano_account_balance_interval_unref(&value);

    if (result != CARDANO_SUCCESS)
    {
      cardano_cbor_reader_set_last_error(reader, "There was an error decoding 'account_balance_intervals_map', the map must not contain duplicated keys.");
      cardano_account_balance_intervals_map_unref(&map);

      return result;
    }
  }

  result = cardano_cbor_validate_end_map("account_balance_intervals_map", reader);

  if (result != CARDANO_SUCCESS)
  {
    cardano_account_balance_intervals_map_unref(&map);
    return result;
  }

  *account_balance_intervals_map = map;

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_account_balance_intervals_map_to_cbor(const cardano_account_balance_intervals_map_t* account_balance_intervals_map, cardano_cbor_writer_t* writer)
{
  if (account_balance_intervals_map == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (writer == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_error_t result = CARDANO_SUCCESS;

  size_t map_size = cardano_array_get_size(account_balance_intervals_map->array);
  result          = cardano_cbor_writer_write_start_map(writer, (int64_t)map_size);

  if (result != CARDANO_SUCCESS)
  {
    return result;
  }

  for (size_t i = 0; i < cardano_array_get_size(account_balance_intervals_map->array); ++i)
  {
    cardano_object_t* kvp = cardano_array_get(account_balance_intervals_map->array, i);

    if (kvp == NULL)
    {
      cardano_cbor_writer_set_last_error(writer, "Element in account balance intervals map is NULL");
      return CARDANO_ERROR_ENCODING;
    }

    cardano_account_balance_intervals_map_kvp_t* kvp_data = (cardano_account_balance_intervals_map_kvp_t*)((void*)kvp);

    result = cardano_cbor_writer_write_bytestring(writer, cardano_reward_address_get_bytes(kvp_data->key), cardano_reward_address_get_bytes_size(kvp_data->key));

    if (result != CARDANO_SUCCESS)
    {
      cardano_object_unref(&kvp);
      return result;
    }

    result = cardano_account_balance_interval_to_cbor(kvp_data->value, writer);

    cardano_object_unref(&kvp);

    if (result != CARDANO_SUCCESS)
    {
      return result;
    }
  }

  return CARDANO_SUCCESS;
}

size_t
cardano_account_balance_intervals_map_get_length(const cardano_account_balance_intervals_map_t* account_balance_intervals_map)
{
  if (account_balance_intervals_map == NULL)
  {
    return 0;
  }

  return cardano_array_get_size(account_balance_intervals_map->array);
}

cardano_error_t
cardano_account_balance_intervals_map_get(
  const cardano_account_balance_intervals_map_t* account_balance_intervals_map,
  cardano_reward_address_t*                      key,
  cardano_account_balance_interval_t**           element)
{
  if (account_balance_intervals_map == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (key == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (element == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  for (size_t i = 0; i < cardano_array_get_size(account_balance_intervals_map->array); ++i)
  {
    cardano_object_t*                            object = cardano_array_get(account_balance_intervals_map->array, i);
    cardano_account_balance_intervals_map_kvp_t* kvp    = (cardano_account_balance_intervals_map_kvp_t*)((void*)object);

    if (reward_address_equals(kvp->key, key))
    {
      cardano_account_balance_interval_ref(kvp->value);

      *element = kvp->value;

      cardano_object_unref(&object);
      return CARDANO_SUCCESS;
    }

    cardano_object_unref(&object);
  }

  return CARDANO_ERROR_ELEMENT_NOT_FOUND;
}

cardano_error_t
cardano_account_balance_intervals_map_insert(
  cardano_account_balance_intervals_map_t* account_balance_intervals_map,
  cardano_reward_address_t*                key,
  cardano_account_balance_interval_t*      value)
{
  if (account_balance_intervals_map == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (key == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (value == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  for (size_t i = 0; i < cardano_array_get_size(account_balance_intervals_map->array); ++i)
  {
    cardano_object_t*                            object = cardano_array_get(account_balance_intervals_map->array, i);
    cardano_account_balance_intervals_map_kvp_t* kvp    = (cardano_account_balance_intervals_map_kvp_t*)((void*)object);

    const bool duplicated = reward_address_equals(kvp->key, key);

    cardano_object_unref(&object);

    if (duplicated)
    {
      return CARDANO_ERROR_DUPLICATED_KEY;
    }
  }

  cardano_account_balance_intervals_map_kvp_t* kvp = _cardano_malloc(sizeof(cardano_account_balance_intervals_map_kvp_t));

  if (kvp == NULL)
  {
    return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
  }

  kvp->base.ref_count     = 0;
  kvp->base.last_error[0] = '\0';
  kvp->base.deallocator   = cardano_account_balance_intervals_map_kvp_deallocate;
  kvp->key                = key;
  kvp->value              = value;

  cardano_reward_address_ref(key);
  cardano_account_balance_interval_ref(value);

  const size_t old_size = cardano_array_get_size(account_balance_intervals_map->array);
  const size_t new_size = cardano_array_push(account_balance_intervals_map->array, (cardano_object_t*)((void*)kvp));

  if (new_size != (old_size + 1U))
  {
    cardano_account_balance_intervals_map_kvp_deallocate(kvp);
    return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
  }

  cardano_array_sort(account_balance_intervals_map->array, compare_by_bytes, NULL);

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_account_balance_intervals_map_insert_ex(
  cardano_account_balance_intervals_map_t* account_balance_intervals_map,
  const char*                              reward_address,
  size_t                                   reward_address_size,
  cardano_account_balance_interval_t*      value)
{
  if (account_balance_intervals_map == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_reward_address_t* key = NULL;

  cardano_error_t result = cardano_reward_address_from_bech32(reward_address, reward_address_size, &key);

  if (result != CARDANO_SUCCESS)
  {
    return result;
  }

  result = cardano_account_balance_intervals_map_insert(account_balance_intervals_map, key, value);

  cardano_reward_address_unref(&key);

  return result;
}

cardano_error_t
cardano_account_balance_intervals_map_get_keys(
  cardano_account_balance_intervals_map_t* account_balance_intervals_map,
  cardano_reward_address_list_t**          keys)
{
  if (account_balance_intervals_map == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (keys == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_reward_address_list_t* list = NULL;

  cardano_error_t result = cardano_reward_address_list_new(&list);

  if (result != CARDANO_SUCCESS)
  {
    return result;
  }

  for (size_t i = 0; i < cardano_array_get_size(account_balance_intervals_map->array); ++i)
  {
    cardano_object_t*                            object = cardano_array_get(account_balance_intervals_map->array, i);
    cardano_account_balance_intervals_map_kvp_t* kvp    = (cardano_account_balance_intervals_map_kvp_t*)((void*)object);

    result = cardano_reward_address_list_add(list, kvp->key);

    if (result != CARDANO_SUCCESS)
    {
      cardano_reward_address_list_unref(&list);
      cardano_object_unref(&object);
      return result;
    }

    cardano_object_unref(&object);
  }

  *keys = list;

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_account_balance_intervals_map_get_key_at(
  const cardano_account_balance_intervals_map_t* account_balance_intervals_map,
  const size_t                                   index,
  cardano_reward_address_t**                     reward_address)
{
  if (account_balance_intervals_map == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (reward_address == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (index >= cardano_array_get_size(account_balance_intervals_map->array))
  {
    return CARDANO_ERROR_INDEX_OUT_OF_BOUNDS;
  }

  cardano_object_t*                            object = cardano_array_get(account_balance_intervals_map->array, index);
  cardano_account_balance_intervals_map_kvp_t* kvp    = (cardano_account_balance_intervals_map_kvp_t*)((void*)object);

  cardano_reward_address_ref(kvp->key);
  cardano_object_unref(&object);

  *reward_address = kvp->key;

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_account_balance_intervals_map_get_value_at(
  const cardano_account_balance_intervals_map_t* account_balance_intervals_map,
  const size_t                                   index,
  cardano_account_balance_interval_t**           interval)
{
  if (account_balance_intervals_map == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (interval == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (index >= cardano_array_get_size(account_balance_intervals_map->array))
  {
    return CARDANO_ERROR_INDEX_OUT_OF_BOUNDS;
  }

  cardano_object_t*                            object = cardano_array_get(account_balance_intervals_map->array, index);
  cardano_account_balance_intervals_map_kvp_t* kvp    = (cardano_account_balance_intervals_map_kvp_t*)((void*)object);

  cardano_account_balance_interval_ref(kvp->value);
  cardano_object_unref(&object);

  *interval = kvp->value;

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_account_balance_intervals_map_get_key_value_at(
  const cardano_account_balance_intervals_map_t* account_balance_intervals_map,
  const size_t                                   index,
  cardano_reward_address_t**                     reward_address,
  cardano_account_balance_interval_t**           interval)
{
  if (account_balance_intervals_map == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (reward_address == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (interval == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  if (index >= cardano_array_get_size(account_balance_intervals_map->array))
  {
    return CARDANO_ERROR_INDEX_OUT_OF_BOUNDS;
  }

  cardano_object_t*                            object = cardano_array_get(account_balance_intervals_map->array, index);
  cardano_account_balance_intervals_map_kvp_t* kvp    = (cardano_account_balance_intervals_map_kvp_t*)((void*)object);

  cardano_reward_address_ref(kvp->key);
  cardano_account_balance_interval_ref(kvp->value);
  cardano_object_unref(&object);

  *reward_address = kvp->key;
  *interval       = kvp->value;

  return CARDANO_SUCCESS;
}

cardano_error_t
cardano_account_balance_intervals_map_to_cip116_json(const cardano_account_balance_intervals_map_t* value, cardano_json_writer_t* writer)
{
  if ((value == NULL) || (writer == NULL))
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_json_writer_write_start_array(writer);

  for (size_t i = 0U; i < cardano_array_get_size(value->array); ++i)
  {
    cardano_object_t* entry = cardano_array_get(value->array, i);

    if (entry == NULL)
    {
      return CARDANO_ERROR_ENCODING;
    }

    const cardano_account_balance_intervals_map_kvp_t* pair   = (const cardano_account_balance_intervals_map_kvp_t*)((const void*)entry);
    cardano_error_t                                    result = CARDANO_SUCCESS;

    cardano_json_writer_write_start_object(writer);
    cardano_json_writer_write_property_name(writer, "key", 3);
    const char*  address = cardano_reward_address_get_string(pair->key);
    const size_t size    = cardano_safe_strlen(address, cardano_reward_address_get_bech32_size(pair->key));
    cardano_json_writer_write_string(writer, address, size);
    cardano_json_writer_write_property_name(writer, "value", 5);
    result = cardano_account_balance_interval_to_cip116_json(pair->value, writer);
    cardano_json_writer_write_end_object(writer);
    cardano_object_unref(&entry);

    if (result != CARDANO_SUCCESS)
    {
      return result;
    }
  }

  cardano_json_writer_write_end_array(writer);

  return CARDANO_SUCCESS;
}

void
cardano_account_balance_intervals_map_unref(cardano_account_balance_intervals_map_t** account_balance_intervals_map)
{
  if ((account_balance_intervals_map == NULL) || (*account_balance_intervals_map == NULL))
  {
    return;
  }

  cardano_object_t* object = &(*account_balance_intervals_map)->base;
  cardano_object_unref(&object);

  if (object == NULL)
  {
    *account_balance_intervals_map = NULL;
    return;
  }
}

void
cardano_account_balance_intervals_map_ref(cardano_account_balance_intervals_map_t* account_balance_intervals_map)
{
  if (account_balance_intervals_map == NULL)
  {
    return;
  }

  cardano_object_ref(&account_balance_intervals_map->base);
}

size_t
cardano_account_balance_intervals_map_refcount(const cardano_account_balance_intervals_map_t* account_balance_intervals_map)
{
  if (account_balance_intervals_map == NULL)
  {
    return 0;
  }

  return cardano_object_refcount(&account_balance_intervals_map->base);
}

void
cardano_account_balance_intervals_map_set_last_error(cardano_account_balance_intervals_map_t* account_balance_intervals_map, const char* message)
{
  cardano_object_set_last_error(&account_balance_intervals_map->base, message);
}

const char*
cardano_account_balance_intervals_map_get_last_error(const cardano_account_balance_intervals_map_t* account_balance_intervals_map)
{
  return cardano_object_get_last_error(&account_balance_intervals_map->base);
}

/**
 * Deep copies a private map entry and its owned values.
 */
static cardano_error_t
cardano_account_balance_intervals_map_kvp_deep_clone(const cardano_account_balance_intervals_map_kvp_t* source, cardano_account_balance_intervals_map_kvp_t** clone)
{
  if (clone == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  *clone = NULL;

  if (source == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_account_balance_intervals_map_kvp_t* copy = _cardano_malloc(sizeof(cardano_account_balance_intervals_map_kvp_t));

  if (copy == NULL)
  {
    return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
  }

  copy->base.ref_count     = 1U;
  copy->base.deallocator   = source->base.deallocator;
  copy->base.last_error[0] = '\0';
  copy->key                = NULL;
  copy->value              = NULL;

  if (source->key != NULL)
  {
    const cardano_error_t result = cardano_reward_address_deep_clone(source->key, &copy->key);

    if (result != CARDANO_SUCCESS)
    {
      copy->base.deallocator(copy);
      return result;
    }
  }

  if (source->value != NULL)
  {
    const cardano_error_t result = cardano_account_balance_interval_deep_clone(source->value, &copy->value);

    if (result != CARDANO_SUCCESS)
    {
      copy->base.deallocator(copy);
      return result;
    }
  }

  *clone = copy;

  return CARDANO_SUCCESS;
}

/**
 * Deep copies one element for the typed collection.
 */
static cardano_error_t
clone_array_element(const cardano_object_t* source, cardano_object_t** clone)
{
  cardano_account_balance_intervals_map_kvp_t* copy   = NULL;
  const cardano_error_t                        result = cardano_account_balance_intervals_map_kvp_deep_clone((const cardano_account_balance_intervals_map_kvp_t*)((const void*)source), &copy);

  *clone = (cardano_object_t*)((void*)copy);

  return result;
}

cardano_error_t
cardano_account_balance_intervals_map_deep_clone(const cardano_account_balance_intervals_map_t* source, cardano_account_balance_intervals_map_t** clone)
{
  if (clone == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  *clone = NULL;

  if (source == NULL)
  {
    return CARDANO_ERROR_POINTER_IS_NULL;
  }

  cardano_account_balance_intervals_map_t* copy = _cardano_malloc(sizeof(cardano_account_balance_intervals_map_t));

  if (copy == NULL)
  {
    return CARDANO_ERROR_MEMORY_ALLOCATION_FAILED;
  }

  copy->base.ref_count     = 1U;
  copy->base.deallocator   = source->base.deallocator;
  copy->base.last_error[0] = '\0';
  copy->array              = NULL;

  if (source->array != NULL)
  {
    const cardano_error_t result = cardano_array_deep_clone(source->array, clone_array_element, &copy->array);

    if (result != CARDANO_SUCCESS)
    {
      copy->base.deallocator(copy);
      return result;
    }
  }

  *clone = copy;

  return CARDANO_SUCCESS;
}