123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // // ******************************************************************
- // // /\ /| @File commonhelper.cc
- // // \ V/ @Brief
- // // | "") @Author lijinwen, ghz005@uni-trend.com.cn
- // // / | @Creation 2024-05-31
- // // / \\ @Modified 2024-06-24
- // // *(__\_\
- // // ******************************************************************
- #include "common_helper.h"
- namespace Protocol
- {
- // 移除数组中第一个元素,如果它与其他元素的差异最大
- std::vector<int32_t> CommonHelper::RemoveFirstIfMostDifferent(const std::vector<int32_t>& array)
- {
- if (array.size() < 2)
- {
- throw std::invalid_argument("Array must contain at least two elements.");
- }
- const int32_t first_value = array[0];
- int32_t max_difference = 0;
- int32_t most_different_index = 0;
- // 找到与第一个元素差异最大的元素
- for (int32_t i = 1; i < static_cast<int32_t>(array.size()); ++i)
- {
- int32_t difference = std::abs(array[i] - first_value);
- if (difference > max_difference)
- {
- max_difference = difference;
- most_different_index = i;
- }
- }
- // 如果第一个元素与其他元素差异最大,则移除第一个元素
- if (most_different_index == 0)
- {
- return std::vector<int32_t>(array.begin() + 1, array.end());
- }
- return array; // 如果第一个元素不是差异最大的,则返回原数组
- }
- bool CommonHelper::CheckTwoLevelEdgePulseValid(TwoLevelEdgePulse* node)
- {
- return node != nullptr && node->start_index >= 0 && node->end_index > 0
- && node->LevelCount() > 0 && node->current_level != TwoLevelEdgePulseStatusType::None;
- }
- uint8_t CommonHelper::SwapHighLowBits(const uint8_t value)
- {
- // 获取低4位
- const uint8_t lower_nibble = value & 0x0F;
- // 获取高4位
- const uint8_t higher_nibble = (value >> 4) & 0x0F;
- // 交换高低4位
- return static_cast<uint8_t>(higher_nibble | lower_nibble << 4);
- }
- uint8_t CommonHelper::ReverseOrderBits(uint8_t value)
- {
- value = SwapHighLowBits(value); //高低互换
- value = static_cast<uint8_t>((value & 0xCC) >> 2 | (value & 0x33) << 2); //交换每对相邻的位
- value = static_cast<uint8_t>((value & 0xAA) >> 1 | (value & 0x55) << 1); //交换每对相邻的位
- return value;
- }
- bool CommonHelper::GetNodeByIndex(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s)
- {
- if (index < 0 || node == nullptr) return false;
- if (index >= node->start_index && index <= node->end_index) return true;
- TwoLevelEdgePulse* temp_node = node;
- if (index > temp_node->end_index)
- {
- const auto start_time = std::chrono::steady_clock::now();
- while (CheckTwoLevelEdgePulseValid(temp_node))
- {
- if (index >= temp_node->start_index && index <= temp_node->end_index)
- {
- node = temp_node;
- return true;
- }
- temp_node++;
- if (auto end_time = std::chrono::steady_clock::now();
- std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time).count() > time_out_by_s)
- {
- return false;
- }
- }
- }
- else if (index < temp_node->start_index)
- {
- const auto start_time = std::chrono::steady_clock::now();
- while (CheckTwoLevelEdgePulseValid(temp_node))
- {
- if (index >= temp_node->start_index && index <= temp_node->end_index)
- {
- node = temp_node;
- return true;
- }
- temp_node--;
- if (auto end_time = std::chrono::steady_clock::now();
- std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time).count() > time_out_by_s)
- {
- return false;
- }
- }
- }
- return false;
- }
- bool CommonHelper::FindNextNode(const int32_t index, TwoLevelEdgePulse*& node, const int32_t time_out_by_s)
- {
- if (index < 0)
- {
- return false;
- }
- if (GetNodeByIndex(index, node))
- {
- node++;
- if (CheckTwoLevelEdgePulseValid(node))
- {
- return true;
- }
- }
- /*while (CheckTwoLevelEdgePulseValid(node) && index< node->start_index)
- {
- node++;
- if (node->start_index > index)
- {
- break;
- }
- }
- node++;
- if (CheckTwoLevelEdgePulseValid(node) && node->start_index > index)
- {
- return true;
- }*/
- return false;
- }
- }
|