Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
feat(utils): Add datatype bundle object array
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Sep 24, 2019
1 parent 77d7a9b commit ddc16d8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
9 changes: 9 additions & 0 deletions utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ cc_library(
"@entangled//common/model:transaction",
],
)

cc_library(
name = "bundle_array",
srcs = ["bundle_array.h"],
deps = [
"@com_github_uthash//:uthash",
"@entangled//common/model:bundle",
],
)
61 changes: 61 additions & 0 deletions utils/bundle_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2019 BiiLabs Co., Ltd. and Contributors
* All Rights Reserved.
* This is free software; you can redistribute it and/or modify it under the
* terms of the MIT license. A copy of the license can be found in the file
* "LICENSE" at the root of this distribution.
*/

#ifndef UTILS_BUNDLE_ARRAY_H_
#define UTILS_BUNDLE_ARRAY_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "common/model/bundle.h"
#include "utarray.h"

/**
* @file bundle_array.h
* @brief Implementation of bundle array object. This object would be used when we fetch multiple
* `bundle_transactions_t` objects. It provides an easier way to save and traverse all the bundles.
*/

typedef UT_array bundle_array_t;

static UT_icd bundle_array_icd = {sizeof(bundle_transactions_t), 0, 0, 0};

/**
* Allocate memory of bundle_array_t
*/
static inline void bundle_array_new(bundle_array_t **const bundle_array) {
utarray_new(*bundle_array, &bundle_array_icd);
}

/**
* @brief The bundle array iterator.
*/
#define BUNDLE_ARRAY_FOREACH(bundles, bdl) \
for (bdl = (bundle_transactions_t *)utarray_front(bundles); bdl != NULL; \
bdl = (bundle_transactions_t *)utarray_next(bundles, bdl))

/**
* Free memory of bundle_array_t
*/
static inline void bundle_array_free(bundle_array_t **const bundle_array) {
// TODO set dtor and use utarray_free() instead.
if (bundle_array && *bundle_array) {
bundle_transactions_t *bundle = NULL;
iota_transaction_t *tx = NULL;
BUNDLE_ARRAY_FOREACH(*bundle_array, bundle) { bundle_transactions_free(&bundle); }
free(*bundle_array);
}
*bundle_array = NULL;
}

#ifdef __cplusplus
}
#endif

#endif // UTILS_BUNDLE_ARRAY_H_
2 changes: 1 addition & 1 deletion utils/pow.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
#define UTILS_POW_H_

#include <stdint.h>
#include "./utarray.h"
#include "accelerator/errors.h"
#include "common/model/bundle.h"
#include "common/trinary/flex_trit.h"
#include "utarray.h"

#ifdef __cplusplus
extern "C" {
Expand Down

0 comments on commit ddc16d8

Please sign in to comment.