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

extern cint boolean_True;
extern cint object_Undefined;
extern cint boolean_False;
extern cint strpool_str_32;
extern cint strpool_str_53;


cptr sset_methods[meth_max];

cint sset_alloc(cint sset_self){
  cint sset_result, sset_buf, sset_i;
  cint t1, t3;
  sset_buf = cobarray_mem_malloc((cint)128);
  
  for (sset_i = 0; sset_i <= 31; sset_i++){
    ((cptr)(sset_buf))[sset_i] = 0;
  };t2:;
  t3 = set_alloc((cint)sset_self);
  sset_result = t3;
  ((cptr)sset_self)[5] /* representation*/ = strpool_str_53;
  ((cptr)sset_self)[8] /* elems*/ = sset_buf;
  ((cptr)sset_self)[9] /* max_elems*/ = 32;
  ((cptr)sset_self)[10] /* old_i*/ = 0;
  ((cptr)sset_self)[11] /* old_i_pos*/ = -1;
  t1 = sset_result;
  goto t1_label;
  t1_label: return t1;
}

cint sset_reclaim(cint sset_self){
  cint sset_i;
  cint t4, t6, t7;
  t6 = ((cptr)sset_self)[9] /* max_elems*/ - 1;
  for (sset_i = 0; sset_i <= t6; sset_i++){
    t7 = ((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i] != 0;
    if (t7){
      object_Dec_ref(((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i]);
    };
  };t5:;
  cobarray_mem_free((cint)((cptr)sset_self)[8] /* elems*/);
  set_reclaim((cint)sset_self);
  t4_label: return t4;
}

cint sset_Append(cint sset_self, cint sset_item){
  cint sset_result, sset_r, sset_i;
  cint t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21, t22, t23, t24, t25, t26, t27, t28, t29, t30, t31, t32;
  sset_r = sset_self;
  t9 = ((cptr)(((cptr)sset_self)[0]))[161];
  t10 = ((f0)t9)((cint)sset_self, (cint)sset_item); /* get_pos*/
  sset_i = t10;
  t12 = ((cptr)(((cptr)sset_r)[8] /* elems*/))[sset_i] != 0;
  if (t12){
    object_Dec_ref(sset_item);
    t8 = sset_self;
    goto t8_label;
  };
  t13 = ((cptr)sset_r)[9] /* max_elems*/ / 2;
  t14 = ((cptr)sset_r)[3] /* len*/ >= t13;
  if (t14){
    t15 = ((cptr)(((cptr)sset_self)[0]))[21];
    t16 = ((f0)t15)((cint)sset_self); /* copy_of*/
    sset_r = t16;
    t18 = (cptr)(((cptr)(((cptr)sset_r)[0]))[162]); /* increase_size_and_rehash*/
    ((f0)t18)((cint)sset_r);
    t19 = ((cptr)(((cptr)sset_r)[0]))[161];
    t20 = ((f0)t19)((cint)sset_r, (cint)sset_item); /* get_pos*/
    sset_i = t20;
  };
  t22 = ((cptr)(((cptr)sset_r)[0]))[21];
  t23 = ((f0)t22)((cint)sset_r); /* copy_of*/
  sset_r = t23;
  t25 = ((cptr)sset_r)[2] /* hashval*/ + ((cptr)sset_item)[2] /* hashval*/;
  ((cptr)sset_r)[2] /* hashval*/ = t25;
  ((cptr)(((cptr)sset_r)[8] /* elems*/))[sset_i] = sset_item;
  t26 = ((cptr)sset_r)[3] /* len*/ + 1;
  ((cptr)sset_r)[3] /* len*/ = t26;
  ((cptr)sset_r)[10] /* old_i*/ = 0;
  ((cptr)sset_r)[11] /* old_i_pos*/ = -1;
  t27 = ((cptr)(((cptr)((cptr)sset_r)[7] /* elemtype*/)[0]))[41];
  t28 = ((f0)t27)((cint)((cptr)sset_r)[7] /* elemtype*/, (cint)sset_item); /* Expanded_with*/
  ((cptr)sset_r)[7] /* elemtype*/ = t28;
  t30 = ((cptr)(((cptr)((cptr)sset_r)[6] /* name_space*/)[0]))[56];
  t31 = ((f0)t30)((cint)((cptr)sset_r)[6] /* name_space*/, (cint)((cptr)sset_item)[6] /* name_space*/); /* join*/
  ((cptr)sset_item)[6] /* name_space*/ = t31;
  t8 = sset_r;
  goto t8_label;
  t8_label: return t8;
}

cint sset_Contains(cint sset_self, cint sset_ob){
  cint sset_result, sset_i;
  cint t33, t34, t35, t36, t37;
  t34 = ((cptr)(((cptr)sset_self)[0]))[161];
  t35 = ((f0)t34)((cint)sset_self, (cint)sset_ob); /* get_pos*/
  sset_i = t35;
  sset_result = boolean_False;
  t37 = ((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i] != 0;
  if (t37){
    sset_result = boolean_True;
  };
  object_Dec_ref(sset_self);
  object_Dec_ref(sset_ob);
  t33 = sset_result;
  goto t33_label;
  t33_label: return t33;
}

cint sset_copy_of(cint sset_self){
  cint sset_result, sset_i;
  cint t38, t39, t40, t41, t42, t44, t45;
  t39 = ((cptr)sset_self)[1] /* refcount*/ <= 1;
  if (t39){
    t38 = sset_self;
    goto t38_label;
  };
  t40 = mem_malloc(48); ((cptr)t40)[0] = sset_methods;
  t41 = set_alloc((cint)t40);
  sset_result = t41;
  ((cptr)sset_result)[5] /* representation*/ = strpool_str_53;
  ((cptr)((cptr)sset_self)[7] /* elemtype*/)[1] += 1;
  ((cptr)sset_result)[7] /* elemtype*/ = ((cptr)sset_self)[7] /* elemtype*/;
  ((cptr)sset_result)[6] /* name_space*/ = ((cptr)sset_self)[6] /* name_space*/;
  ((cptr)sset_result)[2] /* hashval*/ = ((cptr)sset_self)[2] /* hashval*/;
  ((cptr)sset_result)[3] /* len*/ = ((cptr)sset_self)[3] /* len*/;
  t42 = ((cptr)sset_self)[9] /* max_elems*/ * 4;
  ((cptr)sset_result)[8] /* elems*/ = cobarray_mem_malloc((cint)t42);
  ((cptr)sset_result)[9] /* max_elems*/ = ((cptr)sset_self)[9] /* max_elems*/;
  ((cptr)sset_result)[10] /* old_i*/ = 0;
  ((cptr)sset_result)[11] /* old_i_pos*/ = -1;
  t44 = ((cptr)sset_self)[9] /* max_elems*/ - 1;
  for (sset_i = 0; sset_i <= t44; sset_i++){
    t45 = ((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i] != 0;
    if (t45){
      ((cptr)((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i])[1] += 1;
      ((cptr)(((cptr)sset_result)[8] /* elems*/))[sset_i] = ((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i];
    }else{
      ((cptr)(((cptr)sset_result)[8] /* elems*/))[sset_i] = 0;
    };
  };t43:;
  object_Dec_ref(sset_self);
  t38 = sset_result;
  goto t38_label;
  t38_label: return t38;
}

cint sset_Delete_ith(cint sset_self, cint sset_i){
  cint sset_result, sset_ith_elem, sset_hash, sset_j;
  cint t46, t47, t48, t49, t50, t51, t52, t53, t54, t55, t56, t59, t60, t61, t62, t63, t64, t65, t66;
  t47 = ((cptr)(((cptr)sset_self)[0]))[21];
  t48 = ((f0)t47)((cint)sset_self); /* copy_of*/
  sset_result = t48;
  t50 = ((cptr)(((cptr)sset_result)[0]))[62];
  t51 = ((f0)t50)((cint)sset_result, (cint)sset_i); /* lookup*/
  sset_ith_elem = t51;
  t53 = ((cptr)sset_result)[3] /* len*/ - 1;
  ((cptr)sset_result)[3] /* len*/ = t53;
  t54 = ((cptr)sset_result)[2] /* hashval*/ - ((cptr)sset_ith_elem)[2] /* hashval*/;
  ((cptr)sset_result)[2] /* hashval*/ = t54;
  sset_hash = cint_mod((cint)((cptr)sset_ith_elem)[2] /* hashval*/, (cint)((cptr)sset_result)[9] /* max_elems*/);
  object_Dec_ref(((cptr)(((cptr)sset_result)[8] /* elems*/))[((cptr)sset_result)[11] /* old_i_pos*/]);
  ((cptr)(((cptr)sset_result)[8] /* elems*/))[((cptr)sset_result)[11] /* old_i_pos*/] = 0;
  t55 = ((cptr)sset_result)[11] /* old_i_pos*/ + 1;
  sset_j = t55;
  t56 = sset_j >= ((cptr)sset_result)[9] /* max_elems*/;
  if (t56){
    sset_j = 0;
  };
  t57:
  t59 = ((cptr)(((cptr)sset_result)[8] /* elems*/))[sset_j] != 0;
  t60 = t59;
  if (t60){
    t61 = cint_mod((cint)((cptr)((cptr)(((cptr)sset_result)[8] /* elems*/))[sset_j])[2] /* hashval*/, (cint)((cptr)sset_result)[9] /* max_elems*/) == sset_hash;
    t60 = t61;
  };
  if (!t60) goto t58;
    t62 = sset_j == 0;
    if (t62){
      t63 = ((cptr)sset_result)[9] /* max_elems*/ - 1;
      ((cptr)(((cptr)sset_result)[8] /* elems*/))[t63] = ((cptr)(((cptr)sset_result)[8] /* elems*/))[sset_j];
    }else{
      t64 = sset_j - 1;
      ((cptr)(((cptr)sset_result)[8] /* elems*/))[t64] = ((cptr)(((cptr)sset_result)[8] /* elems*/))[sset_j];
    };
    ((cptr)(((cptr)sset_result)[8] /* elems*/))[sset_j] = 0;
    t65 = sset_j + 1;
    sset_j = t65;
    t66 = sset_j >= ((cptr)sset_result)[9] /* max_elems*/;
    if (t66){
      sset_j = 0;
    };
  goto t57;
  t58:;
  ((cptr)sset_result)[10] /* old_i*/ = 0;
  ((cptr)sset_result)[11] /* old_i_pos*/ = -1;
  t46 = sset_result;
  goto t46_label;
  t46_label: return t46;
}

cint sset_equal(cint sset_self, cint sset_ob){
  cint sset_result, sset_i;
  cint t67, t68, t69, t70, t71, t73, t74, t75, t76, t77;
  t68 = sset_self == sset_ob;
  if (t68){
    t67 = 1;
    goto t67_label;
  };
  t69 = ((cptr)sset_ob)[4] /* class_of*/ != strpool_str_32;
  if (t69){
    t67 = 0;
    goto t67_label;
  };
  t70 = ((cptr)sset_self)[3] /* len*/ != ((cptr)sset_ob)[3] /* len*/;
  if (t70){
    t67 = 0;
    goto t67_label;
  };
  t71 = ((cptr)sset_self)[2] /* hashval*/ != ((cptr)sset_ob)[2] /* hashval*/;
  if (t71){
    t67 = 0;
    goto t67_label;
  };
  for (sset_i = 1; sset_i <= ((cptr)sset_self)[3] /* len*/; sset_i++){
    t73 = ((cptr)(((cptr)sset_ob)[0]))[62];
    t74 = ((f0)t73)((cint)sset_ob, (cint)sset_i); /* lookup*/
    t75 = ((cptr)(((cptr)sset_self)[0]))[161];
    t76 = ((f0)t75)((cint)sset_self, (cint)t74); /* get_pos*/
    t77 = ((cptr)(((cptr)sset_self)[8] /* elems*/))[t76] == 0;
    if (t77){
      t67 = 0;
      goto t67_label;
    };
  };t72:;
  t67 = 1;
  goto t67_label;
  t67_label: return t67;
}

cint sset_get_pos(cint sset_self, cint sset_item){
  cint sset_result;
  cint t78, t79, t80, t83, t84, t85, t86, t87, t88, t89;
  sset_result = cint_mod((cint)((cptr)sset_item)[2] /* hashval*/, (cint)((cptr)sset_self)[9] /* max_elems*/);
  t81:
  t85 = ((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_result] != 0;
  t86 = t85;
  if (t86){
  t83 = ((cptr)(((cptr)((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_result])[0]))[37];
  t84 = ((f0)t83)((cint)((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_result], (cint)sset_item); /* equal*/
    t87 = t84 == 0;
    t86 = t87;
  };
  if (!t86) goto t82;
    t88 = sset_result + 1;
    sset_result = t88;
    t89 = sset_result >= ((cptr)sset_self)[9] /* max_elems*/;
    if (t89){
      sset_result = 0;
    };
  goto t81;
  t82:;
  t78 = sset_result;
  goto t78_label;
  t78_label: return t78;
}

cint sset_increase_size_and_rehash(cint sset_self){
  cint sset_old_buf, sset_old_max, sset_i;
  cint t90, t91, t92, t93, t94, t96, t98, t99, t100, t101, t102;
  sset_old_buf = ((cptr)sset_self)[8] /* elems*/;
  sset_old_max = ((cptr)sset_self)[9] /* max_elems*/;
  t91 = sset_old_max < 1024;
  if (t91){
    t92 = ((cptr)sset_self)[9] /* max_elems*/ * 2;
    ((cptr)sset_self)[9] /* max_elems*/ = t92;
  }else{
    t93 = ((cptr)sset_self)[9] /* max_elems*/ + 1024;
    ((cptr)sset_self)[9] /* max_elems*/ = t93;
  };
  t94 = ((cptr)sset_self)[9] /* max_elems*/ * 4;
  ((cptr)sset_self)[8] /* elems*/ = cobarray_mem_malloc((cint)t94);
  t96 = ((cptr)sset_self)[9] /* max_elems*/ - 1;
  for (sset_i = 0; sset_i <= t96; sset_i++){
    ((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i] = 0;
  };t95:;
  t98 = sset_old_max - 1;
  for (sset_i = 0; sset_i <= t98; sset_i++){
    t99 = ((cptr)(sset_old_buf))[sset_i] != 0;
    if (t99){
      t100 = ((cptr)(((cptr)sset_self)[0]))[161];
      t101 = ((f0)t100)((cint)sset_self, (cint)((cptr)(sset_old_buf))[sset_i]); /* get_pos*/
      ((cptr)(((cptr)sset_self)[8] /* elems*/))[t101] = ((cptr)(sset_old_buf))[sset_i];
    };
  };t97:;
  cobarray_mem_free((cint)sset_old_buf);
  ((cptr)sset_self)[10] /* old_i*/ = 0;
  ((cptr)sset_self)[11] /* old_i_pos*/ = -1;
  t90_label: return t90;
}

cint sset_Intersect(cint sset_self, cint sset_ob){
  cint sset_result, sset_i;
  cint t103, t104, t105, t106, t107, t108, t109, t110, t111, t112, t113, t114, t116, t117, t118, t119, t120, t121, t122, t123, t124;
  t104 = ((cptr)sset_self)[3] /* len*/ > ((cptr)sset_ob)[3] /* len*/;
  t105 = t104;
  if (t105){
    t106 = ((cptr)sset_ob)[3] /* len*/ > 0;
    t105 = t106;
  };
  t107 = t105;
  if (!t107){    t108 = ((cptr)sset_ob)[4] /* class_of*/ != strpool_str_32;
    t107 = t108;
  };
  if (t107){
    t109 = ((cptr)(((cptr)sset_ob)[0]))[54];
    t110 = ((f0)t109)((cint)sset_ob, (cint)sset_self); /* Intersect*/
    t103 = t110;
    goto t103_label;
  };
  t112 = ((cptr)sset_self)[3] /* len*/ == 0;
  if (t112){
    object_Dec_ref(sset_ob);
    t103 = sset_self;
    goto t103_label;
  };
  t113 = mem_malloc(48); ((cptr)t113)[0] = sset_methods;
  t114 = sset_alloc((cint)t113);
  sset_result = t114;
  t116 = ((cptr)sset_self)[9] /* max_elems*/ - 1;
  for (sset_i = 0; sset_i <= t116; sset_i++){
    t119 = ((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i] != 0;
    t120 = t119;
    if (t120){
    ((cptr)sset_ob)[1] += 1;
    ((cptr)((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i])[1] += 1;
    t117 = ((cptr)(((cptr)sset_ob)[0]))[20];
    t118 = ((f0)t117)((cint)sset_ob, (cint)((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i]); /* Contains*/
      t121 = t118 == boolean_True;
      t120 = t121;
    };
    if (t120){
      ((cptr)((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i])[1] += 1;
      t122 = ((cptr)(((cptr)sset_result)[0]))[7];
      t123 = ((f0)t122)((cint)sset_result, (cint)((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_i]); /* Append*/
      sset_result = t123;
    };
  };t115:;
  object_Dec_ref(sset_self);
  object_Dec_ref(sset_ob);
  t103 = sset_result;
  goto t103_label;
  t103_label: return t103;
}

cint sset_lookup(cint sset_self, cint sset_i){
  cint sset_result, sset_oi, sset_oip;
  cint t125, t126, t127, t128, t129, t132, t133, t134, t137, t138, t141, t142, t143, t146, t147;
  t126 = sset_i < 1;
  t127 = t126;
  if (!t127){    t128 = sset_i > ((cptr)sset_self)[3] /* len*/;
    t127 = t128;
  };
  if (t127){
    t125 = object_Undefined;
    goto t125_label;
  };
  t129 = sset_i == 1;
  if (t129){
    sset_oi = 0;
    sset_oip = -1;
  }else{
    sset_oi = ((cptr)sset_self)[10] /* old_i*/;
    sset_oip = ((cptr)sset_self)[11] /* old_i_pos*/;
  };
  t130:
  t132 = sset_oi < sset_i;
  if (!t132) goto t131;
    t133 = sset_oi + 1;
    sset_oi = t133;
    t134 = sset_oip + 1;
    sset_oip = t134;
    t135:
    t137 = ((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_oip] == 0;
    if (!t137) goto t136;
      t138 = sset_oip + 1;
      sset_oip = t138;
    goto t135;
    t136:;
  goto t130;
  t131:;
  t139:
  t141 = sset_oi > sset_i;
  if (!t141) goto t140;
    t142 = sset_oi - 1;
    sset_oi = t142;
    t143 = sset_oip - 1;
    sset_oip = t143;
    t144:
    t146 = ((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_oip] == 0;
    if (!t146) goto t145;
      t147 = sset_oip - 1;
      sset_oip = t147;
    goto t144;
    t145:;
  goto t139;
  t140:;
  ((cptr)sset_self)[10] /* old_i*/ = sset_oi;
  ((cptr)sset_self)[11] /* old_i_pos*/ = sset_oip;
  t125 = ((cptr)(((cptr)sset_self)[8] /* elems*/))[sset_oip];
  goto t125_label;
  t125_label: return t125;
}

cint sset_Plus(cint sset_self, cint sset_ob){
  cint sset_result, sset_i;
  cint t148, t149, t150, t151, t152, t153, t154, t155, t156, t157, t158, t159, t160, t161, t163, t164, t165, t166, t167, t168;
  t149 = ((cptr)sset_ob)[4] /* class_of*/ != strpool_str_32;
  if (t149){
    object_Dec_ref(sset_self);
    object_Dec_ref(sset_ob);
    t148 = object_Undefined;
    goto t148_label;
  };
  t150 = ((cptr)sset_self)[3] /* len*/ == 0;
  if (t150){
    object_Dec_ref(sset_self);
    t148 = sset_ob;
    goto t148_label;
  };
  t151 = ((cptr)sset_self)[1] /* refcount*/ > 1;
  t152 = t151;
  if (t152){
    t153 = ((cptr)sset_ob)[1] /* refcount*/ == 1;
    t152 = t153;
  };
  t154 = t152;
  if (!t154){    t155 = ((cptr)sset_ob)[5] /* representation*/ != strpool_str_53;
    t154 = t155;
  };
  if (t154){
    t156 = ((cptr)(((cptr)sset_ob)[0]))[97];
    t157 = ((f0)t156)((cint)sset_ob, (cint)sset_self); /* Plus*/
    t148 = t157;
    goto t148_label;
  };
  t159 = ((cptr)(((cptr)sset_self)[0]))[21];
  t160 = ((f0)t159)((cint)sset_self); /* copy_of*/
  sset_result = t160;
  ((cptr)sset_result)[6] /* name_space*/ = object_Undefined;
  for (sset_i = 1; sset_i <= ((cptr)sset_ob)[3] /* len*/; sset_i++){
    t165 = ((cptr)(((cptr)sset_ob)[0]))[62];
    t164 = ((f0)t165)((cint)sset_ob, (cint)sset_i); /* lookup*/
    ((cptr)t164)[1] += 1;
    t166 = ((cptr)(((cptr)sset_result)[0]))[7];
    t167 = ((f0)t166)((cint)sset_result, (cint)t164); /* Append*/
    sset_result = t167;
  };t162:;
  object_Dec_ref(sset_ob);
  t148 = sset_result;
  goto t148_label;
  t148_label: return t148;
}

void init_sset(){
  copy_meth_table(sset_methods, set_methods);
  sset_methods[0] = 48;
  sset_methods[3] = sset_alloc;
  sset_methods[4] = sset_reclaim;
  sset_methods[7] = sset_Append;
  sset_methods[20] = sset_Contains;
  sset_methods[21] = sset_copy_of;
  sset_methods[29] = sset_Delete_ith;
  sset_methods[37] = sset_equal;
  sset_methods[161] = sset_get_pos;
  sset_methods[162] = sset_increase_size_and_rehash;
  sset_methods[54] = sset_Intersect;
  sset_methods[62] = sset_lookup;
  sset_methods[97] = sset_Plus;
}
