Ejemplo n.º 1
0
DetailWindow::DetailWindow(std::vector<size_t> data, QWidget* parent) : QWidget(parent)
{
	setupUi(this);
	size_t nEntries = data.size();
	std::vector< std::pair<float, float> > list_data(nEntries);

	for (size_t i=0; i<nEntries; i++)
		list_data.push_back(std::pair<float, float>(static_cast<float>(i), static_cast<float>(data[i])));

	DiagramList* list = new DiagramList();
	list->setList(list_data);
	list->setXUnit("Value");
	list->setYUnit("Amount");
	list->setName("Histogram");
	stationView->setRenderHints( QPainter::Antialiasing );
	stationView->addGraph(list);
	resizeWindow();
}
Ejemplo n.º 2
0
int chtbl_remove(CHTbl *htbl, void **data) {
    ListElmt *element, *prev;
    int bucket;
    bucket = htbl->h(*data, htbl->buckets) % htbl->buckets;
    prev = NULL;
    for (element = list_head(&htbl->table[bucket]); element != NULL; element = list_next(element)) {
        if (htbl->match(*data, list_data(element))) {
            if (list_rem_next(&htbl->table[bucket], prev, data) == 0) {
                htbl->size--;
                return 0;
            } else {
                return -1;
            }
        }
        prev = element;
    }
    return -1;
}
Ejemplo n.º 3
0
int set_remove(Set *set, void **data)
{
    ListElmt *member, *prev;

    prev = NULL;

    for (member = list_head(set); member != NULL; member = list_next(member))
    {
        if (set->match(*data, list_data(member)))
            break;
        prev = member;
    }

    if (member == NULL)
        return -1;

    return list_rem_next(set, prev, data);
}
Ejemplo n.º 4
0
Archivo: route.c Proyecto: whatot/ma_c
/* route */
int route(List * paths, PathVertex * destination, PathVertex ** next,
	  int (*match) (const void *key1, const void *key2))
{
	PathVertex *temp, *parent;
	ListElmt *element;
	int found;

	/* Locate the destination in the list of gateways. */
	found = 0;

	for (element = list_head(paths); element != NULL; element =
	     list_next(element)) {
		if (match(list_data(element), destination)) {
			temp = (PathVertex *)list_data(element);
			parent = ((PathVertex *) list_data(element))->parent;
			found = 1;
			break;
		}
	}

	/* Return if the destination is not reachable. */
	if (!found) {
		return -1;
	}

	/* Compute the next gateway in the shortest path to the destination.*/
	while (parent != NULL) {

		temp = (PathVertex *)list_data(element);
		found = 0;

		for (element = list_head(paths); element != NULL; element =
		     list_next(element)) {

			if (match(list_data(element), parent)) {
				parent = ((PathVertex *)
					  list_data(element))->parent;
				found = 1;
				break;
			}
		}
		/* Return if the destination is not reachable. */
		if (!found) {
			return -1;
		}
	}

	*next = temp;

	return 0;
}
Ejemplo n.º 5
0
Region& Region::operator=(const Region& other)
{
	if (&other == this)
		return *this;

	// Create a copy of the list and its contents
	list_destroy(rect_list);
	list_init(rect_list, __kfree);

	ListElement* cur_node = list_head(other.rect_list);
	while (cur_node != NULL) {
		list_insert_next(rect_list, NULL,
			new Rect(*((Rect *)list_data(cur_node))));

		cur_node = cur_node->next;
	}

	return *this;
}
Ejemplo n.º 6
0
/* Find a job, given its ID number. Sets <head> to the list containing it. */
static List * job_find(unsigned int id, List ***head)
{
	List	**list, *iter;

	if(IS_PERIODIC(id))
		list = &cron_info.periodic;
	else
		list = &cron_info.oneshot;

	for(iter = *list; iter != NULL; iter = list_next(iter))
	{
		if(((Job *) list_data(iter))->id == id)
			break;
	}
	if(head)
		*head = list;

	return iter;
}
Ejemplo n.º 7
0
/*--------------------------------------------------------------*/
void create_path_list(List *dijkstra, SPElement *path) {
	ListElmt *e;
	PathVertex *p;
	
	for (e = list_head(dijkstra); e != NULL; e = list_next(e)) {
		
		p = (PathVertex*) list_data(e);
		
		path[ p->index ].c.x = ((CoordData*)(p->data))->x;
		path[ p->index ].c.y = ((CoordData*)(p->data))->y;
		path[ p->index ].c.z = ((CoordData*)(p->data))->z;
		path[ p->index ].d   = p->d;
		
		if (p->parent != NULL)
			path[ p->index ].parent = ((PathVertex*)(p->parent))->index;
		else
			path[ p->index ].parent = -1;
	}
}
int set_is_member(const Set * set, const void *data)
{

    ListElmt *member;

    /*****************************************************************************
    *  Determine if the data is a member of the set.                             *
    *****************************************************************************/

    for (member = list_head(set); member != NULL;
            member = list_next(member)) {

        if (set->match(data, list_data(member)))
            return 1;

    }

    return 0;

}
Ejemplo n.º 9
0
void
print_lineno(const List *list) {

	ListElmt *element;
	Loc *tmp_loc;
	int i = 0;

	element = list_head(list);

	for(; i < list_size(list) ; i++) {
		tmp_loc = list_data(element);
		if(i != 0) 
			printf(", ");
		
		printf("%d", tmp_loc->line_no);
		element = list_next(element);
	};
	printf("\n");
	return;
};
Ejemplo n.º 10
0
void
print_formatedlist(const List *list) {

	ListElmt *element;
	Loc *tmp_loc;
	int i = 0;

	element = list_head(list);

	for(; i < list_size(list) ; i++) {
		tmp_loc = list_data(element);
		printf("%s:%s, %s; ", 
				tmp_loc->ftype,
				tmp_loc->fname,
				tmp_loc->type);
		element = list_next(element);
	};
	printf("\n");
	return;
};
Ejemplo n.º 11
0
//set_remove
int set_remove(Set *set, void **data)
{
    ListElmt *member, *prev;

    //find the member to remove.
    prev = NULL;

    for (member = list_head(set); member != NULL; member = list_next(member))
    {
        if (set->match(*data, list_data(member)))
            break;
        prev = member;
    }
    // Return if the member was not found.
    if (member == NULL)
        return -1;

    //remove the member.
    return list_rem_next(set, prev, data);
}
Ejemplo n.º 12
0
/*
* Dispatch one chunk of speech to espeak.
* Guarded by queue_guard_mutex in the dispatch thread
*/
int send_speech(void)
{
	espeak_ERROR erc;
	TTS_QUEUE_ENTRY_T *qe;
	ListElmt *element;

	if (queue_size(&tts_queue) < 1)
		return -1;

	if ( (element = malloc(sizeof(ListElmt))) == NULL)
		return -1;

	queue_pop(&tts_queue, (void*)element);
	qe = (TTS_QUEUE_ENTRY_T*)list_data(element);

	erc = espeak_Synth(qe->speech, qe->length+1, 0, POS_CHARACTER, 0, SYNTH_FLAGS, NULL, st);

	free(qe->speech);
	free(element);
	return 0;
} /* send_speech */
Ejemplo n.º 13
0
Archivo: ui.c Proyecto: hbkk/sequencer
static int
ui_send_pcopy(ui_t *ui, int from_pattern, int to_pattern)
{
    list_t *notes;
    note_t *note = NULL;

    if (from_pattern == to_pattern) {
        return 1;
    }

    ui_send_clear(ui, to_pattern);

    for (notes = ui->patterns[from_pattern]; notes; notes = list_next(notes)) {
        note = (note_t *)list_data(notes);

        ui_send_add(ui, note->key, note->vel, note->step, note->len,
                to_pattern);
    }

    return 1;
}
Ejemplo n.º 14
0
void test_list_find_data(void)
{
	int entries[] = { 89, 23, 42, 16, 15, 4, 8, 99, 50, 30 };
	int num_entries = sizeof(entries) / sizeof(int);
	ListEntry *list;
	ListEntry *result;
	int i;
	int val;
	int *data;

	/* Generate a list containing the entries */

	list = NULL;
	for (i=0; i<num_entries; ++i) {
		assert(list_append(&list, &entries[i]) != NULL);
	}

	/* Check that each value can be searched for correctly */

	for (i=0; i<num_entries; ++i) {

		val = entries[i];

		result = list_find_data(list, int_equal, &val);

		assert(result != NULL);

		data = (int *) list_data(result);
		assert(*data == val);
	}

	/* Check some invalid values return NULL */

	val = 0;
	assert(list_find_data(list, int_equal, &val) == NULL);
	val = 56;
	assert(list_find_data(list, int_equal, &val) == NULL);

	list_free(list);
}
Ejemplo n.º 15
0
/* assume A or B is a t_LIST */
static GEN
listconcat(GEN A, GEN B)
{
  long i, l1, lx;
  GEN L, z, L1, L2;

  if (typ(A) != t_LIST) {
    if (list_typ(B)!=t_LIST_RAW) pari_err_TYPE("listconcat",B);
    L2 = list_data(B);
    if (!L2) return mklistcopy(A);
    lx = lg(L2) + 1;
    z = listcreate();
    list_data(z) = L = cgetg(lx, t_VEC);
    for (i = 2; i < lx; i++) gel(L,i) = gcopy(gel(L2,i-1));
    gel(L,1) = gcopy(A); return z;
  } else if (typ(B) != t_LIST) {
    if (list_typ(A)!=t_LIST_RAW) pari_err_TYPE("listconcat",A);
    L1 = list_data(A);
    if (!L1) return mklistcopy(B);
    lx = lg(L1) + 1;
    z = listcreate();
    list_data(z) = L = cgetg(lx, t_VEC);
    for (i = 1; i < lx-1; i++) gel(L,i) = gcopy(gel(L1,i));
    gel(L,i) = gcopy(B); return z;
  }
  /* A, B both t_LISTs */
  if (list_typ(A)!=t_LIST_RAW) pari_err_TYPE("listconcat",A);
  if (list_typ(B)!=t_LIST_RAW) pari_err_TYPE("listconcat",B);
  L1 = list_data(A); if (!L1) return listcopy(B);
  L2 = list_data(B); if (!L2) return listcopy(A);

  l1 = lg(L1);
  lx = l1-1 + lg(L2);
  z = cgetg(3, t_LIST);
  z[1] = 0UL;
  list_data(z) = L = cgetg(lx, t_VEC);
  L2 -= l1-1;
  for (i=1; i<l1; i++) gel(L,i) = gclone(gel(L1,i));
  for (   ; i<lx; i++) gel(L,i) = gclone(gel(L2,i));
  return z;
}
Ejemplo n.º 16
0
int set_remove(Set *set, void **data) {

    ListElem *member, *prev;

    //find member to remove
    prev = NULL;

    for (member = list_head(set); member != NULL; member = list_next(member)) {

        if (set->match(*data, list_data(member))) {
            break;
        }

        prev = member;
    }

    //member not found
    if (member == NULL) {
        return -1;
    }

    return list_rem_next(set, prev, data);
};
Ejemplo n.º 17
0
void* search_in_list(List *list, char *hash){
   ListElmt           *element;
   void *res;
   struct dalvik_hook_t *dh;
   element = list_head(list);

   while (1) {

   dh = list_data(element);

   if(strncmp(element->hashvalue,hash,sizeof(hash)) == 0 ) {
      return dh;
   }

   if (list_is_tail(element))
      break;
   else
      element = list_next(element);

   }
   return;

}
Ejemplo n.º 18
0
Archivo: ui.c Proyecto: hbkk/sequencer
static int
ui_send_kcopy(ui_t *ui, int from_pattern, int to_pattern, int from_key,
        int to_key)
{
    list_t *notes;
    note_t *note = NULL;

    if (from_pattern == to_pattern && from_key == to_key) {
        return 1;
    }

    ui_send_delete(ui, to_key, to_pattern);

    for (notes = ui->patterns[from_pattern]; notes; notes = list_next(notes)) {
        note = (note_t *)list_data(notes);
        if (note->key == from_key) {
            ui_send_add(ui, to_key, note->vel, note->step, note->len,
                    to_pattern);
        }
    }

    return 1;
}
Ejemplo n.º 19
0
Archivo: list-1.c Proyecto: whatot/ma_c
static void print_list(const List *list) {
	ListElmt *element;
	int *data;
	int i;

	fprintf(stdout, "List size is %d\n", list_size(list));

	i = 0;
	element = list_head(list);
	while (1) {
		data = list_data(element);
		fprintf(stdout, "list[%03d]=%03d\n", i, *data);
		
		i++;

		if (list_is_tail(element)) {
			break;
		} else {
			element = list_next(element);
		}
	}
	return;
}
Ejemplo n.º 20
0
void
dump(char* path, struct list* files)
{
	FILE* f;
	struct list* runner;
	struct tib_file* tf;

	f = fopen(path, "w");
	if (f == NULL)
		return;

	fprintf(f, "File,L1,L2,Misses\n");

	for (runner = files; runner != NULL; )
	{
		tf = list_data(runner);
		fprintf(f, "%s,%llu,%llu,%llu\n", tf->path, tf->l1_hits, tf->l2_hits, 
		                                     tf->misses);
		runner = list_next(runner);
	}

	fclose(f);
}
Ejemplo n.º 21
0
void graph_destroy(Graph *g)
{
    
    DList *edgeList;
    List edgeListAll;
    ListElem *elem;
    int opRes;
    
    if (g == 0) {
        return;
    }
    
    list_init(&edgeListAll, 0);
    opRes = bst_listElements(&g->treeVertexUndirectedEdge, &edgeListAll);
    
    if (opRes == 0) {
        elem = list_head(&edgeListAll);
        while (elem != 0) {
            edgeList = (DList *) list_data(elem);
            dlist_destroy(edgeList);
            free((void *) edgeList);
            elem = list_next(elem);
        }
    }
    list_destroy(&edgeListAll);
    
    bst_destroy(&g->treeVertexUndirectedEdge);
    bst_destroy(&g->treeUndirectedEdgeSource1);
    bst_destroy(&g->treeUndirectedEdgeSource2);
    
    bst_destroy(&g->treeVertexData);
    bst_destroy(&g->treeUndirectedEdgeData);
    
    g->cmp_vertex = 0;
    g->cmp_edge = 0;
    return;
}
Ejemplo n.º 22
0
static void print_list_tsp(List *vertices) {

	TspVertex *vertex;

	ListElmt *element;

	int i;

	/*****************************************************************************
	 *                                                                            *
	 *  Display the list of vertices in a traveling-salesman problem.             *
	 *                                                                            *
	 *****************************************************************************/

	fprintf(stdout, "Vertices=%d\n", list_size(vertices));

	i = 0;
	element = list_head(vertices);

	while (1) {

		vertex = list_data(element);
		fprintf(stdout, "vertices[%03d]=%s: (%.1lf, %.1lf)\n", i,
				(char *) vertex->data, vertex->x, vertex->y);

		i++;

		if (list_is_tail(element))
			break;
		else
			element = list_next(element);

	}

	return;

}
int ccipher_writeColumn(const char *inputText, int inputLen, ColumnData *cData, List *columnSerial) {
	
	ListElem *elem;
	int *columnIndex, globalInputIndex;
	register unsigned int rowIndex;
	char *array, *writeLocation;
	unsigned int arrayLength;
	unsigned int noUseLength;
	char *noUseFirstIndex;
	
	array = cData->data;
	globalInputIndex = 0;
	elem = list_head(columnSerial);
	arrayLength = cData->rowSize * cData->columnSize;
	noUseLength = arrayLength - (unsigned int) inputLen;
	noUseFirstIndex = array + arrayLength - 1 - noUseLength + 1;
	
	while (elem != 0) {
		
		columnIndex = (int*) list_data(elem);
		rowIndex = 0;
		
		while (rowIndex < cData->rowSize
			   && globalInputIndex < inputLen) {
			   	
			writeLocation = array + (rowIndex * cData->columnSize) + *columnIndex - 1;
			if (writeLocation < noUseFirstIndex) {
				*writeLocation = *(inputText + globalInputIndex);
				globalInputIndex = globalInputIndex + 1;
			}
			rowIndex = rowIndex + 1;
		}
		
		elem = list_next(elem);
	}
	return 0;
}
Ejemplo n.º 24
0
/* Worker function to print outline of a node hierarchy. */
static void do_print_outline(const XmlNode *root, int indent)
{
	int		i;
	const List	*iter;

	for(i = 0; i < indent; i++)
		putchar(' ');
	if(root->element != NULL)
		printf("%s%s", root->element, root->text != NULL ? " : " : "");
	if(root->text != NULL)
		printf("\"%s\"", root->text);
	if(root->attrib_num > 0)
	{
		size_t	i;

		printf(" [");
		for(i = 0; i < root->attrib_num; i++)
			printf(" %s=\"%s\"", root->attrib[i].name, root->attrib[i].value);
		printf(" ]");
	}
	putchar('\n');
	for(iter = root->children; iter != NULL; iter = list_next(iter))
		do_print_outline(list_data(iter), indent + 1);
}
Ejemplo n.º 25
0
static void
display_access (List *access_list, int first, int total)
{
  ListNode *node;
  struct access *entry;
  register int row = 0, col = 0, i;

  clear ();

  if (first != 0)
    mvprintw (row++, col, _(" -- More -- "));

  node = list_head (access_list);

  for (i=0; i<first; i++)
    node = list_next (node);

  for (; i < total && node != NULL && row < LINES - 6; i++)
    {
      entry = (struct access *) list_data (node);

      /* FIXME: there is a access.h method to be made here. */

      mvprintw (row++, col, "%2d %s%-*s %s", i + 1, classmap[entry->scope],
                NAMESZ, entry->name, permmap[access_permissions (entry)]);

      node = list_next (node);
    }

  if (i < total)
    mvprintw (row++, col, _(" -- More -- "));

  refresh ();

  return;
}
Ejemplo n.º 26
0
Archivo: mst.c Proyecto: whatot/ma_c
/* mst */
int mst(Graph *graph, const MstVertex *start, List *span,
	int (*match)(const void *key1, const void *key2))
{
	AdjList *adjlist;
	MstVertex *mst_vertex, *adj_vertex;
	ListElmt *element, *member;
	double minimum;
	int found, i;

	/* Initialize all of the vertices in the graph. */
	found = 0;

	for (element = list_head(&graph_adjlists(graph));
	     element != NULL; element = list_next(element)) {
		mst_vertex = ((AdjList *)list_data(element))->vertex;

		if (match(mst_vertex, start)) {
			/* Initialize the start vertex. */
			mst_vertex->color = white;
			mst_vertex->key = 0;
			mst_vertex->parent = NULL;
			found = 1;
		} else {
			/* Initialize vertices other than the start vertex. */
			mst_vertex->color = white;
			mst_vertex->key = DBL_MAX;
			mst_vertex->parent = NULL;
		}
	}
	/* Return if the start vertex was not found. */
	if (!found) {
		return -1;
	}

	/* Use Prim's algorithm to compute a minimum spanning tree. */
	i = 0;

	while (i < graph_vcount(graph)) {
		/* Select the white vertex with the smallest key value. */
		minimum = DBL_MAX;

		for (element = list_head(&graph_adjlists(graph));
		     element != NULL; element = list_next(element)) {
			mst_vertex = ((AdjList *)list_data(element))->vertex;

			if (mst_vertex->color == white
			    && mst_vertex->key < minimum) {
				minimum = mst_vertex->key;
				adjlist = list_data(element);
			}
		}
		/* color the selected vertex black. */
		((MstVertex *)adjlist->vertex)->color = black;

		/* traverse each vertex adjacent to the selected vertex. */
		for (member = list_head(&adjlist->adjacent);
		     member != NULL; member = list_next(member)) {
			adj_vertex = list_data(member);

			/* Find the adjacent vertex in the list of
			 * adjacency-list structures. */
			for (element = list_head(&graph_adjlists(graph));
			     element != NULL; element = list_next(element)) {
				mst_vertex = ((AdjList *)
					      list_data(element))->vertex;

				if (match(mst_vertex, adj_vertex)) {
					/* decide whether to change the key
					 * value and parent of the adjacent
					 * vertex in the list of
					 * adjacency-list structures.*/
					if (mst_vertex->color == white
					    && adj_vertex->weight
						< mst_vertex->key) {

						mst_vertex->key
							= adj_vertex->weight;
						mst_vertex->parent
							= adjlist->vertex;
					}
					break;
				}
			}
		}
		/* prepare to select the next vertex. */
		i++;
	}

	/* Load the minimum spanning tree into a list. */
	list_init(span, NULL);

	for (element = list_head(&graph_adjlists(graph));
	     element != NULL ; element = list_next(element)) {
		/* Load each black vertex from the list of
		 * adjacency-list structures. */
		mst_vertex = ((AdjList *)list_data(element))->vertex;

		if (mst_vertex->color == black) {
			if (list_ins_next(span, list_tail(span),
					  mst_vertex) != 0) {
				list_destroy(span);
				return -1;
			}
		}
	}

	return 0;
}
Ejemplo n.º 27
0
int tsp(List *vertices, const TspVertex *start, List *tour, int (*match)
   (const void *key1, const void *key2)) {

TspVertex          *tsp_vertex,
                   *tsp_start,
                   *selection;

ListElmt           *element;

double             minimum,
                   distance,
                   x,
                   y;

int                found,
                   i;

/*****************************************************************************
*                                                                            *
*  Initialize the list for the tour.                                         *
*                                                                            *
*****************************************************************************/

list_init(tour, NULL);

/*****************************************************************************
*                                                                            *
*  Initialize all of the vertices in the graph.                              *
*                                                                            *
*****************************************************************************/

found = 0;

for (element = list_head(vertices); element != NULL; element =
   list_next(element)) {

   tsp_vertex = list_data(element);

   if (match(tsp_vertex, start)) {

      /***********************************************************************
      *                                                                      *
      *  Start the tour at the start vertex.                                 *
      *                                                                      *
      ***********************************************************************/

      if (list_ins_next(tour, list_tail(tour), tsp_vertex) != 0) {

         list_destroy(tour);
         return -1;

      }

      /***********************************************************************
      *                                                                      *
      *  Save the start vertex and its coordinates.                          *
      *                                                                      *
      ***********************************************************************/

      tsp_start = tsp_vertex;
      x = tsp_vertex->x;
      y = tsp_vertex->y;

      /***********************************************************************
      *                                                                      *
      *  Color the start vertex black.                                       *
      *                                                                      *
      ***********************************************************************/

      tsp_vertex->color = black;
      found = 1;

      }

   else {

      /***********************************************************************
      *                                                                      *
      *  Color all other vertices white.                                     *
      *                                                                      *
      ***********************************************************************/

      tsp_vertex->color = white;

   }

}

/*****************************************************************************
*                                                                            *
*  Return if the start vertex was not found.                                 *
*                                                                            *
*****************************************************************************/

if (!found) {

   list_destroy(tour);
   return -1;

}

/*****************************************************************************
*                                                                            *
*  Use the nearest-neighbor heuristic to compute the tour.                   *
*                                                                            *
*****************************************************************************/

i = 0;

while (i < list_size(vertices) - 1) {

   /**************************************************************************
   *                                                                         *
   *  Select the white vertex closest to the previous vertex in the tour.    *
   *                                                                         *
   **************************************************************************/

   minimum = DBL_MAX;

   for (element = list_head(vertices); element != NULL; element =
      list_next(element)) {

      tsp_vertex = list_data(element);

      if (tsp_vertex->color == white) {

         distance = sqrt(pow(tsp_vertex->x-x,2.0) + pow(tsp_vertex->y-y,2.0));

         if (distance < minimum) {

            minimum = distance;
            selection = tsp_vertex;

         }

      }

   }

   /**************************************************************************
   *                                                                         *
   *  Save the coordinates of the selected vertex.                           *
   *                                                                         *
   **************************************************************************/

   x = selection->x;
   y = selection->y;

   /**************************************************************************
   *                                                                         *
   *  Color the selected vertex black.                                       *
   *                                                                         *
   **************************************************************************/

   selection->color = black;

   /**************************************************************************
   *                                                                         *
   *  Insert the selected vertex into the tour.                              *
   *                                                                         *
   **************************************************************************/

   if (list_ins_next(tour, list_tail(tour), selection) != 0) {

      list_destroy(tour);
      return -1;

   }

   /**************************************************************************
   *                                                                         *
   *  Prepare to select the next vertex.                                     *
   *                                                                         *
   **************************************************************************/

   i++;

}

/*****************************************************************************
*                                                                            *
*  Insert the start vertex again to complete the tour.                       *
*                                                                            *
*****************************************************************************/

if (list_ins_next(tour, list_tail(tour), tsp_start) != 0) {

   list_destroy(tour);
   return -1;

}

return 0;

}
Ejemplo n.º 28
0
int main(int argc, char **argv) {

	List               list;
	ListElmt           *element;

	int                *data,
					   i;

	/*****************************************************************************
	*                                                                            *
	*  Initialize the linked list.                                               *
	*                                                                            *
	*****************************************************************************/

	list_init(&list, free);

	/*****************************************************************************
	*                                                                            *
	*  Perform some linked list operations.                                      *
	*                                                                            *
	*****************************************************************************/

	element = list_head(&list);

	for (i = 10; i > 0; i--) {

	   if ((data = (int *)malloc(sizeof(int))) == NULL)
		  return 1;

	   *data = i;

	   if (list_ins_next(&list, NULL, data) != 0)
		  return 1;

	}

	print_list(&list);

	element = list_head(&list);

	for (i = 0; i < 7; i++)
	   element = list_next(element);

	data = list_data(element);
	fprintf(stdout, "Removing an element after the one containing %03d\n", *data);

	if (list_rem_next(&list, element, (void **)&data) != 0)
	   return 1;

	print_list(&list);

	fprintf(stdout, "Inserting 011 at the tail of the list\n");

	*data = 11;
	if (list_ins_next(&list, list_tail(&list), data) != 0)
	   return 1;

	print_list(&list);

	fprintf(stdout, "Removing an element after the first element\n");

	element = list_head(&list);
	if (list_rem_next(&list, element, (void **)&data) != 0)
	   return 1;

	print_list(&list);

	fprintf(stdout, "Inserting 012 at the head of the list\n");

	*data = 12;
	if (list_ins_next(&list, NULL, data) != 0)
	   return 1;

	print_list(&list);

	fprintf(stdout, "Iterating and removing the fourth element\n");

	element = list_head(&list);
	element = list_next(element);
	element = list_next(element);

	if (list_rem_next(&list, element, (void **)&data) != 0)
	   return 1;

	print_list(&list);

	fprintf(stdout, "Inserting 013 after the first element\n");

	*data = 13;
	if (list_ins_next(&list, list_head(&list), data) != 0)
	   return 1;

	print_list(&list);

	i = list_is_head(&list, list_head(&list));
	fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i);
	i = list_is_head(&list, list_tail(&list));
	fprintf(stdout, "Testing list_is_head...Value=%d (0=OK)\n", i);
	i = list_is_tail(list_tail(&list));
	fprintf(stdout, "Testing list_is_tail...Value=%d (1=OK)\n", i);
	i = list_is_tail(list_head(&list));
	fprintf(stdout, "Testing list_is_tail...Value=%d (0=OK)\n", i);

	/*****************************************************************************
	*                                                                            *
	*  Destroy the linked list.                                                  *
	*                                                                            *
	*****************************************************************************/

	fprintf(stdout, "Destroying the list\n");
	list_destroy(&list);

	return 0;

}
Ejemplo n.º 29
0
Archivo: mst.c Proyecto: rjose/products
int mst(Graph *graph, const MstVertex *start, List *span,
                               int (*match)(const void *key1, const void *key2))
{
        AdjList *adjlist = NULL;

        MstVertex *mst_vertex, *adj_vertex;

        ListElmt *element, *member;

        double minimum;

        int found, i;

        found = 0;

        for (element = list_head(&graph_adjlists(graph)); element != NULL;
                                                 element = list_next(element)) {
                mst_vertex = ((AdjList *)list_data(element))->vertex;

                if (match(mst_vertex, start)) {
                        mst_vertex->color = white;
                        mst_vertex->key = 0;
                        mst_vertex->parent = NULL;
                        found = 1;
                }
                else {
                        mst_vertex->color = white;
                        mst_vertex->key = DBL_MAX;
                        mst_vertex->parent = NULL;
                }
        } 

        if (!found)
                return -1;

        /*
         * Use Prim's algorithm
         */
        i = 0;

        /* Select white vertex with the smallest value */
        while (i < graph_vcount(graph)) {
                minimum = DBL_MAX;

                for (element = list_head(&graph_adjlists(graph));
                                element != NULL; element = list_next(element)) {
                        mst_vertex = ((AdjList *)list_data(element))->vertex;

                        if (mst_vertex->color == white &&
                                                    mst_vertex->key < minimum) {
                                minimum = mst_vertex->key;
                                adjlist = list_data(element);
                        }
                }

                /* Color the selected vertex black */
                ((MstVertex *)adjlist->vertex)->color = black;

                for (member = list_head(&adjlist->adjacent); member != NULL;
                                                   member = list_next(member)) {
                        adj_vertex = list_data(member);

                        for(element = list_head(&graph_adjlists(graph));
                                element != NULL; element = list_next(element)) {
                                mst_vertex = ((AdjList *)list_data(element))->vertex;

                                if (match(mst_vertex, adj_vertex)) {
                                        if (mst_vertex->color == white &&
                                          adj_vertex->weight < mst_vertex->key) {
                                           mst_vertex->key = adj_vertex->weight;
                                           mst_vertex->parent = adjlist->vertex;
                                        }
                                        break;
                                }
                        }

                }

                i++;
        }

        list_init(span, NULL);

        for (element = list_head(&graph_adjlists(graph)); element != NULL;
                                                 element = list_next(element)) {
                mst_vertex = ((AdjList *)list_data(element))->vertex;
                if (mst_vertex->color == black) {
                        if (list_ins_next(span, list_tail(span), mst_vertex)
                                                                         != 0) {
                                list_destroy(span);
                                return -1;
                        }
                }
        }

        return 0;
}
Ejemplo n.º 30
0
int parser_eval(const expr_t *e, long double *r, hashtbl_t *vars) {
    if (!e || !r) {
        fprintf(stderr, "eval error: null expression or result var\n");
        return 1;
    }

    /* load known functions */
    static hashtbl_t *functions = NULL;
    if (unlikely(functions == NULL)) {
        functions = hashtbl_init(NULL, NULL);
        register_functions(functions);
    }
    /* stash constants into whatever symtab we get */
    if (unlikely(vars && !hashtbl_get(vars, "_stashed"))) {
        register_constants(vars);
    }

    const list_t *l = (const list_t*)e;
    const list_node_t *n = list_last(l);
    list_t *args = list_init(free, NULL);
    const symbol_t *s;

    while (n && (s = (const symbol_t*)list_data(n))) {
        long double *d = NULL, *v = NULL;
        long double (*f)(list_t*, size_t);

        switch (s->type) {
        case stNumber:
            d = (long double*)zmalloc(sizeof(long double));
            *d = s->number;
            list_push(args, d);
            break;

        case stVariable:
            if (!vars) {
                fprintf(stderr, "eval error: no symbol table\n");
                list_destroy(args);
                return 1;
            }
            if (!(v = (long double*)hashtbl_get(vars, s->variable))) {
                fprintf(stderr, "eval error: uninitialized variable [%s]\n", s->variable);
                list_destroy(args);
                return 1;
            }
            d = (long double*)zmalloc(sizeof(long double));
            *d = *v;
            list_push(args, d);
            break;

        case stBinOperator:
            /* rhs operand */
            if (!(v = (long double*)list_pop(args))) {
                fprintf(stderr, "eval error: missing rhs operand\n");
                list_destroy(args);
                return 1;
            }
        case stUniOperator:
            /* lhs operand, don't pop it... use it to store the result too */
            if (!(d = (long double*)list_peek_head(args))) {
                fprintf(stderr, "eval error: missing lhs operand\n");
                list_destroy(args);
                return 1;
            }
            *d = semanter_operator(s->operator, *d, s->type == stBinOperator ? *v : 0.0);
            free(v);
            break;

        case stFunction:
            if (!(f = (long double(*)(list_t*, size_t))hashtbl_get(functions, s->func.name))) {
                fprintf(stderr, "eval error: unknown function [%s]\n", s->func.name);
                list_destroy(args);
                return 1;
            }
            d = (long double*)zmalloc(sizeof(long double));
            *d = f(args, s->func.nargs);
            list_push(args, d);
            break;
        }
        n = list_prev(n);
    }

    if (list_size(args) != 1) {
        fprintf(stderr, "eval error: corrupt args stack\n");
        list_destroy(args);
        return 1;
    }

    long double *d = (long double*)list_peek_head(args);
    *r = *d;
    list_destroy(args);
    return 0;
}