source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/ntl/detail/Other.h @ 1526

Revision 1526, 6.8 KB checked in by gumbau, 18 years ago (diff)

Updated modules to the new interface and the new simplification algorithm improvements.

Line 
1////////////////////////////////////////////////////////////////////////////////
2// NTL Nonstandard Template Library for C++
3// Copyright (c) 2003 by Miroslav Fidler, Tomas Rylek
4//
5// Created by Miroslav Fidler, Tomas Rylek, cxl@volny.cz
6//
7// Permission to use, copy, modify, distribute and sell this software for any
8//     purpose is hereby granted without fee, provided that the above copyright
9//     notice appear in all copies and that both that copyright notice and this
10//     permission notice appear in supporting documentation.
11// The author makes no representations about the suitability of this software
12//     for any purpose. It is provided "as is"
13//     without express or implied warranty.
14////////////////////////////////////////////////////////////////////////////////
15
16#ifdef UPP
17template <class T>
18T& Single() {
19        static T *p;
20        if(!p) {
21                CriticalSection::LockMain __;
22                if(!p) {
23                        static T o;
24                        p = &o;
25                }
26        }
27        return *p;
28}
29#endif
30
31template <class T>
32class One : Moveable< One<T> >, DeepCopyOption< One<T> > {
33        mutable T  *ptr;
34
35        void        Free()                     { if(ptr && ptr != (T*)1) delete ptr; }
36        void        Chk() const                { ASSERT(ptr != (T*)1); }
37        void        ChkP() const               { Chk(); ASSERT(ptr); }
38        void        Pick(pick_ One<T>& data)   { T *p = data.ptr; data.ptr = (T*)1; ptr = p; }
39
40public:
41        void        Attach(T *data)            { Free(); ptr = data; }
42        T          *Detach() pick_             { ChkP(); T *t = ptr; ptr = NULL; return t; }
43        T          *operator-() pick_          { return Detach(); }
44        void        Clear()                    { Free(); ptr = NULL; }
45
46        void        operator=(T *data)         { Attach(data); }
47        void        operator=(pick_ One<T>& d) { Free(); Pick(d); }
48
49        const T    *operator->() const         { ChkP(); return ptr; }
50        T          *operator->()               { ChkP(); return ptr; }
51        const T    *operator~() const          { Chk(); return ptr; }
52        T          *operator~()                { Chk(); return ptr; }
53        const T&    operator*() const          { ChkP(); return *ptr; }
54        T&          operator*()                { ChkP(); return *ptr; }
55
56        bool        IsPicked() const           { return ptr == (T*)1; }
57        bool        IsEmpty() const            { Chk(); return !ptr; }
58
59        operator bool() const                  { return ptr; }
60
61        One()                                  { ptr = NULL; }
62        One(T *newt)                           { ptr = newt; }
63        One(pick_ One<T>& p)                   { Pick(p); }
64        One(const One<T>& p, int)              { ptr = p.IsEmpty() ? NULL : DeepCopyNew(*p); }
65        ~One()                                 { Free(); }
66};
67
68//# System dependent
69template <class T>
70class Mitor : Moveable< Mitor<T> > {
71        union {
72                mutable unsigned   count;
73                mutable Vector<T> *vector;
74        };
75        byte elem0[sizeof(T)];
76       
77        T&        Get(int i) const;
78        void      Pick(pick_ Mitor& m);
79        void      Copy(const Mitor& m);
80        void      Chk() const               { ASSERT(count != 2); }
81
82public:
83        T&        operator[](int i)         { return Get(i); }
84        const T&  operator[](int i) const   { return Get(i); }
85        int       GetCount() const;
86        T&        Add();
87        void      Add(const T& x);
88        void      Clear();
89        void      Shrink();
90       
91        Mitor(pick_ Mitor& m)               { Pick(m); }
92        void operator=(pick_ Mitor& m)      { Clear(); Pick(m); }
93
94        Mitor(Mitor& m, int)                { Copy(m); }
95        void operator<<=(const Mitor& m)    { Clear(); Copy(m); }
96
97        Mitor()                             { count = 0; }
98        ~Mitor()                            { Clear(); }
99};
100
101template <class T>
102T& Mitor<T>::Get(int i) const
103{
104        ASSERT(i >= 0 && i < GetCount());
105        return i == 0 ? *(T*)elem0 : (*const_cast<Vector<T>*>(vector))[i - 1];
106}
107
108template <class T>
109void Mitor<T>::Pick(pick_ Mitor& m)
110{
111        m.Chk();
112        vector = m.vector;
113        memcpy(&elem0, &m.elem0, sizeof(T));
114        m.count = 2;
115}
116
117template <class T>
118void Mitor<T>::Copy(const Mitor& m)
119{
120        m.Chk();
121        if(m.count > 0)
122                DeepCopyConstruct((T*)elem0, m.elem0);
123        if(m.count > 1)
124                vector = new Vector<T>(m.vector, 1);
125}
126
127template <class T>
128int Mitor<T>::GetCount() const
129{
130        Chk();
131        return count > 1 ? vector->GetCount() + 1 : count;
132}
133
134template <class T>
135T& Mitor<T>::Add()
136{
137        Chk();
138        if(count == 0) {
139                count = 1;
140                return *new(elem0) T;
141        }
142        if(count == 1)
143                vector = new Vector<T>;
144        return vector->Add();
145}
146
147template <class T>
148void Mitor<T>::Add(const T& x)
149{
150        Chk();
151        if(count == 0) {
152                count = 1;
153                new((T*) elem0) T(x);
154        }
155        else {
156                if(count == 1)
157                        vector = new Vector<T>;
158                vector->Add(x);
159        }
160}
161
162template <class T>
163void Mitor<T>::Clear()
164{
165        if(count > 2)
166                delete vector;
167        if(count && count != 2)
168                ((T*)elem0)->T::~T();
169        count = 0;
170}
171
172template <class T>
173void Mitor<T>::Shrink()
174{
175        if(count > 2)
176                vector->Shrink();
177}
178
179//#
180template <class T, int N = 1>
181class Link {
182protected:
183        T *prev[N];
184        T *next[N];
185
186        void LPN(int i)                      { prev[i]->next[i] = next[i]->prev[i] = (T *)this; }
187
188public:
189        T       *GetPtr()                    { return (T *) this; }
190        const T *GetPtr() const              { return (const T *) this; }
191        T       *GetNext(int i = 0)          { return next[i]; }
192        T       *GetPrev(int i = 0)          { return prev[i]; }
193        const T *GetNext(int i = 0) const    { return next[i]; }
194        const T *GetPrev(int i = 0) const    { return prev[i]; }
195
196        void LinkSelf(int i = 0)             { next[i] = prev[i] = (T *)this; }
197        void LinkSelfAll()                   { for(int i = 0; i < N; i++) LinkSelf(i); }
198        void Unlink(int i = 0)               { next[i]->prev[i] = prev[i]; prev[i]->next[i] = next[i];
199                                               LinkSelf(i); }
200        void UnlinkAll()                     { for(int i = 0; i < N; i++) Unlink(i); }
201        void LinkBefore(Link *n, int i = 0)  { next[i] = (T *)n; prev[i] = next[i]->prev[i]; LPN(i); }
202        void LinkAfter(Link *p, int i = 0)   { prev[i] = (T *)p; next[i] = prev[i]->next[i]; LPN(i); }
203
204        T   *InsertNext(int i = 0)           { T *x = new T; x->LinkAfter(this, i); return x; }
205        T   *InsertPrev(int i = 0)           { T *x = new T; x->LinkBefore(this, i); return x; }
206
207        void DeleteList(int i = 0)           { while(next[i] != GetPtr()) delete next[i]; }
208
209        bool InList(int i = 0) const         { return next[i] != GetPtr(); }
210        bool IsEmpty(int i = 0) const        { return !InList(i); }
211
212        Link()                               { LinkSelfAll(); }
213        ~Link()                              { UnlinkAll(); }
214
215private:
216        Link(const Link&);
217        void operator=(const Link&);
218
219public:
220#ifdef _DEBUG
221        void Dump() {
222                for(T *t = GetNext(); t != this; t = t->GetNext())
223                        LOG(t);
224                LOG("-------------------------------------");
225        }
226#endif
227};
228
229template <class T, int N = 1>
230class LinkOwner : public Link<T, N> {
231public:
232        ~LinkOwner()                         { Link<T, N>::DeleteList(); }
233};
Note: See TracBrowser for help on using the repository browser.