Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Explain why we have 2 set libraries in SG
[simgrid.git] / src / xbt / swag.c
index 9cc8cec..4b4fdd3 100644 (file)
@@ -1,7 +1,9 @@
-/* Authors: Arnaud Legrand                                                  */
+/*     $Id$     */
+
+/* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
 
 /* This program is free software; you can redistribute it and/or modify it
  under the terms of the license (GNU LGPL) which comes with this package. */
* under the terms of the license (GNU LGPL) which comes with this package. */
 
 /* Warning, this module is done to be efficient and performs tons of
    cast and dirty things. So avoid using it unless you really know
 #include "xbt/error.h"
 #include "xbt/swag.h"
 
+/** \defgroup XBT_swag A O(1) set datatype
+ *  \brief a O(1) set based on linked lists
+ *
+ *  Warning, this module is done to be efficient and performs tons of
+ *  cast and dirty things. So avoid using it unless you really know
+ *  what you are doing. It is basically a fifo but with restrictions so that 
+ *  it can be used as a set. Any operation (add, remove, belongs) is O(1) and 
+ *  no call to malloc/free is done.
+ */
+
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(swag,xbt,"Swag : O(1) set library");
+
 #define PREV(obj,offset) xbt_swag_getPrev(obj,offset)
 #define NEXT(obj,offset) xbt_swag_getNext(obj,offset)
 
-/*
-  Usage : xbt_swag_new(&obj.setA-&obj.setA);
+/** \name Functions 
+ *  \ingroup XBT_swag
  */
+/*@{*/
 
+/** Creates a new swag.
+ * \param offset where the hookup is located in the structure
+ * \see xbt_swag_offset
+ *
+ * Usage : xbt_swag_new(&obj.setA-&obj); 
+ */
 xbt_swag_t xbt_swag_new(size_t offset)
 {
   xbt_swag_t swag = xbt_new0(s_xbt_swag_t, 1);
@@ -29,11 +50,36 @@ xbt_swag_t xbt_swag_new(size_t offset)
   return swag;
 }
 
+/** 
+ * \param swag poor victim
+ * 
+ * kilkil a swag but not it's content. If you do not understand why
+ * xbt_swag_free should not free its content, don't use swags.
+ */
+void xbt_swag_free(xbt_swag_t swag)
+{
+  xbt_free(swag);
+}
+
+/** Creates a new swag.
+ * \param swag the swag to initialize
+ * \param offset where the hookup is located in the structure
+ * \see xbt_swag_offset
+ *
+ * Usage : xbt_swag_init(swag,&obj.setA-&obj); 
+ */
 void xbt_swag_init(xbt_swag_t swag, size_t offset)
 {
   swag->offset = offset;
 }
 
+
+/** 
+ * \param obj the objet to insert in the swag
+ * \param swag a swag
+ *
+ * insert \a obj in \a swag
+ */
 void xbt_swag_insert(void *obj, xbt_swag_t swag)
 {
 
@@ -42,6 +88,61 @@ void xbt_swag_insert(void *obj, xbt_swag_t swag)
 
   (swag->count)++;
   if (swag->head == NULL) {
+    xbt_assert0(!(swag->tail), "Inconsistent swag.");
+    swag->head = obj;
+    swag->tail = obj;
+    return;
+  }
+
+  PREV(obj, swag->offset) = swag->tail;
+  NEXT(PREV(obj, swag->offset), swag->offset) = obj;
+
+  swag->tail = obj;
+}
+
+/** 
+ * \param obj the objet to insert in the swag
+ * \param swag a swag
+ *
+ * insert (at the head... you probably had a very good reason to do
+ * that, I hope you know what you're doing) \a obj in \a swag
+ */
+void xbt_swag_insert_at_head(void *obj, xbt_swag_t swag)
+{
+
+  if (xbt_swag_belongs(obj, swag))
+    return;
+
+  (swag->count)++;
+  if (swag->head == NULL) {
+    xbt_assert0(!(swag->tail), "Inconsistent swag.");
+    swag->head = obj;
+    swag->tail = obj;
+    return;
+  }
+
+  NEXT(obj, swag->offset) = swag->head;
+  PREV(NEXT(obj, swag->offset), swag->offset) = obj;
+
+  swag->head = obj;
+}
+
+/** 
+ * \param obj the objet to insert in the swag
+ * \param swag a swag
+ *
+ * insert (at the tail... you probably had a very good reason to do
+ * that, I hope you know what you're doing) \a obj in \a swag
+ */
+void xbt_swag_insert_at_tail(void *obj, xbt_swag_t swag)
+{
+
+  if (xbt_swag_belongs(obj, swag))
+    return;
+
+  (swag->count)++;
+  if (swag->head == NULL) {
+    xbt_assert0(!(swag->tail), "Inconsistent swag.");
     swag->head = obj;
     swag->tail = obj;
     return;
@@ -53,6 +154,13 @@ void xbt_swag_insert(void *obj, xbt_swag_t swag)
   swag->tail = obj;
 }
 
+/** 
+ * \param obj the objet to remove from the swag
+ * \param swag a swag
+ * \return \a obj if it was in the \a swag and NULL otherwise
+ *
+ * removes \a obj from \a swag
+ */
 void *xbt_swag_remove(void *obj, xbt_swag_t swag)
 {
   size_t offset = swag->offset;
@@ -68,6 +176,7 @@ void *xbt_swag_remove(void *obj, xbt_swag_t swag)
       return NULL;
     swag->head = NULL;
     swag->tail = NULL;
+    NEXT(obj, offset) = PREV(obj, offset) = NULL;
   } else if (obj == swag->head) {      /* It's the head */
     swag->head = NEXT(obj, offset);
     PREV(swag->head, offset) = NULL;
@@ -85,6 +194,10 @@ void *xbt_swag_remove(void *obj, xbt_swag_t swag)
   return obj;
 }
 
+/** 
+ * \param swag a swag
+ * \return an object from the \a swag
+ */
 void *xbt_swag_extract(xbt_swag_t swag)
 {
   size_t offset = swag->offset;
@@ -97,22 +210,33 @@ void *xbt_swag_extract(xbt_swag_t swag)
 
   if (swag->head == swag->tail) {      /* special case */
     swag->head = swag->tail = NULL;
+    PREV(obj, offset) = NEXT(obj, offset) = NULL;
   } else {
     swag->head = NEXT(obj, offset);
     PREV(swag->head, offset) = NULL;
     NEXT(obj, offset) = NULL;
   }
+  (swag->count)--;
 
   return obj;
 }
-
+/** 
+ * \param swag a swag
+ * \return the number of objects in \a swag
+ */
 int xbt_swag_size(xbt_swag_t swag)
 {
   return (swag->count);
 }
 
+/** 
+ * \param obj an object
+ * \param swag a swag
+ * \return 1 if \a obj is in the \a swag and 0 otherwise
+ */
 int xbt_swag_belongs(void *obj, xbt_swag_t swag)
 {
   return ((NEXT(obj, swag->offset)) || (PREV(obj, swag->offset))
          || (swag->head == obj));
 }
+/*@}*/