00001 #ifndef SET_IMPLEMENTATION
00002 #define SET_IMPLEMENTATION
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "array.cpp"
00019 #include "guards.h"
00020 #include "set.h"
00021
00022 namespace basis {
00023
00024 template <class contents>
00025 bool set<contents>::member(const contents &to_test) const
00026 {
00027 for (int i = 0; i < elements(); i++)
00028 if (to_test == this->get(i))
00029 return true;
00030 return false;
00031 }
00032
00033 template <class contents>
00034 bool set<contents>::add(const contents &to_add)
00035 {
00036 if (this->member(to_add)) return false;
00037 this->concatenate(to_add);
00038 return true;
00039 }
00040
00041 template <class contents>
00042 int set<contents>::find(const contents &to_find) const
00043 {
00044 for (int i = 0; i < elements(); i++)
00045 if (to_find == this->get(i))
00046 return i;
00047 return common::NOT_FOUND;
00048 }
00049
00050 template <class contents>
00051 bool set<contents>::remove(const contents &to_remove)
00052 {
00053 for (int i = 0; i < elements(); i++)
00054 if (to_remove == this->get(i)) {
00055 this->zap(i, i);
00056 return true;
00057 }
00058 return false;
00059 }
00060
00061 template <class contents>
00062 set<contents> set<contents>::intersection(const set &intersect_with) const
00063 {
00064 set<contents> created(0, NIL, this->flags());
00065 const set *smaller = this;
00066 const set *larger = &intersect_with;
00067 if (this->elements() > intersect_with.elements()) {
00068
00069 smaller = &intersect_with;
00070 larger = this;
00071 }
00072 for (int i = 0; i < smaller->length(); i++)
00073 if (larger->member(smaller->get(i)))
00074 created.concatenate(smaller->get(i));
00075 return created;
00076 }
00077
00078 template <class contents>
00079 set<contents> set<contents>::set_union(const set &union_with) const
00080 {
00081 set<contents> created = *this;
00082 for (int i = 0; i < union_with.elements(); i++)
00083 created.add(union_with.get(i));
00084 return created;
00085 }
00086
00087 template <class contents>
00088 void set<contents>::unionize(const set &union_with)
00089 {
00090 for (int i = 0; i < union_with.elements(); i++)
00091 this->add(union_with.get(i));
00092 }
00093
00094 template <class contents>
00095 set<contents> set<contents>::difference(const set &differ_with) const
00096 {
00097 set<contents> created = *this;
00098 for (int i = 0; i < differ_with.elements(); i++) {
00099 if (created.member(differ_with[i]))
00100 created.remove(differ_with[i]);
00101 }
00102 return created;
00103 }
00104
00105 template <class contents>
00106 void set<contents>::differentiate(const set &differ_with)
00107 {
00108 for (int i = 0; i < differ_with.elements(); i++) {
00109 if (this->member(differ_with[i]))
00110 this->remove(differ_with[i]);
00111 }
00112 }
00113
00114 }
00115
00116 #endif
00117