在另一个帖子中,我被告知在速度和紧凑性方面switch可能比查找表更好.
所以我想了解这个之间的区别:
static void func1(){}
static void func2(){}
typedef enum
{
FUNC1,
FUNC2,
FUNC_COUNT
} state_e;
typedef void (*func_t)(void);
const func_t lookUpTable[FUNC_COUNT] =
{
[FUNC1] = &func1,
[FUNC2] = &func2
};
void fsm(state_e state)
{
if (state < FUNC_COUNT)
lookUpTable[state]();
else
;// Error handling
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
static void func1(){}
static void func2(){}
void fsm(int state)
{
switch(state)
{
case FUNC1: func1(); break;
case FUNC2: func2(); break;
default: ;// Error handling
}
}
Run Code Online (Sandbox Code Playgroud)
我认为查找表更快,因为编译器尝试在可能的情况下将switch语句转换为跳转表.既然这可能是错的,我想知道为什么!
谢谢你的帮助!
我有如下定义的查找表,我正在使用GCC.当我编译时,我收到警告
warning: braces around scalar initializer
Run Code Online (Sandbox Code Playgroud)
这个警告意味着什么?我该如何初始化这个LUT?我在初始化这个结构时犯了错误吗?
救命!!
typedef struct TECH
{
float velocity1, velocity2;
float temp;
float measure;
int id;
float storage[64];
}TECH;
struct TECH lut_model_1[2] = {{{296.001465},
{74.216972},
{2.025908},
{1.516384},
{1},
{0.001746,
0.000256, 0.006216, 0.005249, -0.001668, -0.001377, 0.009865, 0.010454, -0.000288, -0.005853, 0.010584, 0.015440, 0.000465, -0.000602, 0.004330, 0.005700, 0.017120,
0.233015, 0.034154, 0.244022, 0.007644, 0.385683, 0.042960, 0.406633, -0.007811, 0.346931, 0.040123, 0.387361, 0.007030, 0.225309, 0.017897, 0.241024, 0.003700,
0.103601, 0.060748, 0.121059, -0.045041, 0.076974, 0.070647, 0.148810, -0.022399, 0.074007, 0.054797, 0.141794, 0.010376, 0.052482, 0.045013, …Run Code Online (Sandbox Code Playgroud) 在我生成的数组中,我有一个图像读入numpy中有相当多的像素.
我计算了一个包含256个值的查找表.现在我想做以下事情:
for i in image.rows:
for j in image.cols:
mapped_image[i,j] = lut[image[i,j]]
Run Code Online (Sandbox Code Playgroud)
是的,这基本上就像一个人.
唯一的问题是:我想做到高效并在python中调用该循环将让我等待几秒钟才能完成.
我知道numpy.vectorize(),它只是一个调用相同python代码的便捷函数.
是否有任何良好的网络服务为国家和州/省提供良好的查询信息?
如果是这样你用的是什么?
我有以下TSQL表变量:
declare @NumDaysMonth table
(
month_id smallint,
num_days smallint
)
Run Code Online (Sandbox Code Playgroud)
我只想快速查看每个月的天数.如何像C数组一样初始化此表:
int numDaysMonth[] = {31, 28, 30, ... , 31};
Run Code Online (Sandbox Code Playgroud) 考虑具有以下签名的查找函数,该签名需要返回给定字符串键的整数:
int GetValue(string key) { ... }
Run Code Online (Sandbox Code Playgroud)
此外,在编写函数的源代码时,事先知道编号为N的键值映射,例如:
// N=3
{ "foo", 1 },
{ "bar", 42 },
{ "bazz", 314159 }
Run Code Online (Sandbox Code Playgroud)
因此,上述输入的函数的有效(但不完美!)实现将是:
int GetValue(string key)
{
switch (key)
{
case "foo": return 1;
case "bar": return 42;
case "bazz": return 314159;
}
// Doesn't matter what we do here, control will never come to this point
throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)
事先还知道每个给定密钥在运行时调用该函数的次数(C> = 1).例如:
C["foo"] = 1;
C["bar"] = 1;
C["bazz"] = 2;
Run Code Online (Sandbox Code Playgroud)
但是,此类呼叫的顺序尚不清楚.例如,上面可以描述运行时的以下调用序列:
GetValue("foo");
GetValue("bazz");
GetValue("bar");
GetValue("bazz"); …Run Code Online (Sandbox Code Playgroud) 我想知道是否可以将lambda存放在某个容器中,例如.ArrayList或HashMap.我想更改该代码:
public enum OPCODE implements BinaryOperator<Integer> {
MOV((x, y) -> y),
INC((x, y) -> ++x),
DEC((x, y) -> --x),
ADD((x, y) -> x + y),
SUB((x, y) -> x - y);
private final BinaryOperator<Integer> binaryOperator;
OPCODE(BinaryOperator<Integer> binaryOperator) {
this.binaryOperator = binaryOperator;
}
@Override
public Integer apply(Integer integer, Integer integer2) {
return binaryOperator.apply(integer, integer2);
}
}
Run Code Online (Sandbox Code Playgroud)
对于这样的事情:
List<BinaryOperator<Integer>> opcodes = new ArrayList<BinaryOperator<Integer>>(){
((x, y) -> y),
((x, y) -> ++x)
};
Run Code Online (Sandbox Code Playgroud)
等等
并像这样使用它:
opcodes[0].apply(a, b);
Run Code Online (Sandbox Code Playgroud)
甚至可能吗?
假设我没有超过一两个具有不同属性的对象,例如:
UID,名称,值,颜色,类型,位置
我希望能够使用Location ="Boston"或Type ="Primary"调用所有对象.经典的数据库查询类型.
大多数表解决方案(pytables,*sql)对于这么小的数据集来说真的太过分了.我应该简单地迭代所有对象并为每个数据列创建一个单独的字典(在添加新对象时向字典添加值)?
这将创建像这样的dicts:
{'Boston':[234,654,234],'Chicago':[324,765,342]} - 这些3位数的条目代表UID之类的东西.
如你所见,查询这将是一个痛苦.
对替代方案的任何想法?
我想计算一个32位CRC查找表.我试过的一种方法是使用本网站的以下代码:
#include <iostream>
#include <stdint.h>
void make_crc_table()
{
unsigned long POLYNOMIAL = 0x04c11db7;
unsigned long WIDTH = 8 * sizeof(unsigned long);
unsigned long TOPBIT = 1 << (WIDTH - 1);
unsigned long crcTable[256];
unsigned long remainder;
// Compute the remainder of each possible dividend
for (int dividend = 0; dividend < 256; ++dividend)
{
// Start with the dividend followed by zeros
remainder = dividend << (WIDTH - 8);
// Perform modulo-2 division, a bit at a time …Run Code Online (Sandbox Code Playgroud) 我正在开发一个嵌入式C项目.我有一个LCD显示屏,每个字符有一个5x7点阵.要显示特定字符,您必须移动5个字节,与要打开的点相关联.所以我需要使用一个键来制作某种查找表,我可以传入一个ASCII字符,然后返回一个包含5个字节的数组...例如,调用这个函数就像这样,
GetDisplayBytes('A');
应该返回`这样的数组......
C[0] = 0x7E : C[1] = 0x90 : C[2] = 0x90 : C[3] = 0x90 : C[4] = 0x7E
在C中这样做的最佳方法是什么?
lookup-tables ×10
c ×4
arrays ×1
c++ ×1
containers ×1
crc32 ×1
embedded ×1
gcc-warning ×1
hash ×1
java-8 ×1
lambda ×1
numpy ×1
perfect-hash ×1
performance ×1
python ×1
python-2.x ×1
sql ×1
t-sql ×1
web-services ×1