C 中的函数泛化
function generalization in C
我对 C 还很陌生
我写了下面的代码。并且以下所有功能几乎相同。我只想知道有没有一种方法可以将以下功能简化为通用功能
struct tensor add(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] + t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
struct tensor sub(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] - t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
struct tensor mul(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] * t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
还有更多的功能。每个运算符一个,格式如下
struct tensor genericfunc(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] ?? t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
#define definefunc(name, op) \
Tensor name(Tensor t1, Tensor t2) { \
int data[t1.size]; \
for (int i = 0; i < t1.size; ++i) \
data[i] = t1.data[i] op t2.data[i]; \
\
return Tensor(t1.rank, t1.shape, data); \
}
definefunc(add, +)
definefunc(sub, -)
definefunc(mul, *)
或
typedef int (*TensorOp)(int, int)
Tensor genericfunc(Tensor t1, Tensor t2, TensorOp op) {
int data[t1.size];
for (int i = 0; i < t1.size; ++i)
data[i] = op(t1.data[i], t2.data[i]);
return Tensor(t1.rank, t1.shape, data);
}
int TensorOp_add(int a, int b) { return a + b; }
int TensorOp_sub(int a, int b) { return a - b; }
int TensorOp_mul(int a, int b) { return a * b; }
Tensor add(Tensor t1, Tensor t2) { return genericfunc(t1, t2, TensorOp_add); }
Tensor sub(Tensor t1, Tensor t2) { return genericfunc(t1, t2, TensorOp_sub); }
Tensor mul(Tensor t1, Tensor t2) { return genericfunc(t1, t2, TensorOp_mul); }
以上代码片段假定以下内容以避免在任何地方使用 struct
:
typedef struct tensor Tensor;
老实说,这听起来像是一个不需要修复的问题。
但你可以这样做:
struct tensor genericfunc(struct tensor t1, char op, struct tensor t2) {
int data[t1.size];
int i = 0;
switch(op) {
case '+': while (i < t1.size) { data[i] = t1.data[i] + t2.data[i]; i++; } break;
// Implement the rest of the ops here
}
return Tensor(t1.rank, t1.shape, data);
}
你可以在你的参数上添加决定因素
struct tensor add(struct tensor t1, struct tensor t2,int decider){
if(decider==1){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] + t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
if(decider ==2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] - t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
if (decider ==3){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] * t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
好吧,我希望这就是你想要的
我对 C 还很陌生
我写了下面的代码。并且以下所有功能几乎相同。我只想知道有没有一种方法可以将以下功能简化为通用功能
struct tensor add(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] + t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
struct tensor sub(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] - t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
struct tensor mul(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] * t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
还有更多的功能。每个运算符一个,格式如下
struct tensor genericfunc(struct tensor t1, struct tensor t2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] ?? t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
#define definefunc(name, op) \
Tensor name(Tensor t1, Tensor t2) { \
int data[t1.size]; \
for (int i = 0; i < t1.size; ++i) \
data[i] = t1.data[i] op t2.data[i]; \
\
return Tensor(t1.rank, t1.shape, data); \
}
definefunc(add, +)
definefunc(sub, -)
definefunc(mul, *)
或
typedef int (*TensorOp)(int, int)
Tensor genericfunc(Tensor t1, Tensor t2, TensorOp op) {
int data[t1.size];
for (int i = 0; i < t1.size; ++i)
data[i] = op(t1.data[i], t2.data[i]);
return Tensor(t1.rank, t1.shape, data);
}
int TensorOp_add(int a, int b) { return a + b; }
int TensorOp_sub(int a, int b) { return a - b; }
int TensorOp_mul(int a, int b) { return a * b; }
Tensor add(Tensor t1, Tensor t2) { return genericfunc(t1, t2, TensorOp_add); }
Tensor sub(Tensor t1, Tensor t2) { return genericfunc(t1, t2, TensorOp_sub); }
Tensor mul(Tensor t1, Tensor t2) { return genericfunc(t1, t2, TensorOp_mul); }
以上代码片段假定以下内容以避免在任何地方使用 struct
:
typedef struct tensor Tensor;
老实说,这听起来像是一个不需要修复的问题。
但你可以这样做:
struct tensor genericfunc(struct tensor t1, char op, struct tensor t2) {
int data[t1.size];
int i = 0;
switch(op) {
case '+': while (i < t1.size) { data[i] = t1.data[i] + t2.data[i]; i++; } break;
// Implement the rest of the ops here
}
return Tensor(t1.rank, t1.shape, data);
}
你可以在你的参数上添加决定因素
struct tensor add(struct tensor t1, struct tensor t2,int decider){
if(decider==1){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] + t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
if(decider ==2){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] - t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
if (decider ==3){
int data[t1.size];
int i = 0;
while (i < t1.size) { data[i] = t1.data[i] * t2.data[i]; i++; }
return Tensor(t1.rank, t1.shape, data);
}
好吧,我希望这就是你想要的