tuple.c

来自「压缩包里面的都是精致的基本C语言小程序」· C语言 代码 · 共 100 行

C
100
字号
#include <stdio.h>#include "mystdlib.h"#include "error.h"#include "tuple.h"static int flag = 1;struct tuple{  tyVft vft;    poly x;  poly y;};struct tyVft tupleVft;tuple newTuple (poly x, poly y){  if (flag)  {    flag--;    tupleVft.equals = tupleEquals;    tupleVft.getPlist = tupleGetPlist;    tupleVft.hashCode = tupleHashCode;    tupleVft.toString = tupleToString;  }    tuple t = checkedMalloc (sizeof (*t));  t->vft = &tupleVft;    t->x = x;  t->y = y;  return t;}poly tupleFirst (tuple t){  return t->x;}poly tupleSecond (tuple t){  return t->y;}int tupleEquals (poly t11, poly t12){  tuple t1 = (tuple)t11;  tuple t2 = (tuple)t12;    tyEquals eqx = getVft (t1->x)->equals;  tyEquals eqy = getVft (t1->y)->equals;  return (eqx (t1->x, t2->x) && eqy (t1->y, t2->y));}int tupleEquals2 (tuple t1, tuple t2, tyEquals eqx, tyEquals eqy){  return (eqx (t1->x, t2->x) && eqy (t1->y, t2->y));}str tupleToString (poly t1){  tuple t = (tuple)t1;    tyToString strx = getVft (t->x)->toString;  tyToString stry = getVft (t->y)->toString;  str s1 = newStr ("(");  str s2 = strx (t->x);  str s3 = newStr (", ");  str s4 = stry (t->y);  str s5 = newStr (")");  // free all these stuff?  return strConcat (s1, strConcat (s2, strConcat (s3, strConcat (s4, s5))));}plist tupleGetPlist (poly t){  exception ("not implemented yet\n");  return 0;}int tupleHashCode (poly t1){  tuple t = (tuple)t1;    tyHashCode h1 = getVft (t->x)->hashCode;  tyHashCode h2 = getVft (t->y)->hashCode;    int i1 = h1 (t->x);  int i2 = h2 (t->y);    return i1+i2;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?