C ++ case声明? [英] C++ case declaration?
问题描述
最初我有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。
例如:
如果 au_map.size()= 4
,我需要模拟我的算法16个案件。
for(It_au it = a_map.begin != a_map.end(); it ++)
{
acq_pair it1 = it-> second;
// case 0:
// C3 = 0,C2 = 0,C1 = 0,C0 = 0
//使用相应的C值更新it1.second
//模拟算法
// case 1:
// C3 = 0,C2 = 0,C1 = 0,C0 = 1
//模拟$ b b
.........
// case 15:
// C3 = 1,C2 = 1,C1 = 1,C0 = 1
// simulate
}
em>不是最好的主意。现在你通过手动设置C1-C4并在 c>循环中编写一些仿真程序来完成大量无用的工作。
自动化它
使用一些抽象 State-Simulator
mapper (其中模拟器
实际上代表一些具体的功能对象)。
$ b b
typedef char State;
struct basic_simulator {
//你也可以传递一些其他参数到你的
//模拟器
virtual void operator()(...)= 0
};
struct concrete_simulator:public basic_simulator {
virtual void operator()(...){
// Do something concrete
// Trolololo
}
};
typedef basic_simulator模拟器;
在这种情况下,你的实际包装将看起来像 std :: map<状态,模拟器*> map;
下一步需要做的就是获得C1-C4 从您的州定义为 char
的值。使用按位运算符。
所有状态都可以定义为 0-15
数字转换为二进制$ c> 2 - > 0010 )。因此,要获得
// C1 C2 C3 C4
// -----------
// 1 1 1 1
状态状态= 15;
for(int i = 3; i> = 0; i--){
//由一个位定义的一些输入的状态
InputState C_xx = ((state>> i)& 1);
}
现在只需映射所有 code>状态到适当的模拟函子:
std :: map< State,Simulator *>映射器
//一般来说,你应该使用某种类型的
//这里的智能指针
mapper [0] = new concrete_simulator(...);
mapper [1] = ...
在这种情况下,调用实际模拟将意味着类似于:
//触发适当的模拟物件
(*(mapper [actual_state]))
并使每个可能的模拟手段迭代每个地图元素。
更新:同样的技术可用于有超过 4 个输入/单输入状态的状态
只需编写适当的映射函数和状态生成器。 / p>
I am trying to adapt a digital electronics problem to a C++ STL based program.
Originally I have 4 inputs C1, C2, C3, C4. This means I have a total of 16 combinations:
0000
0001
.
.
.
1111
I have a multimap defined by
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;
The no. of simulations depend on the size of the au_map
.
For eg: if the au_map.size() = 5
I will have C1, C2, C3, C4, C5. and therefore 2^5 = 32 cases.
For example:
If theau_map.size()=4
, I need to simulate my algorithm for 16 cases.
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
}
Not the best idea. Right now you're doing a lot of useless work by manually setting your C1-C4 and by writing some simulation routines right in your for
loop.
Automate it.
Use some abstract State-Simulator
mapper (where Simulator
actually stands for some concrete functional object).
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;
And in this case your actual wrapper would look like std::map<State, Simulator*> map;
What you need to do next do means getting C1-C4 values from your state which is defined as char
. Use bitwise operators.
All your states could be defined as 0-15
numbers converted to binary (2 -> 0010
). So to get C1-C4 values you would simply have to make appropriate shifts:
// 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);
}
Now simply map all those 0-15
states to appropriate simulating functor:
std::map<State, Simulator*> mapper;
// Generally speaking, you should use some sort of
// smart pointer here
mapper[0] = new concrete_simulator(...);
mapper[1] = ...
What is really cool that you could have only, let's say, 3 or four concrete simulators which would be mapped to some states accordingly.
In this case invoking actual simulation would mean something like:
// Fire appropriate simulation object
(*(mapper[actual_state]))(...);
and making every possible simulation means iterating over every map element.
Update: the same technique could be used for states where you have more than 4 inputs / single input state can have more than two possible values.
Just write an appropriate mapping function and state generator.
这篇关于C ++ case声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!