[262685]: TestArray.c  Maximize  Restore  History

Download this file

117 lines (100 with data), 2.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "AutoPanoSift.h"
struct sPoint1 {
double x;
};
typedef struct sPoint1 Point1;
void dump(ArrayList* l)
{
int i;
printf("[");
for(i=0; i<ArrayList_Count(l); i++) {
printf("%g ", ((Point1*)ArrayList_GetItem(l, i))->x);
}
printf("]\n");
}
Point1* Point1_new0()
{
Point1* self = (Point1*)malloc(sizeof(Point1));
self->x = 0;
return self;
}
Point1* Point1_new (double x)
{
Point1* self = Point1_new0();
self->x = x;
return self;
}
void Point1_delete(Point1* self)
{
if (self) {
free(self);
}
}
int Point1_Compare(IComparator* self, const Point1* p1, const Point1* p2)
{
if (p1->x<p2->x)
return -1;
else if(p1->x>p2->x)
return 1;
return 0;
}
int main(int argc, char* argv[])
{
SortedLimitedList *s;
IComparator cmp;
Point1 *p;
ArrayList* l = ArrayList_new0(Point1_delete);
dump(l);
ArrayList_AddItem(l, Point1_new(0));
ArrayList_AddItem(l, Point1_new(1));
ArrayList_AddItem(l, Point1_new(2));
dump(l);
// overwrite
Point1_delete(ArrayList_GetItem(l,1));
ArrayList_SetItem(l, 1, Point1_new(42));
dump(l);
Point1_delete(ArrayList_GetItem(l,1));
ArrayList_RemoveAt(l, 1);
dump(l);
p = Point1_new(42);
Point1_delete(ArrayList_GetItem(l,1));
ArrayList_SetItem(l, 1, p);
ArrayList_AddItem(l, Point1_new(2));
dump(l);
ArrayList_RemoveItem(l, p);
Point1_delete(p);
dump(l);
ArrayList_AddItem(l, Point1_new(3));
ArrayList_AddItem(l, Point1_new(4));
ArrayList_AddItem(l, Point1_new(5));
ArrayList_AddItem(l, Point1_new(6));
dump(l);
Point1_delete(ArrayList_GetItem(l,2));
Point1_delete(ArrayList_GetItem(l,3));
ArrayList_RemoveRange(l, 2, 2);
dump(l);
ArrayList_AddItem(l, Point1_new(3));
ArrayList_AddItem(l, Point1_new(4));
dump(l);
cmp.compareTo = Point1_Compare;
ArrayList_Sort(l, &cmp);
dump(l);
ArrayList_delete(l);
s = SortedLimitedList_new(5, Point1_delete);
s->comparator.compareTo = Point1_Compare;
SortedLimitedList_AddItem(s, Point1_new(50));
SortedLimitedList_AddItem(s, Point1_new(40));
SortedLimitedList_AddItem(s, Point1_new(30));
SortedLimitedList_AddItem(s, Point1_new(20));
SortedLimitedList_AddItem(s, Point1_new(10));
SortedLimitedList_AddItem(s, Point1_new(0));
dump(&s->base);
SortedLimitedList_AddItem(s, Point1_new(25));
dump(&s->base);
while (SortedLimitedList_Count(s)>0) {
Point1_delete(SortedLimitedList_GetItem(s,0));
SortedLimitedList_RemoveAt(s, 0);
dump(&s->base);
}
SortedLimitedList_delete(s);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks