Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

mem_push.H

Go to the documentation of this file.
00001 /* Copyright 1992, Brown Computer Graphics Group.  All Rights Reserved. */
00002 
00003 /* -------------------------------------------------------------------------
00004  *
00005  *                <     File description here    >
00006  *
00007  * ------------------------------------------------------------------------- */
00008 
00009 #ifndef STD_MEM_HAS_BEEN_INCLUDED
00010 #define STD_MEM_HAS_BEEN_INCLUDED
00011 
00012 /* --------------------------    Constants     ----------------------------- */
00013 
00014 /* --------------------------      Types       ----------------------------- */
00015 
00016 typedef char          UGAgeneric;
00017 typedef UGAgeneric *UGAptr;
00018 typedef enum {
00019      STD_FALSE = 0,
00020      STD_TRUE  = 1
00021 } STDbool;
00022 
00023 /* --------------------------     Classes      ----------------------------- */
00024 
00025 /*
00026  * TITLE :  Stack/Queue Storage Class
00027  *
00028  * DESCR :  This private class provides simple queue and/or stack
00029  *       functionality for data objects.  Means of data access are
00030  *       not restricted to LIFO or FIFO; enforcement of stack/queue
00031  *       semantics is left up to child objects.
00032  *
00033  * DETAILS :   In order to make data i/o as efficient as possible with a
00034  *       minimum of reallocations, the data is stored in a series of
00035  *       rotating buffers, with byte offsets used to mark the
00036  *       current push and pop positions.
00037  */
00038 class mem_push {
00039  public:
00040     mem_push(size_t datasize = sizeof(UGAgeneric));
00041     virtual ~mem_push();
00042 
00043    STDbool is_empty () const { return num_objects == 0 ? STD_TRUE : STD_FALSE; }
00044    
00045    void remove_all() {remove_top(0, count());}
00046 
00047  protected:
00048    /*
00049     * DESCR   :   Basic pushoperations, providing double-ended data
00050     *       access. 
00051     *
00052     * DETAILS :   Note to class authors: in the current implementation,
00053     *       the methods @insert_bottom@ and @remove_top@ are more
00054     *       efficient than their counterparts and should be
00055     *       preferred. 
00056     */
00057    void   insert_top    (UGAptr, size_t count = 1);
00058    void   insert_bottom (const char *, size_t count = 1);
00059    size_t remove_top    (UGAptr = NULL, size_t count = 1);
00060    size_t peek_top      (UGAptr = NULL, size_t count = 1) const;
00061    
00062    size_t count () const { return num_objects; }
00063 
00064  private:
00065    void   increase_mem (size_t);
00066    void   decrease_mem (size_t);
00067 
00068    size_t block_index  (size_t offset) const  { return offset / block_size; }
00069    size_t block_offset (size_t offset) const  { return offset % block_size; }
00070    size_t block_left   (size_t offset) const  { return block_size - block_offset (offset); }
00071    UGAptr block_addr   (size_t offset) const  { return blocks[block_index(offset)] + 
00072                                                        block_offset (offset); }
00073 
00074    UGAptr   *blocks;
00075    size_t    num_blocks;
00076    size_t    num_objects;
00077    size_t    obj_size, block_size;
00078    size_t    top, bottom;
00079 };
00080 
00081 
00082 /*
00083  * TITLE   :   Queue memory storage
00084  */
00085 class STDmem_queue : public mem_push {
00086  public:
00087    STDmem_queue (size_t datasize = sizeof(UGAgeneric)):mem_push(datasize) {}
00088 
00089    void   put  (const char *data, size_t count = 1)       { insert_bottom(data, count); }
00090    size_t get  (UGAptr data=NULL, size_t count = 1)       { return remove_top(data,count);}
00091    size_t peek (UGAptr data=NULL, size_t count = 1) const { return peek_top(data,count); }
00092    size_t count()                                   const { return mem_push::count(); }
00093 };
00094 
00095 
00096 /*
00097  * TITLE   :   Stack memory storage
00098  */
00099 class STDmem_stack : public mem_push {
00100  public:
00101    STDmem_stack (size_t datasize = sizeof(UGAgeneric)):mem_push(datasize) {}
00102 
00103    void   push (UGAptr data,        size_t count = 1)       { insert_top(data,count); }
00104    size_t pop  (UGAptr data = NULL, size_t count = 1)       { return remove_top(data,count); }
00105    size_t peek (UGAptr data = NULL, size_t count = 1) const { return peek_top(data,count); }
00106    size_t count()                                     const { return mem_push::count(); }
00107 };
00108 
00109 
00110 /*
00111  * TITLE   :   Double-ended queue storage
00112  */
00113 class STDmem_dequeue : public mem_push {
00114  public:
00115    STDmem_dequeue (size_t datasize = sizeof(UGAgeneric)):mem_push(datasize) {}
00116 
00117    void   push (UGAptr data,        size_t count = 1)       { insert_top(data,count); }
00118    void   put  (UGAptr data,        size_t count = 1)       { insert_bottom(data,count); }
00119    size_t pop  (UGAptr data = NULL, size_t count = 1)       { return remove_top(data,count); }
00120    size_t peek (UGAptr data = NULL, size_t count = 1) const { return peek_top(data,count); }
00121    size_t count()                                     const { return mem_push::count(); }
00122 };
00123 
00124 #endif  /* STD_MEM_HAS_BEEN_INCLUDED */

Generated on Mon Sep 18 11:39:32 2006 for jot by  doxygen 1.4.4