[653073] | 1 | #include "Singular/libsingular.h" |
---|
| 2 | #include <sstream> |
---|
| 3 | #include <unordered_set> |
---|
| 4 | |
---|
| 5 | typedef std::unordered_set<int> si_intset; /* intset is already defined in std*/ |
---|
| 6 | |
---|
| 7 | static int si_intset_type_id = -1; |
---|
| 8 | |
---|
| 9 | void* si_intset_Init(blackbox *b) |
---|
| 10 | { |
---|
| 11 | void *d=new si_intset(); |
---|
| 12 | return (void*)d; |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | void si_intset_destroy(blackbox *b, void *d) |
---|
| 16 | { |
---|
| 17 | if(d!=NULL) |
---|
| 18 | { |
---|
| 19 | si_intset* tobedeleted=(si_intset*) d; |
---|
| 20 | delete tobedeleted; |
---|
| 21 | } |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | char* si_intset_String(blackbox *b, void *d) |
---|
| 25 | { |
---|
| 26 | if(d!=NULL) |
---|
| 27 | { |
---|
| 28 | si_intset *myset; |
---|
| 29 | myset=(si_intset*)d; |
---|
| 30 | std::stringstream out; |
---|
| 31 | StringSetS("{"); |
---|
| 32 | for (si_intset::iterator it = myset->begin(); it != myset->end(); ++it) |
---|
| 33 | { |
---|
| 34 | StringAppend("%d,", *it); |
---|
| 35 | } |
---|
| 36 | StringAppendS("}"); |
---|
| 37 | return(omStrDup(StringEndS())); |
---|
| 38 | } |
---|
| 39 | else |
---|
| 40 | { |
---|
| 41 | return omStrDup("invalid object"); |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | void* si_intset_Copy(blackbox *b, void *d) |
---|
| 46 | { |
---|
| 47 | si_intset *tobecopied= (si_intset*)d; |
---|
| 48 | si_intset *dd=new si_intset(*tobecopied); |
---|
| 49 | return (void*)dd; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | BOOLEAN si_intset_Assign(leftv l, leftv r) |
---|
| 53 | { |
---|
| 54 | if (r->Typ()==l->Typ()) |
---|
| 55 | { |
---|
| 56 | if (l->Data()!=NULL) |
---|
| 57 | { |
---|
| 58 | //si_intset* tobedeleted=(si_intset*) l->Data(); |
---|
| 59 | //delete tobedeleted; |
---|
| 60 | si_intset* tobecleared=(si_intset*) l->Data(); |
---|
| 61 | tobecleared->clear(); |
---|
| 62 | } |
---|
| 63 | si_intset *tobeassigned= (si_intset*)r->CopyD(); |
---|
| 64 | if (l->rtyp==IDHDL) |
---|
| 65 | { |
---|
| 66 | IDDATA((idhdl)l->data)=(char*) tobeassigned; |
---|
| 67 | } |
---|
| 68 | else |
---|
[aad4ca4] | 69 | { |
---|
[653073] | 70 | l->data = (void*) tobeassigned; |
---|
[aad4ca4] | 71 | } |
---|
[653073] | 72 | } |
---|
| 73 | else |
---|
| 74 | { |
---|
| 75 | Werror("assign Type(%d) = Type(%d) not implemented",l->Typ(),r->Typ()); |
---|
| 76 | return TRUE; |
---|
| 77 | } |
---|
| 78 | return FALSE; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | BOOLEAN unite_set (leftv result, leftv arg) |
---|
| 82 | { |
---|
| 83 | if ((arg==NULL) |
---|
| 84 | ||(arg->next==NULL) |
---|
| 85 | ||(arg->Typ()!=si_intset_type_id) |
---|
| 86 | ||(arg->next->Typ()!=si_intset_type_id) |
---|
| 87 | ||(arg->next->next->Typ()!=si_intset_type_id)) |
---|
| 88 | { |
---|
| 89 | WerrorS("syntax: unite_set(<intset>,<intset>,<intset>)"); |
---|
| 90 | return TRUE; |
---|
| 91 | } |
---|
| 92 | si_intset *a=(si_intset*)arg->Data(); |
---|
| 93 | si_intset *b=(si_intset*)arg->next->Data(); |
---|
| 94 | si_intset *c=(si_intset*)arg->next->next->Data(); |
---|
| 95 | c->clear(); |
---|
| 96 | for (si_intset::iterator it = a->begin(); it != a->end(); ++it) |
---|
| 97 | { |
---|
| 98 | c->insert(*it); |
---|
| 99 | } |
---|
| 100 | for (si_intset::iterator it = b->begin(); it != b->end(); ++it) |
---|
| 101 | { |
---|
| 102 | c->insert(*it); |
---|
| 103 | } |
---|
| 104 | result->rtyp= NONE; |
---|
| 105 | result->data= NULL; |
---|
| 106 | return FALSE; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | BOOLEAN intersect_set (leftv result, leftv arg) |
---|
| 110 | { |
---|
| 111 | if ((arg==NULL) |
---|
| 112 | ||(arg->next==NULL) |
---|
| 113 | ||(arg->Typ()!=si_intset_type_id) |
---|
| 114 | ||(arg->next->Typ()!=si_intset_type_id) |
---|
| 115 | ||(arg->next->next->Typ()!=si_intset_type_id)) |
---|
| 116 | { |
---|
| 117 | WerrorS("syntax: intersect_set(<intset>,<intset>,<intset>)"); |
---|
[aad4ca4] | 118 | return TRUE; |
---|
[653073] | 119 | } |
---|
| 120 | si_intset *a=(si_intset*)arg->Data(); |
---|
| 121 | si_intset *b=(si_intset*)arg->next->Data(); |
---|
| 122 | si_intset *c=(si_intset*)arg->next->next->Data(); |
---|
| 123 | c->clear(); |
---|
| 124 | if (b->size() < a->size()) |
---|
| 125 | { |
---|
| 126 | for (si_intset::const_iterator it = b->begin(); it != b->end(); it++) |
---|
| 127 | { |
---|
| 128 | if (a->find(*it) != a->end()) |
---|
| 129 | c->insert(*it); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | else |
---|
| 133 | { |
---|
| 134 | for (si_intset::const_iterator it = a->begin(); it != a->end(); it++) |
---|
| 135 | { |
---|
| 136 | if (b->find(*it) != b->end()) |
---|
| 137 | c->insert(*it); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | result->rtyp=NONE; |
---|
| 141 | result->data= NULL; |
---|
| 142 | return FALSE; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | BOOLEAN insert_set (leftv result, leftv arg) |
---|
| 146 | { |
---|
| 147 | if ((arg==NULL) |
---|
| 148 | ||(arg->next==NULL) |
---|
| 149 | ||(arg->Typ()!=si_intset_type_id) |
---|
| 150 | ||(arg->next->Typ()!=INT_CMD)) |
---|
| 151 | { |
---|
[aad4ca4] | 152 | WerrorS("syntax: insert_set(<intset>,<int>)"); |
---|
[653073] | 153 | return TRUE; |
---|
| 154 | } |
---|
| 155 | si_intset *a=(si_intset*)arg->Data(); |
---|
| 156 | int b=(int)(long)arg->next->Data(); |
---|
| 157 | a->insert(b); |
---|
[aad4ca4] | 158 | result->rtyp=NONE; |
---|
[653073] | 159 | result->data=NULL; |
---|
| 160 | return FALSE; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | BOOLEAN equal_set (leftv result, leftv arg) |
---|
| 164 | { |
---|
| 165 | if ((arg==NULL) |
---|
| 166 | ||(arg->next==NULL) |
---|
| 167 | ||(arg->Typ()!=si_intset_type_id) |
---|
| 168 | ||(arg->next->Typ()!=si_intset_type_id)) |
---|
| 169 | { |
---|
| 170 | WerrorS("syntax: equal_set(<intset>,<intset>)"); |
---|
| 171 | return TRUE; |
---|
| 172 | } |
---|
| 173 | si_intset *a=(si_intset*)arg->Data(); |
---|
| 174 | si_intset *b=(si_intset*)arg->next->Data(); |
---|
| 175 | for (si_intset::const_iterator it = b->begin(); it != b->end(); it++) |
---|
| 176 | { |
---|
| 177 | if (a->find(*it) != a->end()) |
---|
| 178 | { |
---|
| 179 | result->data= (void *) (FALSE); |
---|
| 180 | result->rtyp= INT_CMD; |
---|
| 181 | return FALSE; |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | for (si_intset::const_iterator it = a->begin(); it != a->end(); it++) |
---|
| 185 | { |
---|
| 186 | if (b->find(*it) != b->end()) |
---|
| 187 | { |
---|
| 188 | result->data= (void *) (FALSE); |
---|
| 189 | result->rtyp= INT_CMD; |
---|
| 190 | return FALSE; |
---|
| 191 | } |
---|
| 192 | } |
---|
| 193 | result->data= (void *) (TRUE); |
---|
| 194 | result->rtyp= INT_CMD; |
---|
| 195 | return FALSE; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | extern "C" int mod_init(SModulFunctions* psModulFunctions) |
---|
| 199 | { |
---|
| 200 | blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox)); |
---|
| 201 | b->blackbox_Init=si_intset_Init; |
---|
| 202 | b->blackbox_destroy=si_intset_destroy; |
---|
| 203 | b->blackbox_Copy=si_intset_Copy; |
---|
| 204 | b->blackbox_String=si_intset_String; |
---|
| 205 | b->blackbox_Assign=si_intset_Assign; |
---|
| 206 | si_intset_type_id=setBlackboxStuff(b,"intset"); |
---|
| 207 | psModulFunctions->iiAddCproc((currPack->libname? currPack->libname: ""),"unite_set",FALSE,unite_set); |
---|
| 208 | psModulFunctions->iiAddCproc((currPack->libname? currPack->libname: ""),"intersect_set",FALSE,intersect_set); |
---|
| 209 | psModulFunctions->iiAddCproc((currPack->libname? currPack->libname: ""),"insert_set",FALSE,insert_set); |
---|
| 210 | psModulFunctions->iiAddCproc((currPack->libname? currPack->libname: ""),"equal_set",FALSE,equal_set); |
---|
| 211 | return MAX_TOK; |
---|
| 212 | } |
---|