Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EHでの二分探索をbsearchで書き直した #134

Merged
merged 6 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
conv_to_active = {0: "INACTIVE", 1: "ACTIVE"}
conv_to_condition_type = {0: "SINGLE", 1: "CONTINUOUS", 2:"CUMULATIVE"}

# TODO: カウンタのクリアを入れたら足す
# TODO: EH対応EL発行を入れたら,色々変えないとダメ

# 各種設定の確認など
@pytest.mark.real
Expand Down Expand Up @@ -583,6 +585,9 @@ def test_event_handler_respond_single():
assert check_respend_eh() == "not_responded"


# TODO: 複数同時発火のテストを入れる


@pytest.mark.real
@pytest.mark.sils
def test_event_handler_respond_continuous():
Expand Down
109 changes: 58 additions & 51 deletions System/EventManager/event_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
#include "event_handler.h"
#include <string.h>
#include <stdlib.h>
#include "../../CmdTlm/common_tlm_cmd_packet_util.h"
#include "../../Applications/timeline_command_dispatcher.h"
#include "../TimeManager/time_manager.h"
Expand Down Expand Up @@ -51,8 +52,7 @@ typedef enum
EH_RULE_SORTED_INDEX_ACK_NOT_FOUND, //!< ���‚��炸
EH_RULE_SORTED_INDEX_ACK_FULL, //!< ����ȏ�o�^�ł��Ȃ�
EH_RULE_SORTED_INDEX_ACK_RULE_OVERWRITE, //!< ���[���̏㏑���ɂȂ��Ă��܂��i���łɓ��� ID �Ƀ��[�����o�^����Ă��邽�ߊ��p�j
EH_RULE_SORTED_INDEX_ACK_DUPLICATE_FULL, //!< �d������܂ŏd�����Ă��܂��Ă���
EH_RULE_SORTED_INDEX_ACK_UNKNOWN_ERR //!< �s���ȃG���[
EH_RULE_SORTED_INDEX_ACK_DUPLICATE_FULL //!< �d������܂ŏd�����Ă��܂��Ă���
} EH_RULE_SORTED_INDEX_ACK;

/**
Expand Down Expand Up @@ -169,7 +169,6 @@ const EL_Event* EH_get_oldest_event_(void);
* @param[out] found_sorted_idxes: ���‚����� EH_RuleSortedIndex �� index
* @param[out] found_id_num: ���‚����� idx �̐�
* @retval EH_RULE_SORTED_INDEX_ACK_NOT_FOUND: ���‚��炸
* @retval EH_RULE_SORTED_INDEX_ACK_UNKNOWN_ERR: �s���ȃG���[
* @retval EH_RULE_SORTED_INDEX_ACK_OK: ����ɒT������
*/
static EH_RULE_SORTED_INDEX_ACK EH_search_rule_table_index_(EL_GROUP group,
Expand All @@ -178,6 +177,16 @@ static EH_RULE_SORTED_INDEX_ACK EH_search_rule_table_index_(EL_GROUP group,
uint16_t found_sorted_idxes[EH_MAX_RULE_NUM_OF_EL_ID_DUPLICATES],
uint8_t* found_id_num);

/**
* @brief bsearch �p�� EH_RuleSortedIndex ��r�֐�
* @param[in] key: bsearch �Ō������� EH_RuleSortedIndex
* @param[in] elem: bsearch �����Ώۂ� EH_RuleSortedIndex �z��v�f
* @retval 1: key > elem
* @retval 0: key == elem
* @retval -1: key < elem
*/
static int EH_compare_sorted_index_for_bsearch_(const void* key, const void* elem);

/**
* @brief EH_Rule �� EH_RuleTable �� EH_RuleSortedIndex �ɑ}������
* @note ������ rule �Ɋւ��Ă̓A�T�[�V�����ς݂����肷��
Expand Down Expand Up @@ -602,83 +611,81 @@ static EH_RULE_SORTED_INDEX_ACK EH_search_rule_table_index_(EL_GROUP group,
uint8_t* found_id_num)
{
// idx: 0 ~ rule_table.registered_rule_num �̊Ԃœ񕪒T������
// �d�������蓾�邱�Ƃ��l������
// �d�������蓾�邱�Ƃ��l������ (duplicate_id �͈قȂ�)

int32_t lower, upper; // �v�Z�̓s����Cupper �� -1 �ɂȂ邱�Ƃ�����̂ŁC�����t������
uint16_t found_idx = EH_RULE_MAX;
EH_RuleSortedIndex* p_searched_sorted_idx = NULL;
const EH_RuleSortedIndex target_sorted_idx = { group, local, 0, (EH_RULE_ID)0 };
meltingrabbit marked this conversation as resolved.
Show resolved Hide resolved
int i = 0;

if (event_handler_.rule_table.registered_rule_num == 0)
{
return EH_RULE_SORTED_INDEX_ACK_NOT_FOUND;
}

lower = 0;
upper = event_handler_.rule_table.registered_rule_num - 1; // ��r���i��܁j�͈�
p_searched_sorted_idx = (EH_RuleSortedIndex*)bsearch(&target_sorted_idx,
event_handler_.sorted_idxes,
EH_RULE_MAX,
sizeof(EH_RuleSortedIndex),
EH_compare_sorted_index_for_bsearch_);
if (p_searched_sorted_idx == NULL) return EH_RULE_SORTED_INDEX_ACK_NOT_FOUND;
found_idx = (uint16_t)(p_searched_sorted_idx - (&event_handler_.sorted_idxes[0]));
yngyu marked this conversation as resolved.
Show resolved Hide resolved

// �������[�v����̂��߂� while �ł͂Ȃ� for
for (i = 0; i < EH_RULE_MAX; ++i)
// ���‚������D��́C�����‚��邩�H
*found_id_num = 0;
for (i = 0; i < EH_MAX_RULE_NUM_OF_EL_ID_DUPLICATES; ++i)
{
int32_t mid = (lower + upper) / 2;
EH_RuleSortedIndex* p_mid = &event_handler_.sorted_idxes[mid];

if (lower > upper)
EH_RuleSortedIndex* p_idx = &event_handler_.sorted_idxes[found_idx + i];
if ( !(p_idx->group == group && p_idx->local == local) )
{
return EH_RULE_SORTED_INDEX_ACK_NOT_FOUND;
break;
}
found_sorted_idxes[i] = (uint16_t)(found_idx + i);
found_ids[i] = p_idx->rule_id;
(*found_id_num)++;
}

return EH_RULE_SORTED_INDEX_ACK_OK;
}


static int EH_compare_sorted_index_for_bsearch_(const void* key, const void* elem)
{
const EH_RuleSortedIndex* p_key = (const EH_RuleSortedIndex*)key;
const EH_RuleSortedIndex* p_elem = (const EH_RuleSortedIndex*)elem;

if (p_mid->group == group)
if (p_elem->group == p_key->group)
{
if (p_elem->local == p_key->local)
{
if (p_mid->local == local)
{
if (p_mid->duplicate_id == 0)
{
// ���‚������I�I�i�d���̏ꍇ�͍ł� duplicate_id �����������́j
found_idx = (uint16_t)mid;
break;
}
else
{
// ���Ⴂ duplicate_id ������͂�
upper = mid - 1;
}
}
else if (p_mid->local < local)
if (p_elem->duplicate_id == 0)
{
lower = mid + 1;
// �T��������
return 0;
}
else
{
upper = mid - 1;
// �����Ǝ�O�ɂ���͂�
return -1;
}
}
else if (p_mid->group < group)
else if (p_elem->local < p_key->local)
{
lower = mid + 1;
return 1;
}
else
{
upper = mid - 1;
return -1;
}
}

if (found_idx == EH_RULE_MAX) return EH_RULE_SORTED_INDEX_ACK_UNKNOWN_ERR;

// ���‚������D��́C�����‚��邩�H
*found_id_num = 0;
for (i = 0; i < EH_MAX_RULE_NUM_OF_EL_ID_DUPLICATES; ++i)
else if (p_elem->group < p_key->group)
{
EH_RuleSortedIndex* p_idx = &event_handler_.sorted_idxes[found_idx + i];
if ( !(p_idx->group == group && p_idx->local == local) )
{
break;
}
found_sorted_idxes[i] = (uint16_t)(found_idx + i);
found_ids[i] = p_idx->rule_id;
(*found_id_num)++;
return 1;
}
else
{
return -1;
}

return EH_RULE_SORTED_INDEX_ACK_OK;
}


Expand Down