/* librock/fsil.h NOTE: This file is intended to be included from librock/mstruct.h If it is not, then #include FIRST, and protect from multiple inclusion with #ifndef/#define librock_INC_FSIL_H See http://www.mibsoftware.com/librock/ for documentation and original copies of this software. This software shall be distributed with the implementation source code according to the license terms of the source code. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ /* This is one of the general purpose librock memory structures. It is used as the basis of other memory structures and classes. For an overview of all of them see librock/mstruct.h. FSIL - Fixed size indexed list. All items in the list are a fixed size. A pointer to an item is obtained by using an integer index. Items can be inserted or deleted in the list. (To replace an item in the list, get a pointer, and modify the item in place.) A FSIL is implemented in separate blocks of allocated memory, each holding a number of items. This allows adding items without copying the entire list. Sequential items may not be adjacent in memory. Typical uses: base for FIFO and LIFO stacks and queues, and lists. Efficient implementation of arrays with variable number of elements. C++ wrapper: CFsil */ #ifdef librock_PTR #ifdef __cplusplus extern "C" { #endif struct librock_FSIL_s { /* Fixed size item list */ struct librock_FSIL_s librock_PTR *flink; int inseg; /* number in this FSIL */ int isize; int grow; /* Also: max allowed in this FSIL segment */ int shrink; }; struct librock_FSIL_s librock_PTR *librock_FSILnew(int isize,int grow,int shrink); void librock_FSILdelete(struct librock_FSIL_s librock_PTR *m); void librock_PTR *librock_FSILpoint(struct librock_FSIL_s librock_PTR *m,long index); int librock_FSILdeleteAt(struct librock_FSIL_s librock_PTR *m,long index); void librock_PTR *librock_FSILinsertAt(struct librock_FSIL_s librock_PTR *m,long index); void librock_PTR *librock_FSILinsertcopyAt(struct librock_FSIL_s librock_PTR *m,long index,void librock_PTR *init); long librock_FSILitemcount(struct librock_FSIL_s librock_PTR *m); #ifdef __cplusplus }; #endif /**************************************************************/ #ifdef __cplusplus class librock_CFsil { /* Made librock_PTR 6-11-96 */ public: librock_CFsil(int size) {m_isize = size;m_pData =0;Clear();}; ~librock_CFsil() { librock_FSILdelete(m_pData); }; protected: int m_isize; /* Size of each item */ long m_count; struct librock_FSIL_s librock_PTR *m_pData; public: void librock_PTR *InsertBefore(long index) { if (index == m_count+1) { /* Special case */ index--; } m_count++; return librock_FSILinsertAt(m_pData,index); }; struct librock_FSIL_s librock_PTR *fsil() { return m_pData; }; /* 6-28-96 */ void librock_PTR *Append() { return InsertBefore(m_count); }; void DeleteAt(long index) { m_count--;librock_FSILdeleteAt(m_pData,index);}; void Clear() { if (m_pData) {librock_FSILdelete(m_pData);} m_count = 0;m_pData = librock_FSILnew(m_isize,64,32);}; void librock_PTR *PtrAt(long index) { return librock_FSILpoint(m_pData,index); }; long count() { return m_count;}; }; class librock_CInPlaceFsil { /* 8-1-96 Use when an item once defined must not move in memory. This does not implement InsertBefore() and Delete() operations */ protected: librock_CFsil m_data; /* Don't allow cast to CFsil */ public: librock_CInPlaceFsil(int size); public: void librock_PTR *Append() { return m_data.Append(); }; void Clear() { m_data.Clear(); }; void librock_PTR *PtrAt(long index) { return m_data.PtrAt(index); }; long count() { return m_data.count();}; }; /**************************************************************/ /**************************************************************/ class librock_PTR librock_CFsilLongList : public librock_CFsil { public: librock_CFsilLongList(); int ItemCount() { return (int) librock_CFsil::count(); }; /* We use CFsilLongList::Clear */ void InsertBefore(int ind,long l) { long librock_PTR *p = (long librock_PTR *)librock_CFsil::InsertBefore(ind); if (p) { *p = l; } }; void Append(long l) { long librock_PTR *p = (long librock_PTR *)librock_CFsil::Append(); if (p) { *p = l; } } }; /**************************************************************/ /* Expandable stack */ class librock_PTR librock_CFsilLIFO : public librock_CFsil { public: librock_CFsilLIFO(int size); /* Clear() uses CFsil.Clear() */ void librock_PTR *Push() { return librock_CFsil::Append(); }; librock_bool Pop(void librock_PTR *dest); long Depth() { return librock_CFsil::count(); }; void librock_PTR *Pick(long i) { return librock_CFsil::PtrAt(Depth() - i - 1); } }; inline librock_CFsilLIFO::librock_CFsilLIFO(int size) : librock_CFsil(size) { }; #ifdef librock_EXPERIMENTAL_CLongLIFO_x001 class librock_PTR librock_CLongLIFO : public librock_CFsil { public: librock_CLongLIFO(); /* Clear() uses CFsil.Clear() */ char *Push(long val) { *((long librock_PTR *) Append()) = val; return 0; /* To fix: no error return for now... */ }; long Pop(); long Depth() { return librock_CFsil::count(); }; long Pick(long i); }; inline librock_CLongLIFO::librock_CLongLIFO() : librock_CFsil(sizeof(long)) { }; #endif// librock_EXPERIMENTAL_CLongLIFO_x001 /**************************************************************/ #endif /* __cplusplus */ #endif /* $Log: fsil.h,v $ Revision 1.3 2002/02/10 02:29:36 forrest@mibsoftware.com rights=#1 Standardized chg log. EXPERIMENTAL bracketing Revision 1.2 2002/01/29 04:40:02 forrest@mibsoftware.com rights=#1 Prep for publish. API clean up, TAB, space at eol removal Revision 1.1 2001/01/06 19:36:16 forrest@mibsoftware.com rights=#1 Initial import to CVS rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */