File: list.m

package info (click to toggle)
swig 1.1p5-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 9,448 kB
  • ctags: 5,025
  • sloc: cpp: 21,599; ansic: 13,333; yacc: 3,297; python: 2,794; makefile: 2,197; perl: 1,984; tcl: 1,583; sh: 736; lisp: 201; objc: 143
file content (107 lines) | stat: -rw-r--r-- 1,709 bytes parent folder | download | duplicates (12)
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
#import "list.h"

@implementation List

// Create a new list

+new {
  self = [super alloc];
  return [self init];
}

// Initialize the list to a new state

-init {
  int i;
  nitems = 0;
  maxitems = 10;
  items = (id *) malloc(maxitems*sizeof(id));
  for (i = 0; i < maxitems; i++)
    items[i] = 0;
  return self;
}

// Destroy the list.   Does not destroy the objects in the list!

- free {
  free(items);
  [super free];
}

// Expand the list 

-(void) expand {
  int i;
  maxitems = 2*maxitems;
  items = (id *) realloc(items,maxitems*sizeof(id));
};

// Append a new item to the list

-(void) append: (id) item {
  if (nitems == maxitems) [self expand];
  items[nitems] = item;
  nitems++;
}

// Insert a new item to the list

-(void) insert: (id) item : (int) pos {
  int i;
  if (pos < 0) pos = 0;
  if (pos > nitems) pos = nitems;
  if (nitems == maxitems) [self expand];
  for (i = nitems; i > pos; i++) 
    items[i] = items[i-1];
  items[pos] = item;
  nitems++;
}

// Delete an item from the list

-remove: (int) pos {
  int i;
  id  it;
  if (pos < 0) return nil;
  if (pos >= nitems) return nil;
  it = items[pos];
  for (i = pos; i < nitems-1; i++)
    items[i] = items[i+1];
  nitems--;
  return it;
}

// Replace an item in the list. Returns the old item

-replace: (id) item : (int) pos {
  id i;
  i = items[pos];
  items[pos] = item;
  return i;
}

// Get an item from the list

-get: (int) i {
  if (i < 0) return nil;
  if (i >= nitems) return nil;
  return items[i];
}

// Find an item in the list.

-(int) index: obj {
  int i;
  for (i = 0; i < nitems; i++)
    if (items[i] == obj) return i;
  return -1;
}

// Return the length of the list

-(int) len {
  return nitems;
}
@end