Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1913 jaeger 1
void _tp_list_realloc(_tp_list *self,int len) {
2
    if (!len) { len=1; }
3
    self->items = (tp_obj*)tp_realloc(self->items,len*sizeof(tp_obj));
4
    self->alloc = len;
5
}
6
 
7
void _tp_list_set(TP,_tp_list *self,int k, tp_obj v, const char *error) {
8
    if (k >= self->len) { tp_raise(,"%s: KeyError: %d\n",error,k); }
9
    self->items[k] = v;
10
    tp_grey(tp,v);
11
}
12
void _tp_list_free(_tp_list *self) {
13
    tp_free(self->items);
14
    tp_free(self);
15
}
16
 
17
tp_obj _tp_list_get(TP,_tp_list *self,int k,const char *error) {
18
    if (k >= self->len) { tp_raise(tp_None,"%s: KeyError: %d\n",error,k); }
19
    return self->items[k];
20
}
21
void _tp_list_insertx(TP,_tp_list *self, int n, tp_obj v) {
22
    if (self->len >= self->alloc) {
23
        _tp_list_realloc(self,self->alloc*2);
24
    }
25
    if (n < self->len) { memmove(&self->items[n+1],&self->items[n],sizeof(tp_obj)*(self->len-n)); }
26
    self->items[n] = v;
27
    self->len += 1;
28
}
29
void _tp_list_appendx(TP,_tp_list *self, tp_obj v) {
30
    _tp_list_insertx(tp,self,self->len,v);
31
}
32
void _tp_list_insert(TP,_tp_list *self, int n, tp_obj v) {
33
    _tp_list_insertx(tp,self,n,v);
34
    tp_grey(tp,v);
35
}
36
void _tp_list_append(TP,_tp_list *self, tp_obj v) {
37
    _tp_list_insert(tp,self,self->len,v);
38
}
39
tp_obj _tp_list_pop(TP,_tp_list *self, int n, const char *error) {
40
    tp_obj r = _tp_list_get(tp,self,n,error);
41
    if (n != self->len-1) { memmove(&self->items[n],&self->items[n+1],sizeof(tp_obj)*(self->len-(n+1))); }
42
    self->len -= 1;
43
    return r;
44
}
45
 
46
int _tp_list_find(TP,_tp_list *self, tp_obj v) {
47
    int n;
48
    for (n=0; nlen; n++) {
49
        if (tp_cmp(tp,v,self->items[n]) == 0) {
50
            return n;
51
        }
52
    }
53
    return -1;
54
}
55
 
56
tp_obj tp_index(TP) {
57
    tp_obj self = TP_OBJ();
58
    tp_obj v = TP_OBJ();
59
    int i = _tp_list_find(tp,self.list.val,v);
60
    if (i < 0) { tp_raise(tp_None,"tp_index(%s,%s) - item not found",TP_CSTR(self),TP_CSTR(v)); }
61
    return tp_number(i);
62
}
63
 
64
_tp_list *_tp_list_new(void) {
65
    return (_tp_list*)tp_malloc(sizeof(_tp_list));
66
}
67
 
68
tp_obj _tp_list_copy(TP, tp_obj rr) {
69
    tp_obj val = {TP_LIST};
70
    _tp_list *o = rr.list.val;
71
    _tp_list *r = _tp_list_new();
72
    *r = *o; r->gci = 0;
73
    r->items = (tp_obj*)tp_malloc(sizeof(tp_obj)*o->alloc);
74
    memcpy(r->items,o->items,sizeof(tp_obj)*o->alloc);
75
    val.list.val = r;
76
    return tp_track(tp,val);
77
}
78
 
79
tp_obj tp_append(TP) {
80
    tp_obj self = TP_OBJ();
81
    tp_obj v = TP_OBJ();
82
    _tp_list_append(tp,self.list.val,v);
83
    return tp_None;
84
}
85
 
86
tp_obj tp_pop(TP) {
87
    tp_obj self = TP_OBJ();
88
    return _tp_list_pop(tp,self.list.val,self.list.val->len-1,"pop");
89
}
90
 
91
tp_obj tp_insert(TP) {
92
    tp_obj self = TP_OBJ();
93
    int n = TP_NUM();
94
    tp_obj v = TP_OBJ();
95
    _tp_list_insert(tp,self.list.val,n,v);
96
    return tp_None;
97
}
98
 
99
tp_obj tp_extend(TP) {
100
    tp_obj self = TP_OBJ();
101
    tp_obj v = TP_OBJ();
102
    int i;
103
    for (i=0; ilen; i++) {
104
        _tp_list_append(tp,self.list.val,v.list.val->items[i]);
105
    }
106
    return tp_None;
107
}
108
 
109
tp_obj tp_list(TP) {
110
    tp_obj r = {TP_LIST};
111
    r.list.val = _tp_list_new();
112
    return tp ? tp_track(tp,r) : r;
113
}
114
 
115
tp_obj tp_list_n(TP,int n,tp_obj *argv) {
116
    int i;
117
    tp_obj r = tp_list(tp); _tp_list_realloc(r.list.val,n);
118
    for (i=0; i
119
        _tp_list_append(tp,r.list.val,argv[i]);
120
    }
121
    return r;
122
}
123
 
124
int _tp_sort_cmp(tp_obj *a,tp_obj *b) {
125
    return tp_cmp(0,*a,*b);
126
}
127
 
128
tp_obj tp_sort(TP) {
129
    tp_obj self = TP_OBJ();
130
    qsort(self.list.val->items, self.list.val->len, sizeof(tp_obj), (int(*)(const void*,const void*))_tp_sort_cmp);
131
    return tp_None;
132
}
133