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 116 117 118 119 120 121 122 123 124 125 126
|
#ifndef ADDRESSLIST_H
#define ADDRESSLIST_H
#include<stddef.h>
#include<stdlib.h>
typedef struct{
size_t*list;//addresses
size_t len; //list length
size_t cap; //capacity...
}addressl;
addressl*addresslnew(size_t cap){
addressl*res=(addressl*)malloc(sizeof(addressl));
res->cap=cap;
res->len=0;
res->list=(size_t*)malloc(cap*sizeof(size_t));
return res;
};
size_t addresslfind(addressl*cont,const void*item){
size_t pos=0;
size_t address=(size_t)item;
for(;pos<cont->len;++pos)if(cont->list[pos]==address)return pos;
return cont->len;
};
void addresslgoto(addressl*cont,size_t from,size_t to){
size_t address=cont->list[from];
if(to>from){
for(;from<to;++from){
cont->list[from]=cont->list[from+1];
};
}else{
for(;from>to;--from){
cont->list[from]=cont->list[from-1];
};
};
cont->list[to]=address;
};
void addresslmove(addressl*cont,size_t begin,size_t end,size_t to){
size_t len=end-begin;
size_t border=to+len;
if(begin>to){
for(;to<border;++to,++begin){
addresslgoto(cont,begin,to);
};
}else{
for(;to<border;++to){
addresslgoto(cont,begin,border-1);
};
};
};
void addresslpush(addressl*cont,void*item){
++cont->len;
if(cont->len>cont->cap){
cont->cap+=cont->cap;
cont->list=(size_t*)realloc(cont->list,sizeof(size_t)*cont->cap);
};
cont->list[cont->len-1]=(size_t)item;
};
void addresslpop(addressl*cont){
--cont->len;
};
void addresslinsert(addressl*cont,void*item,size_t postarget){
size_t pos=cont->len;
addresslpush(cont,item);
for(;pos>postarget;--pos){
cont->list[pos]=cont->list[pos-1];
};
cont->list[postarget]=(size_t)item;
};
void addresslerase(addressl*cont,size_t posbegin,size_t posend){
size_t ptr;
size_t pos=posbegin;
size_t len=cont->len;
size_t*list=cont->list;
for(;pos<len;++pos){
ptr=(pos-posbegin)+posend;
if(ptr<=len){
list[pos]=list[ptr];
};
};
cont->len-=(posend-posbegin);
};
void addresslfree(addressl*cont,size_t posbegin,size_t posend){
size_t ptr;
size_t pos=posbegin;
size_t len=cont->len;
size_t*list=cont->list;
for(;pos<len;++pos){
ptr=(pos-posbegin)+posend;
if(ptr<=len){
list[pos]=list[ptr];
}else{
free((void*)list[pos]);
};
};
cont->len-=(posend-posbegin);
};
void addresslreserve(addressl*cont,size_t len){
cont->len=len;
cont->list=(size_t*)realloc(cont->list,sizeof(size_t)*len);
};
#endif//ADDRESSLIST_H
|