00001 00012 #include <sys/types.h> 00013 #include <stdlib.h> 00014 #include <stdio.h> 00015 #include <string.h> 00016 #include <strings.h> 00017 #include <assert.h> 00018 #include <appconfig.h> 00019 00023 typedef struct tag_dsObj { 00024 Int32 *bp; 00025 Int32 *sp; 00026 Int32 *se; 00027 Int32 sl; 00028 } dsObj; 00029 00030 #define STACK_IMPL 00031 #include <stack.h> 00032 00038 static void 00039 StackResize(dsObj *sp, Int32 newSize) 00040 { 00041 ptrdiff_t sd = sp->sp - sp->bp; 00042 sp->sl = newSize; 00043 sp->bp = realloc(sp->bp, sp->sl * sizeof (Int32)); 00044 sp->se = sp->bp + (sp->sl - 1); 00045 sp->sp = sp->bp + sd; 00046 } 00047 00048 dsObj * 00049 StackNew(void) 00050 { 00051 dsObj *rv = calloc(sizeof (dsObj), 1); 00052 return (rv); 00053 } 00054 00055 void 00056 StackDelete(dsObj *sp) 00057 { 00058 if (sp->bp) free(sp->bp); 00059 free(sp); 00060 } 00061 00062 void 00063 StackPush(dsObj *sp, Int32 value) 00064 { 00065 if (sp->sp >= sp->se) StackResize(sp, sp->sl ? sp->sl << 1 : 64); 00066 *(++sp->sp) = value; 00067 } 00068 00069 Int32 00070 StackPop(dsObj *sp) 00071 { 00072 assert(sp->sp != NULL); 00073 if (sp->sp >= sp->bp) { 00074 assert(sp->sp != NULL && sp->bp != NULL); 00075 return (*sp->sp--); 00076 } 00077 perror("<DsObj> Object Underflow(out of elements)"); 00078 return (-1); 00079 } 00080 00081 Int8 00082 StackIsEmpty(dsObj *sp) 00083 { 00084 if (sp->sp == NULL) 00085 return ((Int8)1); 00086 return ((Int8)(sp->sp < sp->bp)); 00087 } 00088 00089 void 00090 StackDoEmpty(dsObj *sp) 00091 { 00092 if (sp->sp != NULL) 00093 sp->sp = sp->bp - 1; 00094 } 00095 00096 int 00097 StackNElements(dsObj *sp) 00098 { 00099 return ((sp->sp+1) - sp->bp); 00100 } 00101 00102 Int32 00103 ListGet(dsObj *sp, Int32 index) 00104 { 00105 if (index >= (sp->sp - sp->bp)) 00106 return (-1); 00107 return (sp->bp[index + 1]); 00108 } 00109 00110 void 00111 ListSet(dsObj *sp, Int32 index, Int32 element) 00112 { 00113 if (index < (sp->sp - sp->bp)) 00114 sp->bp[index+1] = element; 00115 } 00116 00117 void 00118 ListInsert(dsObj *sp, Int32 index, Int32 element) 00119 { 00120 if ((index - 1) >= (sp->sp - sp->bp)) { 00121 StackPush(sp, element); 00122 } else { 00123 StackPush(sp, 0); 00124 bcopy(sp->bp + (index - 1), sp->bp + index, 00125 (sp->sp - (sp->bp + (index - 1))) * sizeof (Int32)); 00126 sp->bp[index] = element; 00127 } 00128 } 00129 00130 Int32 00131 ListRemove(dsObj *ds, Int32 index) 00132 { 00133 Int32 rv; 00134 00135 if (index >= (ds->sp - ds->bp)) 00136 return (-1); 00137 00138 rv = ds->bp[index]; 00139 bcopy(ds->bp + (index + 1), ds->bp + index, 00140 (ds->sp - (ds->bp + index)) * sizeof(Int32)); 00141 (void)StackPop(ds); 00142 00143 return (rv); 00144 }