source: trunk/ADOL-C/src/buffer_temp.h @ 42

Last change on this file since 42 was 42, checked in by awalther, 10 years ago

set svn keywords property

  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     struct_buf.h
4 Revision: $Id: buffer_temp.h 42 2009-07-15 18:37:17Z awalther $
5 Contents: - template class for linked list of Type buffers with constant length
6             per buffer
7           - intended to be used with structs
8 
9 Copyright (c) Andreas Kowarz
10 
11 This file is part of ADOL-C. This software is provided as open source.
12 Any use, reproduction, or distribution of the software constitutes
13 recipient's acceptance of the terms of the accompanying license file.
14   
15----------------------------------------------------------------------------*/
16
17#if !defined(ADOLC_STRUCT_BUF_H)
18#define ADOLC_STRUCT_BUF_H 1
19
20#include <common.h>
21#include <taping_p.h>
22
23#if defined(__cplusplus)
24/****************************************************************************/
25/*                                                          This is all C++ */
26
27#include <cstdlib>
28
29#define BUFFER Buffer<SubBufferElement, FunctionPointer, _subBufferSize>
30#define BUFFER_TEMPLATE template<class SubBufferElement,\
31   class FunctionPointer, IndexType _subBufferSize>
32
33typedef locint IndexType;
34
35BUFFER_TEMPLATE class Buffer {
36
37    typedef void (*InitFunctionPointer) (SubBufferElement *subBufferElement);
38
39    typedef struct SubBuffer {
40        SubBufferElement elements[_subBufferSize];
41        struct SubBuffer *nextSubBuffer;
42    }
43    SubBuffer;
44
45public:
46    inline Buffer() {
47        firstSubBuffer = NULL;
48        numEntries = 0;
49        subBufferSize = _subBufferSize;
50        initFunction = NULL;
51    }
52    inline Buffer(InitFunctionPointer _initFunction) {
53        firstSubBuffer = NULL;
54        numEntries = 0;
55        subBufferSize = _subBufferSize;
56        initFunction = _initFunction;
57    }
58    inline ~Buffer();
59
60    inline void init(InitFunctionPointer _initFunction) {
61        initFunction = _initFunction;
62    }
63    SubBufferElement *append(FunctionPointer functionPointer);
64    SubBufferElement *getElement(IndexType index);
65
66private:
67    SubBuffer *firstSubBuffer;
68    InitFunctionPointer initFunction;
69    IndexType subBufferSize;
70    IndexType numEntries;
71};
72
73BUFFER_TEMPLATE
74BUFFER::~Buffer() {
75    SubBuffer *tmpSubBuffer = NULL;
76
77    while (firstSubBuffer != NULL) {
78        tmpSubBuffer = firstSubBuffer;
79        firstSubBuffer = firstSubBuffer->nextSubBuffer;
80        delete tmpSubBuffer;
81    }
82}
83
84BUFFER_TEMPLATE
85SubBufferElement *BUFFER::append(FunctionPointer functionPointer) {
86    SubBuffer *currentSubBuffer=firstSubBuffer, *previousSubBuffer=NULL;
87    IndexType index, tmp=numEntries;
88
89    if (functionPointer==NULL) fail(ADOLC_BUFFER_NULLPOINTER_FUNCTION);
90
91    while (tmp>=subBufferSize) {
92        previousSubBuffer=currentSubBuffer;
93        currentSubBuffer=currentSubBuffer->nextSubBuffer;
94        tmp-=subBufferSize;
95    }
96    if (currentSubBuffer==NULL) {
97        currentSubBuffer=new SubBuffer;
98        if (firstSubBuffer==NULL) firstSubBuffer=currentSubBuffer;
99        else previousSubBuffer->nextSubBuffer=currentSubBuffer;
100        currentSubBuffer->nextSubBuffer=NULL;
101    }
102    index=tmp;
103
104    if (initFunction!=NULL)
105        initFunction(&(currentSubBuffer->elements[index]));
106
107    currentSubBuffer->elements[index].index=numEntries;
108    currentSubBuffer->elements[index].function=functionPointer;
109    ++numEntries;
110
111    return &currentSubBuffer->elements[index];
112}
113
114BUFFER_TEMPLATE
115SubBufferElement *BUFFER::getElement(IndexType index) {
116    SubBuffer *currentSubBuffer=firstSubBuffer;
117
118    if (index>=numEntries) fail(ADOLC_BUFFER_INDEX_TO_LARGE);
119    while (index>=subBufferSize) {
120        currentSubBuffer=currentSubBuffer->nextSubBuffer;
121        index-=subBufferSize;
122    }
123    return &currentSubBuffer->elements[index];
124}
125
126#endif /* __cplusplus */
127
128#endif /* ADOLC_STRUCT_BUF_H */
129
Note: See TracBrowser for help on using the repository browser.