Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
list.cpp@0:4ab1392a0142, 2012-10-07 (annotated)
- Committer:
- gignops
- Date:
- Sun Oct 07 11:49:09 2012 +0000
- Revision:
- 0:4ab1392a0142
This project is an adaptation of tinypy code. The aim is to run python code on Mbed.; ; Have fun !
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| gignops | 0:4ab1392a0142 | 1 | #include "list.h" |
| gignops | 0:4ab1392a0142 | 2 | |
| gignops | 0:4ab1392a0142 | 3 | void _tp_list_realloc(_tp_list *self,int len) { |
| gignops | 0:4ab1392a0142 | 4 | if (!len) { len=1; } |
| gignops | 0:4ab1392a0142 | 5 | self->items = (tp_obj*)tp_realloc(self->items,len*sizeof(tp_obj)); |
| gignops | 0:4ab1392a0142 | 6 | self->alloc = len; |
| gignops | 0:4ab1392a0142 | 7 | } |
| gignops | 0:4ab1392a0142 | 8 | |
| gignops | 0:4ab1392a0142 | 9 | void _tp_list_set(TP,_tp_list *self,int k, tp_obj v, const char *error) { |
| gignops | 0:4ab1392a0142 | 10 | if (k >= self->len) { tp_raise(,"%s: KeyError: %d\n",error,k); } |
| gignops | 0:4ab1392a0142 | 11 | self->items[k] = v; |
| gignops | 0:4ab1392a0142 | 12 | tp_grey(tp,v); |
| gignops | 0:4ab1392a0142 | 13 | } |
| gignops | 0:4ab1392a0142 | 14 | void _tp_list_free(_tp_list *self) { |
| gignops | 0:4ab1392a0142 | 15 | tp_free(self->items); |
| gignops | 0:4ab1392a0142 | 16 | tp_free(self); |
| gignops | 0:4ab1392a0142 | 17 | } |
| gignops | 0:4ab1392a0142 | 18 | |
| gignops | 0:4ab1392a0142 | 19 | tp_obj _tp_list_get(TP,_tp_list *self,int k,const char *error) { |
| gignops | 0:4ab1392a0142 | 20 | if (k >= self->len) { tp_raise(tp_None,"%s: KeyError: %d\n",error,k); } |
| gignops | 0:4ab1392a0142 | 21 | return self->items[k]; |
| gignops | 0:4ab1392a0142 | 22 | } |
| gignops | 0:4ab1392a0142 | 23 | void _tp_list_insertx(TP,_tp_list *self, int n, tp_obj v) { |
| gignops | 0:4ab1392a0142 | 24 | if (self->len >= self->alloc) { |
| gignops | 0:4ab1392a0142 | 25 | _tp_list_realloc(self,self->alloc*2); |
| gignops | 0:4ab1392a0142 | 26 | } |
| gignops | 0:4ab1392a0142 | 27 | if (n < self->len) { memmove(&self->items[n+1],&self->items[n],sizeof(tp_obj)*(self->len-n)); } |
| gignops | 0:4ab1392a0142 | 28 | self->items[n] = v; |
| gignops | 0:4ab1392a0142 | 29 | self->len += 1; |
| gignops | 0:4ab1392a0142 | 30 | } |
| gignops | 0:4ab1392a0142 | 31 | void _tp_list_appendx(TP,_tp_list *self, tp_obj v) { |
| gignops | 0:4ab1392a0142 | 32 | _tp_list_insertx(tp,self,self->len,v); |
| gignops | 0:4ab1392a0142 | 33 | } |
| gignops | 0:4ab1392a0142 | 34 | void _tp_list_insert(TP,_tp_list *self, int n, tp_obj v) { |
| gignops | 0:4ab1392a0142 | 35 | _tp_list_insertx(tp,self,n,v); |
| gignops | 0:4ab1392a0142 | 36 | tp_grey(tp,v); |
| gignops | 0:4ab1392a0142 | 37 | } |
| gignops | 0:4ab1392a0142 | 38 | void _tp_list_append(TP,_tp_list *self, tp_obj v) { |
| gignops | 0:4ab1392a0142 | 39 | _tp_list_insert(tp,self,self->len,v); |
| gignops | 0:4ab1392a0142 | 40 | } |
| gignops | 0:4ab1392a0142 | 41 | tp_obj _tp_list_pop(TP,_tp_list *self, int n, const char *error) { |
| gignops | 0:4ab1392a0142 | 42 | tp_obj r = _tp_list_get(tp,self,n,error); |
| gignops | 0:4ab1392a0142 | 43 | if (n != self->len-1) { memmove(&self->items[n],&self->items[n+1],sizeof(tp_obj)*(self->len-(n+1))); } |
| gignops | 0:4ab1392a0142 | 44 | self->len -= 1; |
| gignops | 0:4ab1392a0142 | 45 | return r; |
| gignops | 0:4ab1392a0142 | 46 | } |
| gignops | 0:4ab1392a0142 | 47 | |
| gignops | 0:4ab1392a0142 | 48 | int _tp_list_find(TP,_tp_list *self, tp_obj v) { |
| gignops | 0:4ab1392a0142 | 49 | int n; |
| gignops | 0:4ab1392a0142 | 50 | for (n=0; n<self->len; n++) { |
| gignops | 0:4ab1392a0142 | 51 | if (tp_cmp(tp,v,self->items[n]) == 0) { |
| gignops | 0:4ab1392a0142 | 52 | return n; |
| gignops | 0:4ab1392a0142 | 53 | } |
| gignops | 0:4ab1392a0142 | 54 | } |
| gignops | 0:4ab1392a0142 | 55 | return -1; |
| gignops | 0:4ab1392a0142 | 56 | } |
| gignops | 0:4ab1392a0142 | 57 | |
| gignops | 0:4ab1392a0142 | 58 | tp_obj tp_index(TP) { |
| gignops | 0:4ab1392a0142 | 59 | tp_obj self = TP_OBJ(); |
| gignops | 0:4ab1392a0142 | 60 | tp_obj v = TP_OBJ(); |
| gignops | 0:4ab1392a0142 | 61 | int i = _tp_list_find(tp,self.list.val,v); |
| gignops | 0:4ab1392a0142 | 62 | if (i < 0) { tp_raise(tp_None,"tp_index(%s,%s) - item not found",TP_CSTR(self),TP_CSTR(v)); } |
| gignops | 0:4ab1392a0142 | 63 | return tp_number(i); |
| gignops | 0:4ab1392a0142 | 64 | } |
| gignops | 0:4ab1392a0142 | 65 | |
| gignops | 0:4ab1392a0142 | 66 | _tp_list *_tp_list_new(void) { |
| gignops | 0:4ab1392a0142 | 67 | return (_tp_list*)tp_malloc(sizeof(_tp_list)); |
| gignops | 0:4ab1392a0142 | 68 | } |
| gignops | 0:4ab1392a0142 | 69 | |
| gignops | 0:4ab1392a0142 | 70 | tp_obj _tp_list_copy(TP, tp_obj rr) { |
| gignops | 0:4ab1392a0142 | 71 | tp_obj val = {TP_LIST}; |
| gignops | 0:4ab1392a0142 | 72 | _tp_list *o = rr.list.val; |
| gignops | 0:4ab1392a0142 | 73 | _tp_list *r = _tp_list_new(); |
| gignops | 0:4ab1392a0142 | 74 | *r = *o; r->gci = 0; |
| gignops | 0:4ab1392a0142 | 75 | r->items = (tp_obj*)tp_malloc(sizeof(tp_obj)*o->alloc); |
| gignops | 0:4ab1392a0142 | 76 | memcpy(r->items,o->items,sizeof(tp_obj)*o->alloc); |
| gignops | 0:4ab1392a0142 | 77 | val.list.val = r; |
| gignops | 0:4ab1392a0142 | 78 | return tp_track(tp,val); |
| gignops | 0:4ab1392a0142 | 79 | } |
| gignops | 0:4ab1392a0142 | 80 | |
| gignops | 0:4ab1392a0142 | 81 | tp_obj tp_append(TP) { |
| gignops | 0:4ab1392a0142 | 82 | tp_obj self = TP_OBJ(); |
| gignops | 0:4ab1392a0142 | 83 | tp_obj v = TP_OBJ(); |
| gignops | 0:4ab1392a0142 | 84 | _tp_list_append(tp,self.list.val,v); |
| gignops | 0:4ab1392a0142 | 85 | return tp_None; |
| gignops | 0:4ab1392a0142 | 86 | } |
| gignops | 0:4ab1392a0142 | 87 | |
| gignops | 0:4ab1392a0142 | 88 | tp_obj tp_pop(TP) { |
| gignops | 0:4ab1392a0142 | 89 | tp_obj self = TP_OBJ(); |
| gignops | 0:4ab1392a0142 | 90 | return _tp_list_pop(tp,self.list.val,self.list.val->len-1,"pop"); |
| gignops | 0:4ab1392a0142 | 91 | } |
| gignops | 0:4ab1392a0142 | 92 | |
| gignops | 0:4ab1392a0142 | 93 | tp_obj tp_insert(TP) { |
| gignops | 0:4ab1392a0142 | 94 | tp_obj self = TP_OBJ(); |
| gignops | 0:4ab1392a0142 | 95 | int n = TP_NUM(); |
| gignops | 0:4ab1392a0142 | 96 | tp_obj v = TP_OBJ(); |
| gignops | 0:4ab1392a0142 | 97 | _tp_list_insert(tp,self.list.val,n,v); |
| gignops | 0:4ab1392a0142 | 98 | return tp_None; |
| gignops | 0:4ab1392a0142 | 99 | } |
| gignops | 0:4ab1392a0142 | 100 | |
| gignops | 0:4ab1392a0142 | 101 | tp_obj tp_extend(TP) { |
| gignops | 0:4ab1392a0142 | 102 | tp_obj self = TP_OBJ(); |
| gignops | 0:4ab1392a0142 | 103 | tp_obj v = TP_OBJ(); |
| gignops | 0:4ab1392a0142 | 104 | int i; |
| gignops | 0:4ab1392a0142 | 105 | for (i=0; i<v.list.val->len; i++) { |
| gignops | 0:4ab1392a0142 | 106 | _tp_list_append(tp,self.list.val,v.list.val->items[i]); |
| gignops | 0:4ab1392a0142 | 107 | } |
| gignops | 0:4ab1392a0142 | 108 | return tp_None; |
| gignops | 0:4ab1392a0142 | 109 | } |
| gignops | 0:4ab1392a0142 | 110 | |
| gignops | 0:4ab1392a0142 | 111 | tp_obj tp_list(TP) { |
| gignops | 0:4ab1392a0142 | 112 | tp_obj r = {TP_LIST}; |
| gignops | 0:4ab1392a0142 | 113 | r.list.val = _tp_list_new(); |
| gignops | 0:4ab1392a0142 | 114 | return tp ? tp_track(tp,r) : r; |
| gignops | 0:4ab1392a0142 | 115 | } |
| gignops | 0:4ab1392a0142 | 116 | |
| gignops | 0:4ab1392a0142 | 117 | tp_obj tp_list_n(TP,int n,tp_obj *argv) { |
| gignops | 0:4ab1392a0142 | 118 | int i; |
| gignops | 0:4ab1392a0142 | 119 | tp_obj r = tp_list(tp); _tp_list_realloc(r.list.val,n); |
| gignops | 0:4ab1392a0142 | 120 | for (i=0; i<n; i++) { |
| gignops | 0:4ab1392a0142 | 121 | _tp_list_append(tp,r.list.val,argv[i]); |
| gignops | 0:4ab1392a0142 | 122 | } |
| gignops | 0:4ab1392a0142 | 123 | return r; |
| gignops | 0:4ab1392a0142 | 124 | } |
| gignops | 0:4ab1392a0142 | 125 | |
| gignops | 0:4ab1392a0142 | 126 | int _tp_sort_cmp(tp_obj *a,tp_obj *b) { |
| gignops | 0:4ab1392a0142 | 127 | return tp_cmp(0,*a,*b); |
| gignops | 0:4ab1392a0142 | 128 | } |
| gignops | 0:4ab1392a0142 | 129 | |
| gignops | 0:4ab1392a0142 | 130 | tp_obj tp_sort(TP) { |
| gignops | 0:4ab1392a0142 | 131 | tp_obj self = TP_OBJ(); |
| gignops | 0:4ab1392a0142 | 132 | qsort(self.list.val->items, self.list.val->len, sizeof(tp_obj), (int(*)(const void*,const void*))_tp_sort_cmp); |
| gignops | 0:4ab1392a0142 | 133 | return tp_None; |
| gignops | 0:4ab1392a0142 | 134 | } |
| gignops | 0:4ab1392a0142 | 135 |