Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

What does prog think of my arraylist

Name: Anonymous 2011-11-25 17:03

Please do tell me all my mistakes so i can fix them.
One of the things i worried about is if i were to use this in some other application and the arraylist holds malloc'd data whenever it goes to destroy the arraylist those malloc'd data will still be there unless i explicitly remove/free them so i wasn't sure what to do in the destroy method aside from freeing the objs array itself.


(1) unixs1 $ cat arraylist.h
#ifndef ARRAYLIST_H
#define ARRAYLIST_H

struct arraylist {
  int cap;
  int size;
  void **objs;
  int (*_equals)(void*,void*);
};

struct arraylist *arraylist_create(int,int (*equals)(void *o1,void *o2));
int arraylist_contains(struct arraylist *,void *);
int arraylist_indeof(struct arraylist *,void *);
void arraylist_set(struct arraylist *,int,void *);
void arraylist_add(struct arraylist *,void *);
void *arraylist_remove(struct arraylist *,int);
void *arraylist_get(struct arraylist *,int);
void arraylist_destroy(struct arraylist *);

#endif

(2) unixs1 $ cat arraylist.c
#include <stdio.h>
#include "arraylist.h"

void arraylist_repop(struct arraylist *);


struct arraylist * arraylist_create(int c,int (*equals)(void *o1,void *o2))
{
  struct arraylist *a = malloc(sizeof(struct arraylist));
  if(!a){
    /* error handling if needed */
    printf("Failed to create the arraylist\n");
    exit(1);
  }
  a->cap = c;
  a->size = 0;
  a->_equals = equals;
  a->objs = malloc(sizeof(void *)*a->size);
  if(!a->objs){
    /* Error handling if needed */
    printf("Failed create the arraylist\n");
    exit(1);
  }
  return a;
}

int arraylist_contains(struct arraylist *a,void *e)
{
  return(arraylist_indexof(a,e) > -1 ? 1 : 0);
}

int arraylist_indexof(struct arraylist *a,void *e)
{
  register int i;
  for(i=0;i<a->size;++i)
    if(a->_equals(arraylist_get(a,i),e) != 0){
      return i;
    }
  return -1;
}

void arraylist_set(struct arraylist *a,int in,void *e)
{
  register int i;
  if(a->cap >= a->size+1)
    arraylist_repop(a);
  for(i=a->size;i>=in;--i){
    a->objs[i+1] = a->objs[i];
  }
  a->objs[in] = e;
  a->size++;
}

void arraylist_add(struct arraylist *a,void *e)
{
  if(a->cap >= a->size+1)
    arraylist_repop(a);
  a->objs[a->size] = e;
  a->size++;
}

void * arraylist_remove(struct arraylist *a,int in){
  register int i;
  void *e = a->objs[in]; /* error if in >= size || < 0 */
  for(i=in;i<a->size;++i){
    a->objs[i] = a->objs[i+1];
  }
  a->size--;
  return e;
}

void * arraylist_get(struct arraylist *a,int in)
{
  return a->objs[in]; /* will error if in >= size || < 0 */
}

void arraylist_repop(struct arraylist *a)
{
  register int i;
  a->cap = a->cap * 2;
  void **ob = a->objs;
  a->objs = malloc(sizeof(void *)*a->cap);
  if(!a->objs){
    /* Error handle */
    printf("Failed to repop\n");
    exit(1);
  }

  for(i=0;i<a->size;++i)
    a->objs[i] = ob[i];
  free(ob);
}

void arraylist_destroy(struct arraylist *a)
{
  free(a->objs);
}


int equals(void *o,void *o2)
{
  char *s1 = (char *)o;
  char *s2 = (char *)o2;
  return strcmp(s1,s2) == 0 ? 1 : 0;
}

int main(){
  struct arraylist *test = arraylist_create(1,equals);
  arraylist_add(test,"Hello Faggots");
  printf("AL GET [%s]\n",arraylist_get(test,0));
  arraylist_add(test,"anusify me captain");
  arraylist_add(test,"Holy bible");
  arraylist_set(test,1,"glory to the gods");

  printf("AL SIZE [%d] | CAP [%d]\n",test->size,test->cap);
  while(test->size > 0){
    printf("AL REM [%s]\n",arraylist_remove(test,0));
  }
}
(3) unixs1 $ atest
AL GET [Hello Faggots]
AL SIZE [4] | CAP [16]
AL REM [Hello Faggots]
AL REM [glory to the gods]
AL REM [anusify me captain]
AL REM [Holy bible]

Name: VIPPER 2011-11-25 17:06

can't talk; sucking dick

Name: Anonymous 2011-11-25 17:18

Some things.

1. Better to use size_t instead of int for storing the array size and capacity;
2. register int i in arraylist_indexof()? Really? So you might bother putting a lot of restrict and some inlines too, why not?;
3. Maybe equals could be compare, in the case you want to sort the array.

Name: Anonymous 2011-11-25 17:23

I would have used size_t (like >>3 said) and realloc inside arraylist_repop but it's nice.

Name: Anonymous 2011-11-25 17:26

you don't need register int.
use size_t like >>3 suggested.
your set is wrong, set should change the element at index. What you did was add at index so fix that.
you should think about adding a method that will let you add an array of elements to the arraylist.
lastly make a trim method that will return a trimmed version of void **objs which could be useful some point in your life.

Name: Anonymous 2011-11-25 17:46

>>3
>>5
Thanks I updated the code with your suggestions and removed the register stuff.

May i ask when is it a good time to use register?

Name: Anonymous 2011-11-25 17:52

>>6
May i ask when is it a good time to use register?
It does nothing if you use enable a modern compiler's optimizations.

Name: Anonymous 2011-11-25 17:53

>>7
Oh, okay. Thank you.

Name: Anonymous 2011-11-25 20:04

>>8

Note that size_t requires stddef.h if you're not including any other header in your header file.

Name: Anonymous 2011-11-25 23:26

Have you heard of realloc()? It automatically moves the data for you.

Name: Anonymous 2011-11-26 5:24

why size_t and not just int?

Name: Anonymous 2011-11-28 0:30

>>11
0/10
IHNBT

Don't change these.
Name: Email:
Entire Thread Thread List