我正在努力使数字电子设备问题适应基于C ++ STL的程序。
最初我有4个输入C1,C2,C3,C4。这意味着我总共有16种组合:
0000
0001
.
.
.
1111
我有一个由
定义的多图typedef std::pair<int, int> au_pair; //vertices
typedef std::pair<int, int> acq_pair; //ch qlty
typedef std::multimap<int, acq_pair> au_map;
typedef au_map::iterator It_au;
没有。模拟的大小取决于au_map
的大小。
例如:如果au_map.size() = 5
我将拥有C1,C2,C3,C4,C5。因此2 ^ 5 = 32例。
例如:
如果是au_map.size()=4
,我需要为16个案例模拟我的算法。
for(It_au it = a_map.begin(); it != a_map.end(); it++)
{
acq_pair it1 = it->second;
//case 0:
//C3 = 0, C2 = 0, C1 = 0, C0 = 0
//Update it1.second with corresponding C values
//simulate algorithm
//case 1:
//C3 = 0, C2 = 0, C1 = 0, C0 = 1
//simulate
.........
//case 15:
//C3 = 1, C2 = 1, C1 = 1, C0 = 1
//simulate
}
答案 0 :(得分:1)
不是最好的主意。现在你通过手动设置C1-C4并在for
循环中编写一些模拟程序来做很多无用的工作。
自动化。
使用一些抽象的State-Simulator
映射器(其中Simulator
实际上代表一些具体的功能对象)。
typedef char State;
struct basic_simulator {
// You could also pass some other parameters to your
// simulator
virtual void operator()(...) = 0
};
struct concrete_simulator : public basic_simulator {
virtual void operator()(...) {
// Do something concrete
// Trolololo
}
};
typedef basic_simulator Simulator;
在这种情况下,您的实际包装器看起来像std::map<State, Simulator*> map;
接下来需要做的是从您的州获取 C1-C4 值,定义为char
。使用按位运算符。
您的所有状态都可以定义为转换为二进制(0-15
)的2 -> 0010
个数字。因此,要获得 C1-C4 值,您只需进行适当的转换:
// C1 C2 C3 C4
// -----------
// 1 1 1 1
State state = 15;
for (int i = 3; i >= 0; i--) {
// State of some of the inputs, defined by one bit
InputState C_xx = ((state >> i) & 1);
}
现在只需将所有0-15
个状态映射到适当的模拟仿函数:
std::map<State, Simulator*> mapper;
// Generally speaking, you should use some sort of
// smart pointer here
mapper[0] = new concrete_simulator(...);
mapper[1] = ...
你可能只有3个或4个混凝土模拟器可以实现真正的酷,这些模拟器会相应地映射到某些状态。
在这种情况下,调用实际模拟意味着:
// Fire appropriate simulation object
(*(mapper[actual_state]))(...);
并进行所有可能的模拟意味着迭代每个地图元素。
更新:相同的技术可用于您有超过 4 输入/单输入状态可能具有两个可能值的状态。< / em>的
只需编写适当的映射函数和状态生成器。
答案 1 :(得分:0)
嗯......为什么不让for
循环枚举各种组合?
for (size_t i = 0; i != 16; ++i)
{
bool const c1 = i & 1;
bool const c2 = i & 2;
bool const c3 = i & 4;
bool const c4 = i & 8;
// your algorithm
}
比手工设置容易一些。