Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Disk-Based Container Objects


April 1998/Disk-Based Container Objects/Listing 1

Listing 1: listman.h — Disk-based container list manager

#ifndef __LISTMAN_H
#define __LISTMAN_H
#include "dfile.h"

typedef ulong filepos;     /* file position type */

template <class T> struct TDSingleLinkNode  {
    filepos Next, Free;
    T Data;
    };

template <class T> struct TDDoubleLinkNode  {
    filepos Prev, Next, Free;
    T Data;
    };

template <class T> struct TDBinarySearchTreeNode  {
    filepos Left, Right, Free;
    T Data;
    };

template <class Nd, class FM> class TDListManager
{   /* Nd is node type, FM is File manager type */
    FM *_Fm;   /* -> file manager */
    struct header {
        filepos Head, Tail, Free;
        ulong NodesAllocated;   /* total count */
        ulong ItemCount;        /* active count */
        } _Hdr;

    #define HDRSIZE  sizeof(header)

   /* BEG_OFFSET places 1st node at specified offset
    * into container file (see vectman.h).....
    */
    #ifndef BEG_OFFSET
    #define BEG_OFFSET  HDRSIZE
    #endif

  public:
    TDListManager(const char *fname, int nn, int m) {
        _Fm = new FM( nn, sizeof(Nd), fname, m );
        _Hdr.Head = _Hdr.Tail = _Hdr.Free = 0;
        _Hdr.ItemCount = _Hdr.NodesAllocated = 0;
        _Fm->ReadAt( 0, (void *) &_Hdr, HDRSIZE );
    }
    ~TDListManager() {  WriteHeader();
                        delete _Fm;    }
    filepos Head() const { return _Hdr.Head; }
    void   Head(filepos hd) { _Hdr.Head = hd; }
    filepos Tail() const { return _Hdr.Tail; }
    void   Tail(filepos tl) { _Hdr.Tail = tl; }
    ulong ItemCount() const { return _Hdr.ItemCount; }
    void ItemCount( ulong c ) {  _Hdr.ItemCount = c; }
    ulong ListSize() const
        { return _Hdr.NodesAllocated; }
    int WriteHeader();
    void GetFree( Nd& );
    void PutFree( Nd& );
    virtual int ReadNode(Nd& t, ulong pos)
        { return _Fm->ReadNode( (void *) &t, pos ); }
    virtual int WriteNode(const Nd& t, ulong pos)    {
        return _Fm->WriteNode((const void *) &t, pos );
        }
};

template <class Nd,class FM>
int TDListManager<Nd,FM>::WriteHeader()
{
    if( _Fm->tempused() )
        return 1;   /* return if non-persistent */
    return _Fm->WriteAt( 0, (void *) &_Hdr, HDRSIZE );
}

template <class Nd,class FM>
void TDListManager<Nd,FM>::GetFree( Nd& node )
{   /* Get address of next free node in the file;
     * Assign new address to node.Free ....... */
    if( _Hdr.Free == 0 )   /* get new node  */
        node.Free = ( _Hdr.NodesAllocated++
                         * sizeof(Nd) ) + BEG_OFFSET;
    else    {  /* Get next free node from file... */
        filepos _free = _Hdr.Free;
        ReadNode( node, _free );
        _Hdr.Free = node.Free;
        node.Free = _free;
    }
}

template <class Nd,class FM>
void TDListManager<Nd,FM>::PutFree( Nd& node )    {
    filepos _free = node.Free;
    node.Free = _Hdr.Free;
    _Hdr.Free = _free;
    WriteNode( node, _free );
}
#endif // EOF

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.