source: Sophya/trunk/SophyaLib/BaseTools/ndatablock.h@ 960

Last change on this file since 960 was 948, checked in by ansari, 25 years ago

ajout de stdlib.h pour definition de size_t - Reza 16/4/2000

File size: 8.7 KB
RevLine 
[268]1// This may look like C code, but it is really -*- C++ -*-
2// Gestion de block de donnees avec partage de references
3// C.Magneville 04/99
4// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
[245]5#ifndef NDATABLOCK_H
6#define NDATABLOCK_H
7
8#include "machdefs.h"
[948]9#include <stdlib.h> /* pour que size_t soit defini */
[268]10#include "anydataobj.h"
11#include <iostream.h>
[245]12
[552]13namespace SOPHYA {
[268]14
[944]15////////////////////////////////////////////////////////////////
16//// ------------------- Class Bridge ----------------------- //
17////////////////////////////////////////////////////////////////
18/*!
19 \class Bridge
20 \ingroup SysTools
21 This class is use by NDataBlock. It allows sharing of datas
22 with external structures : by example, if you want to connect
23 a Blitz data structure with a NDataBlock or a TMatrix or ...
24 \sa NDataBlock
25*/
26
[245]27// Classe pour permettre de partager des donnees avec
28// un autre systeme de gestion de references (ex avec Blitz)
[944]29//! Empty class which allows data sharing with external structures (for NDataBlock)
[245]30class Bridge {
31public:
32 Bridge() { }
33 virtual ~Bridge() { }
34};
35
[944]36////////////////////////////////////////////////////////////////
37//// ----------------- Class NDataBlock --------------------- //
38////////////////////////////////////////////////////////////////
39
[246]40// classe de container avec partage de reference
[944]41//! Container of data with reference sharing
[245]42template <class T>
[268]43class NDataBlock : public AnyDataObj {
[245]44
45public:
46
[944]47 // Methodes statiques pour debug.
48 static void SetPrintDebug(int prtdbglevel=1);
49 static void ResetDebug(size_t nallocdata=0, size_t nallocsref=0);
50 static void PrintDebug();
51
[246]52 // Creation / destruction
[245]53 NDataBlock(size_t n);
54 NDataBlock(size_t n, T* data, Bridge* br=NULL);
55 NDataBlock();
[268]56 NDataBlock(const NDataBlock<T>& a);
57 NDataBlock(const NDataBlock<T>& a,bool share);
[245]58 virtual ~NDataBlock();
59
60 // Temporaire?
[944]61 //! Return true if data block is temporay
[268]62 inline bool IsTemp(void) const {return mIsTemp;}
[944]63 //! Set temporary caracter of data block
[289]64 inline void SetTemp(bool temp=false) const {mIsTemp = temp;}
[245]65
[257]66 // Gestion taille/Remplissage
[268]67 void Clone(const NDataBlock<T>& a);
[289]68 void CloneOrShare(const NDataBlock<T>& a);
69 void Share(const NDataBlock<T>& a);
[257]70 void FillFrom(size_t n,T* data);
[944]71 //! Re-set all data values to \b v
[289]72 inline void Reset(T v=0)
73 {if(mSz==0) return; T *p=Begin(),*pe=End(); while(p<pe) *p++=v;}
[502]74
75 // ReSize redimmensionne une structure pour "n" donnees.
76 // Les donnees precedentes sont perdues (pour cette classe)
77 // et le nouveau tableau mis a zero. La nouvelle structure de
78 // donnees n'a qu'une reference (celle de cette classe).
[944]79 //! Re-size the data structure
80 /*! Old datas are lost (for this class). The new values are set to zero.
81 The new data structure has only one reference (itself!). */
[289]82 inline void ReSize(size_t n) {Alloc(n);}
[502]83
84 void Realloc(size_t nnew,bool force=false);
[245]85
86 // Informations pointeur/data
[944]87 //! Return pointer on data structure.
[245]88 inline T* Data()
89 {if(mSRef) return mSRef->data; else return NULL;}
[944]90 //! Return pointer on data structure.
[268]91 inline T* Data() const
92 {if(mSRef) return mSRef->data; else return NULL;}
[944]93 //! Return the size of the data structure
[249]94 inline size_t Size() const {return mSz;}
[944]95 //! Return the \b i th element of the data structure
[268]96 inline T& operator()(size_t i) {return *(mSRef->data+i);}
[944]97 //! Return the \b i th element of the data structure
[268]98 inline T operator()(size_t i) const {return *(mSRef->data+i);}
[944]99 //! Return pointer to the beginning of the data structure.
[245]100 inline T* Begin() {return mSRef->data;}
[944]101 //! Return pointer to the beginning of the data structure.
[268]102 inline T const* Begin() const {return mSRef->data;}
[944]103 //! Return pointer to the end of the data structure.
[245]104 inline T* End() {return mSRef->data+mSz;}
[944]105 //! Return pointer to the end of the data structure.
[245]106 inline T const* End() const {return mSRef->data+mSz;}
[944]107 //! Return the number of references to the data structure
[502]108 inline size_t NRef() const {if(mSRef) return 0; else return mSRef->nref;}
[245]109
110 // Impression
[268]111 void Print(ostream& os, size_t i1=0,size_t n=10) const;
[944]112 //! print infos and datas (from \b i1 to \b i2) on stdout.
[268]113 inline void Print(size_t i1=0,size_t n=0) const {Print(cout,i1,n);}
[245]114
[268]115 //
116 T Sum(size_t i1=0,size_t n=0) const;
117 T Product(size_t i1=0,size_t n=0) const;
118
[289]119 // Surcharge d'operateurs INPLACE: A = x , A = B , A @= x , A @= B
[268]120 NDataBlock<T>& operator = (const NDataBlock<T>& a);
[245]121 NDataBlock<T>& operator = (T v);
122
123 NDataBlock<T>& operator += (T b);
124 NDataBlock<T>& operator -= (T b);
125 NDataBlock<T>& operator *= (T b);
126 NDataBlock<T>& operator /= (T b);
127
[268]128 NDataBlock<T>& operator += (const NDataBlock<T>& a);
129 NDataBlock<T>& operator -= (const NDataBlock<T>& a);
130 NDataBlock<T>& operator *= (const NDataBlock<T>& a);
131 NDataBlock<T>& operator /= (const NDataBlock<T>& a);
[245]132
[289]133 // Surcharge d'operateurs: C = A @ x , C = A @ B
[268]134 NDataBlock<T> Add(T b) const;
135 NDataBlock<T> Sub(T b) const;
136 NDataBlock<T> SubInv(T b) const;
137 NDataBlock<T> Mul(T b) const;
138 NDataBlock<T> Div(T b) const;
139 NDataBlock<T> DivInv(T b) const;
[245]140
[268]141 NDataBlock<T> Add(const NDataBlock<T>& b) const;
142 NDataBlock<T> Sub(const NDataBlock<T>& b) const;
143 NDataBlock<T> SubInv(const NDataBlock<T>& b) const;
144 NDataBlock<T> Mul(const NDataBlock<T>& b) const;
145 NDataBlock<T> Div(const NDataBlock<T>& b) const;
146 NDataBlock<T> DivInv(const NDataBlock<T>& b) const;
[257]147
[245]148protected:
[944]149 //! NDREF structure for reference management
150 typedef struct {
151 size_t nref; //!< Number of references to the data structure
152 T* data; //!< Pointer to data structure itself
153 Bridge* bridge; //!< Pointer to a bridge for the data structure
154 } NDREF;
[245]155
[773]156 void Alloc(size_t n,T* data=NULL,Bridge* br=NULL, bool zero=true);
[245]157 void Delete(void);
158
[944]159 static int Debug_NDataBlock; //!< DEBUG: set debug level (all type<T> classes)
160 static size_t NallocData; //!< DEBUG: number of allocations (all type<T> classes)
161 static size_t NallocSRef; //!< DEBUG: number of references (all type<T> classes)
162
163 size_t mSz; //!< size of data structure
164 NDREF* mSRef; //!< NDREF structure for reference management
165 mutable bool mIsTemp; //!< true if class is temporary
[245]166};
167
[944]168//! Define operator \<\< for printing
[259]169template<class T>
[268]170inline ostream& operator << (ostream& os, const NDataBlock<T>& a)
[269]171 {a.Print(os); return(os);}
[944]172
173//! Add a constant to datas and return NDataBlock : ND = NDa + b
[268]174template<class T>
175inline NDataBlock<T> operator + (const NDataBlock<T>& a,T b)
[259]176 {return a.Add(b);}
[944]177//! Add a constant to datas and return NDataBlock : ND = b + NDa
[259]178template<class T>
[268]179inline NDataBlock<T> operator + (T b,const NDataBlock<T>& a)
[259]180 {return a.Add(b);}
[944]181//! Substract a constant to datas and return NDataBlock : ND = NDa - b
[259]182template<class T>
[268]183inline NDataBlock<T> operator - (const NDataBlock<T>& a,T b)
[259]184 {return a.Sub(b);}
[944]185//! Substract a constant to datas and return NDataBlock : ND = b - NDa
[259]186template<class T>
[268]187inline NDataBlock<T> operator - (T b,const NDataBlock<T>& a)
[259]188 {return a.SubInv(b);}
[944]189//! Multiply datas by a constant and return NDataBlock : ND = NDa * b
[259]190template<class T>
[268]191inline NDataBlock<T> operator * (const NDataBlock<T>& a,T b)
[259]192 {return a.Mul(b);}
[944]193//! Multiply datas by a constant and return NDataBlock : ND = b * NDa
[259]194template<class T>
[268]195inline NDataBlock<T> operator * (T b,const NDataBlock<T>& a)
[259]196 {return a.Mul(b);}
[944]197//! Divide datas by a constant and return NDataBlock : ND = NDa / b
[259]198template<class T>
[268]199inline NDataBlock<T> operator / (const NDataBlock<T>& a,T b)
[259]200 {return a.Div(b);}
[944]201//! Divide a constant by datas and return NDataBlock : ND = b / NDa
[259]202template<class T>
[268]203inline NDataBlock<T> operator / (T b,const NDataBlock<T>& a)
[259]204 {return a.DivInv(b);}
[245]205
[944]206//! Add datas of two data blocks and return NDataBlock : ND = NDa + NDb
[259]207template<class T>
[268]208inline NDataBlock<T> operator + (const NDataBlock<T>& a,const NDataBlock<T>& b)
[265]209 {return a.Add(b);}
[944]210//! Substract datas of two data blocks and return NDataBlock : ND = NDa - NDb
[259]211template<class T>
[268]212inline NDataBlock<T> operator - (const NDataBlock<T>& a,const NDataBlock<T>& b)
[259]213 {return a.Sub(b);}
[944]214//! Multiply datas of two data blocks and return NDataBlock : ND = NDa * NDb
[259]215template<class T>
[268]216inline NDataBlock<T> operator * (const NDataBlock<T>& a,const NDataBlock<T>& b)
[259]217 {return a.Mul(b);}
[944]218//! Divide datas of two data blocks and return NDataBlock : ND = NDa / NDb
[259]219template<class T>
[268]220inline NDataBlock<T> operator / (const NDataBlock<T>& a,const NDataBlock<T>& b)
[259]221 {return a.Div(b);}
[245]222
[268]223} // Fin du namespace
[257]224
[245]225#endif
Note: See TracBrowser for help on using the repository browser.