#include "cdefs.h"
#include "mem.h"
extern cptr object_methods[meth_max];
extern cptr set_methods[meth_max];
extern cptr map_methods[meth_max];
extern cptr boolean_methods[meth_max];
#include "cint.h"
#include "cobarray.h"

extern cint boolean_True;
extern cint object_Undefined;
extern cint boolean_False;


cptr dset_methods[meth_max];

cint dset_alloc(cint dset_self, cint dset_dm){
  cint dset_result;
  cint t1, t2;
  t2 = set_alloc((cint)dset_self);
  dset_result = t2;
  ((cptr)dset_self)[8] /* dmap*/ = dset_dm;
  ((cptr)dset_self)[3] /* len*/ = ((cptr)dset_dm)[3] /* len*/;
  ((cptr)dset_self)[9] /* old_i*/ = 0;
  ((cptr)dset_self)[10] /* old_i_pos*/ = -1;
  ((cptr)dset_self)[2] /* hashval*/ = ((cptr)dset_dm)[2] /* hashval*/;
  t1 = dset_result;
  goto t1_label;
  t1_label: return t1;
}

cint dset_reclaim(cint dset_self){
  cint t3;
  object_Dec_ref(((cptr)dset_self)[8] /* dmap*/);
  set_reclaim((cint)dset_self);
  t3_label: return t3;
}

cint dset_Contains(cint dset_self, cint dset_ob){
  cint dset_result, dset_i;
  cint t4, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18;
  dset_i = cint_mod((cint)((cptr)dset_ob)[2] /* hashval*/, (cint)((cptr)((cptr)dset_self)[8] /* dmap*/)[9] /* max_elems*/);
  t5:
  t7 = ((cptr)(((cptr)((cptr)dset_self)[8] /* dmap*/)[10] /* x*/))[dset_i] != 0;
  if (!t7) goto t6;
    t8 = ((cptr)(((cptr)((cptr)(((cptr)((cptr)dset_self)[8] /* dmap*/)[10] /* x*/))[dset_i])[0]))[62];
    t9 = ((f0)t8)((cint)((cptr)(((cptr)((cptr)dset_self)[8] /* dmap*/)[10] /* x*/))[dset_i], (cint)1); /* lookup*/
    t11 = ((cptr)(((cptr)t9)[0]))[37];
    t12 = ((f0)t11)((cint)t9, (cint)dset_ob); /* equal*/
    t13 = t12 == 1;
    if (t13){
      t4 = boolean_True;
      goto t4_label;
    };
    t14 = ((cptr)(((cptr)((cptr)(((cptr)((cptr)dset_self)[8] /* dmap*/)[10] /* x*/))[dset_i])[0]))[37];
    t15 = ((f0)t14)((cint)((cptr)(((cptr)((cptr)dset_self)[8] /* dmap*/)[10] /* x*/))[dset_i], (cint)dset_ob); /* equal*/
    t16 = t15 == 1;
    if (t16){
      t4 = boolean_True;
      goto t4_label;
    };
    t17 = dset_i + 1;
    dset_i = t17;
    t18 = dset_i >= ((cptr)((cptr)dset_self)[8] /* dmap*/)[9] /* max_elems*/;
    if (t18){
      dset_i = 0;
    };
  goto t5;
  t6:;
  t4 = boolean_False;
  goto t4_label;
  t4_label: return t4;
}

cint dset_copy_of(cint dset_self){
  cint dset_result;
  cint t19, t20, t21, t22;
  t20 = ((cptr)dset_self)[1] /* refcount*/ == 1;
  if (t20){
    t19 = dset_self;
    goto t19_label;
  };
  object_Dec_ref(dset_self);
  t21 = mem_malloc(44); ((cptr)t21)[0] = dset_methods;
  ((cptr)((cptr)dset_self)[8] /* dmap*/)[1] += 1;
  t22 = dset_alloc((cint)t21, (cint)((cptr)dset_self)[8] /* dmap*/);
  t19 = t22;
  goto t19_label;
  t19_label: return t19;
}

cint dset_lookup(cint dset_self, cint dset_i){
  cint dset_result, dset_oi, dset_oip;
  cint t23, t24, t25, t26, t27, t30, t31, t32, t35, t36, t39, t40, t41, t44, t45, t46, t47, t48, t49;
  t24 = dset_i < 1;
  t25 = t24;
  if (!t25){    t26 = dset_i > ((cptr)dset_self)[3] /* len*/;
    t25 = t26;
  };
  if (t25){
    t23 = object_Undefined;
    goto t23_label;
  };
  t27 = dset_i == 1;
  if (t27){
    dset_oi = 0;
    dset_oip = -1;
  }else{
    dset_oi = ((cptr)dset_self)[9] /* old_i*/;
    dset_oip = ((cptr)dset_self)[10] /* old_i_pos*/;
  };
  t28:
  t30 = dset_oi < dset_i;
  if (!t30) goto t29;
    t31 = dset_oi + 1;
    dset_oi = t31;
    t32 = dset_oip + 1;
    dset_oip = t32;
    t33:
    t35 = ((cptr)(((cptr)((cptr)dset_self)[8] /* dmap*/)[10] /* x*/))[dset_oip] == 0;
    if (!t35) goto t34;
      t36 = dset_oip + 1;
      dset_oip = t36;
    goto t33;
    t34:;
  goto t28;
  t29:;
  t37:
  t39 = dset_oi > dset_i;
  if (!t39) goto t38;
    t40 = dset_oi - 1;
    dset_oi = t40;
    t41 = dset_oip - 1;
    dset_oip = t41;
    t42:
    t44 = ((cptr)(((cptr)((cptr)dset_self)[8] /* dmap*/)[10] /* x*/))[dset_oip] == 0;
    if (!t44) goto t43;
      t45 = dset_oip - 1;
      dset_oip = t45;
    goto t42;
    t43:;
  goto t37;
  t38:;
  ((cptr)dset_self)[9] /* old_i*/ = dset_oi;
  ((cptr)dset_self)[10] /* old_i_pos*/ = dset_oip;
  dset_result = ((cptr)(((cptr)((cptr)dset_self)[8] /* dmap*/)[10] /* x*/))[dset_oip];
  t46 = ((cptr)((cptr)dset_self)[8] /* dmap*/)[7] /* domains*/ != 0;
  if (t46){
    t47 = ((cptr)(((cptr)dset_result)[0]))[62];
    t48 = ((f0)t47)((cint)dset_result, (cint)1); /* lookup*/
    dset_result = t48;
  };
  t23 = dset_result;
  goto t23_label;
  t23_label: return t23;
}

void init_dset(){
  copy_meth_table(dset_methods, set_methods);
  dset_methods[0] = 44;
  dset_methods[3] = dset_alloc;
  dset_methods[4] = dset_reclaim;
  dset_methods[20] = dset_Contains;
  dset_methods[21] = dset_copy_of;
  dset_methods[62] = dset_lookup;
}
