Permalink
Browse files

set

  • Loading branch information...
1 parent 58c9059 commit 7dce6c7bb923cf7a4a80586a11d4a0ca87452e64 @cloudwu committed Apr 25, 2011
Showing with 145 additions and 0 deletions.
  1. +4 −0 src/core/seq.h
  2. +74 −0 src/core/set.c
  3. +29 −0 src/core/set.h
  4. +38 −0 src/test/set.c
View
@@ -0,0 +1,4 @@
+#ifndef windsoul_seq_h
+#define windsoul_seq_h
+
+#endif
View
@@ -0,0 +1,74 @@
+#include "set.h"
+#include "map.h"
+
+#include <stdint.h>
+
+struct set;
+
+#define MAP(a) ((struct map *)a)
+#define SET(a) ((struct set *)a)
+
+struct set*
+setCreate(void)
+{
+ return SET(mapCreate());
+}
+
+void
+setRelease(struct set *s)
+{
+ mapRelease(MAP(s));
+}
+
+int
+setOperate(struct set *s, void *e, int op)
+{
+ struct map_op mop;
+ switch (op) {
+ case SET_EXIST:
+ mop.op = MAP_SEARCH;
+ mop.key.p = e;
+ return (intptr_t)mapSearch(MAP(s),&mop);
+ case SET_PUSH: {
+ mop.op = MAP_SLOT;
+ mop.key.p = e;
+ intptr_t * v = (intptr_t *)mapSearch(MAP(s), &mop);
+ int ret = (int) *v;
+ ++ *v;
+ return ret;
+ }
+ case SET_POP: {
+ mop.op = MAP_SLOT;
+ mop.key.p = e;
+ intptr_t * v = (intptr_t *)mapSearch(MAP(s), &mop);
+ int ret = (int) *v;
+ if (ret>0) {
+ -- *v;
+ }
+ return ret - 1;
+ }
+ case SET_PUSHONCE: {
+ mop.op = MAP_SLOT;
+ mop.key.p = e;
+ intptr_t * v = (intptr_t *)mapSearch(MAP(s), &mop);
+ if (*v == 0) {
+ *v = 1;
+ return 0;
+ }
+ return (int)*v;
+ }
+ default:
+ return 0;
+ }
+}
+
+void*
+setTraverse(struct set *s, void ** state)
+{
+ struct map_op mop;
+ mop.op = MAP_TRAVERSE;
+ mop.value = *state;
+ mapSearch(MAP(s), &mop);
+ *state = mop.value;
+ return mop.key.p;
+}
View
@@ -0,0 +1,29 @@
+#ifndef windsoul_set_h
+#define windsoul_set_h
+
+struct set;
+
+#define SET_EXIST 0
+#define SET_PUSH 1
+#define SET_POP 2
+#define SET_PUSHONCE 3
+
+struct set* setCreate(void);
+void setRelease(struct set *);
+
+/*
+ SET_EXIST
+ return 0 : not existed
+ return 1+ : reference count
+ SET_PUSH
+ return 0+ : reference count before push
+ SET_POP
+ return 0+ : reference count after pop
+ SET_PUSHONCE : call push only when not exited
+ return 0+ : reference count before push
+ */
+int setOperate(struct set *, void *, int op);
+
+void* setTraverse(struct set *, void ** state);
+
+#endif
View
@@ -0,0 +1,38 @@
+#include <stdio.h>
+#include "set.h"
+#include "atom.h"
+
+int _setInit(void);
+int _atomInit(void);
+
+static void
+test()
+{
+ struct set *s = setCreate();
+ setOperate(s,atomLiteral("Hello"),SET_PUSHONCE);
+ setOperate(s,atomLiteral("World"),SET_PUSHONCE);
+ setOperate(s,atomLiteral("Hello"),SET_PUSHONCE);
+ setOperate(s,atomLiteral("Foobar"),SET_PUSHONCE);
+ setOperate(s,atomLiteral("Foobar"),SET_POP);
+
+ void *i = NULL;
+
+ do {
+ const char * v = setTraverse(s,&i);
+ printf("%s\n",v);
+ } while(i);
+
+
+ setRelease(s);
+}
+
+int
+main()
+{
+ if (_setInit()) return 1;
+ if (_atomInit()) return 1;
+
+ test();
+
+ return 0;
+}

0 comments on commit 7dce6c7

Please sign in to comment.