Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/boost/numeric/ublas/storage_sparse.hpp @ 30

Last change on this file since 30 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 18.4 KB
Line 
1//
2//  Copyright (c) 2000-2002
3//  Joerg Walter, Mathias Koch
4//
5//  Permission to use, copy, modify, distribute and sell this software
6//  and its documentation for any purpose is hereby granted without fee,
7//  provided that the above copyright notice appear in all copies and
8//  that both that copyright notice and this permission notice appear
9//  in supporting documentation.  The authors make no representations
10//  about the suitability of this software for any purpose.
11//  It is provided "as is" without express or implied warranty.
12//
13//  The authors gratefully acknowledge the support of
14//  GeNeSys mbH & Co. KG in producing this work.
15//
16
17#ifndef _BOOST_UBLAS_STORAGE_SPARSE_
18#define _BOOST_UBLAS_STORAGE_SPARSE_
19
20#include <map>
21
22#include <boost/numeric/ublas/storage.hpp>
23
24
25namespace boost { namespace numeric { namespace ublas {
26
27    namespace detail {
28
29        template<class I, class T, class C>
30        BOOST_UBLAS_INLINE
31        I lower_bound (const I &begin, const I &end, const T &t, C compare) {
32            // t <= *begin <=> ! (*begin < t)
33            if (begin == end || ! compare (*begin, t))
34                return begin;
35            if (compare (*(end - 1), t))
36                return end;
37            return std::lower_bound (begin, end, t, compare);
38        }
39        template<class I, class T, class C>
40        BOOST_UBLAS_INLINE
41        I upper_bound (const I &begin, const I &end, const T &t, C compare) {
42            if (begin == end || compare (t, *begin))
43                return begin;
44            // (*end - 1) <= t <=> ! (t < *end)
45            if (! compare (t, *(end - 1)))
46                return end;
47            return std::upper_bound (begin, end, t, compare);
48        }
49
50        template<class P>
51        struct less_pair {
52            BOOST_UBLAS_INLINE
53            bool operator () (const P &p1, const P &p2) {
54                return p1.first < p2.first;
55            }
56        };
57        template<class T>
58        struct less_triple {
59            BOOST_UBLAS_INLINE
60            bool operator () (const T &t1, const T &t2) {
61                return t1.first.first < t2.first.first ||
62                       (t1.first.first == t2.first.first && t1.first.second < t2.first.second);
63            }
64        };
65
66    }
67
68#ifdef BOOST_UBLAS_STRICT_MAP_ARRAY
69    template<class A>
70    class sparse_storage_element:
71       public container_reference<A> {
72    public:
73        typedef A array_type;
74        typedef typename A::key_type index_type;
75        typedef typename A::mapped_type data_value_type;
76        // typedef const data_value_type &data_const_reference;
77        typedef typename type_traits<data_value_type>::const_reference data_const_reference;
78        typedef data_value_type &data_reference;
79        typedef typename A::value_type value_type;
80        typedef value_type *pointer;
81
82        // Construction and destruction
83        BOOST_UBLAS_INLINE
84        sparse_storage_element (array_type &a, pointer it):
85            container_reference<array_type> (a), it_ (it), i_ (it->first), d_ (it->second), dirty_ (false) {}
86        BOOST_UBLAS_INLINE
87        sparse_storage_element (array_type &a, index_type i):
88            container_reference<array_type> (a), it_ (), i_ (i), d_ (), dirty_ (false) {
89            pointer it = (*this) ().find (i_);
90            if (it == (*this) ().end ())
91                it = (*this) ().insert ((*this) ().end (), value_type (i_, d_));
92            d_ = it->second;
93        }
94        BOOST_UBLAS_INLINE
95        ~sparse_storage_element () {
96            if (dirty_) {
97                if (! it_)
98                    it_ = (*this) ().find (i_);
99                BOOST_UBLAS_CHECK (it_ != (*this) ().end (), internal_logic ());
100                it_->second = d_;
101            }
102        }
103
104        // Element access - only if data_const_reference is defined
105        BOOST_UBLAS_INLINE
106        typename data_value_type::data_const_reference
107        operator [] (index_type i) const {
108            return d_ [i];
109        }
110
111        // Assignment
112        BOOST_UBLAS_INLINE
113        sparse_storage_element &operator = (const sparse_storage_element &p) {
114            // Overide the implict copy assignment
115            d_ = p.d_;
116            dirty_ = true;
117            return *this;
118        }
119        template<class D>
120        BOOST_UBLAS_INLINE
121        sparse_storage_element &operator = (const D &d) {
122            d_ = d;
123            dirty_ = true;
124            return *this;
125        }
126        template<class D>
127        BOOST_UBLAS_INLINE
128        sparse_storage_element &operator += (const D &d) {
129            d_ += d;
130            dirty_ = true;
131            return *this;
132        }
133        template<class D>
134        BOOST_UBLAS_INLINE
135        sparse_storage_element &operator -= (const D &d) {
136            d_ -= d;
137            dirty_ = true;
138            return *this;
139        }
140        template<class D>
141        BOOST_UBLAS_INLINE
142        sparse_storage_element &operator *= (const D &d) {
143            d_ *= d;
144            dirty_ = true;
145            return *this;
146        }
147        template<class D>
148        BOOST_UBLAS_INLINE
149        sparse_storage_element &operator /= (const D &d) {
150            d_ /= d;
151            dirty_ = true;
152            return *this;
153        }
154
155        // Comparison
156        template<class D>
157        BOOST_UBLAS_INLINE
158        bool operator == (const D &d) const {
159            return d_ == d;
160        }
161        template<class D>
162        BOOST_UBLAS_INLINE
163        bool operator != (const D &d) const {
164            return d_ != d;
165        }
166
167        // Conversion
168        BOOST_UBLAS_INLINE
169        operator data_const_reference () const {
170            return d_;
171        }
172
173        // Swapping
174        BOOST_UBLAS_INLINE
175        void swap (sparse_storage_element p) {
176            if (this != &p) {
177                dirty_ = true;
178                p.dirty_ = true;
179                std::swap (d_, p.d_);
180            }
181        }
182        BOOST_UBLAS_INLINE
183        friend void swap (sparse_storage_element p1, sparse_storage_element p2) {
184            p1.swap (p2);
185        }
186
187    private:
188        pointer it_;
189        index_type i_;
190        data_value_type d_;
191        bool dirty_;
192    };
193#endif
194
195
196    // Default map type is simply forwarded to std::map
197    // FIXME should use ALLOC for map but std::allocator of std::pair<const I, T> and std::pair<I,T> fail to compile
198    template<class I, class T, class ALLOC>
199    class map_std : public std::map<I, T /*, ALLOC */> {
200    };
201
202
203    // Map array
204    //  Implementation requires pair<I, T> allocator definition (without const)
205    template<class I, class T, class ALLOC>
206    class map_array {
207    public:
208        typedef ALLOC allocator_type;
209        typedef typename ALLOC::size_type size_type;
210        typedef typename ALLOC::difference_type difference_type;
211        typedef std::pair<I,T> value_type;
212        typedef I key_type;
213        typedef T mapped_type;
214        typedef const value_type &const_reference;
215        typedef value_type &reference;
216        typedef const value_type *const_pointer;
217        typedef value_type *pointer;
218        // Iterators simply are pointers.
219        typedef const_pointer const_iterator;
220        typedef pointer iterator;
221
222        typedef const T &data_const_reference;
223#ifndef BOOST_UBLAS_STRICT_MAP_ARRAY
224        typedef T &data_reference;
225#else
226        typedef sparse_storage_element<map_array> data_reference;
227#endif
228
229        // Construction and destruction
230        BOOST_UBLAS_INLINE
231        map_array (const ALLOC &a = ALLOC()):
232            alloc_(a), capacity_ (0), size_ (0) {
233                data_ = 0;
234        }
235        BOOST_UBLAS_INLINE
236        map_array (const map_array &c):
237            alloc_ (c.alloc_), capacity_ (c.size_), size_ (c.size_) {
238            if (capacity_) {
239                data_ = alloc_.allocate (capacity_);
240                std::uninitialized_copy (data_, data_ + capacity_, c.data_);
241                // capacity != size_ requires uninitialized_fill (size_ to capacity_)
242            }
243            else
244                data_ = 0;
245        }
246        BOOST_UBLAS_INLINE
247        ~map_array () {
248            if (capacity_) {
249                std::for_each (data_, data_ + capacity_, static_destroy);
250                alloc_.deallocate (data_, capacity_);
251            }
252        }
253
254    private:
255        // Resizing - implicitly exposses uninitialized (but default constructed) mapped_type
256        BOOST_UBLAS_INLINE
257        void resize (size_type size) {
258            BOOST_UBLAS_CHECK (size_ <= capacity_, internal_logic ());
259            if (size > capacity_) {
260                const size_type capacity = size << 1;
261                BOOST_UBLAS_CHECK (capacity, internal_logic ());
262                pointer data = alloc_.allocate (capacity);
263                std::uninitialized_copy (data_, data_ + (std::min) (size, size_), data);
264                std::uninitialized_fill (data + (std::min) (size, size_), data + capacity, value_type ());
265
266                if (capacity_) {
267                    std::for_each (data_, data_ + capacity_, static_destroy);
268                    alloc_.deallocate (data_, capacity_);
269                }
270                capacity_ = capacity;
271                data_ = data;
272            }
273            size_ = size;
274            BOOST_UBLAS_CHECK (size_ <= capacity_, internal_logic ());
275        }
276    public:
277
278        // Reserving
279        BOOST_UBLAS_INLINE
280        void reserve (size_type capacity) {
281            BOOST_UBLAS_CHECK (size_ <= capacity_, internal_logic ());
282            // Reduce capacity_ if size_ allows
283            BOOST_UBLAS_CHECK (capacity >= size_, bad_size ());
284            pointer data;
285            if (capacity) {
286                data = alloc_.allocate (capacity);
287                std::uninitialized_copy (data_, data_ + size_, data);
288                std::uninitialized_fill (data + size_, data + capacity, value_type ());
289            }
290            else
291                data = 0;
292               
293            if (capacity_) {
294                std::for_each (data_, data_ + capacity_, static_destroy);
295                alloc_.deallocate (data_, capacity_);
296            }
297            capacity_ = capacity;
298            data_ = data;
299            BOOST_UBLAS_CHECK (size_ <= capacity_, internal_logic ());
300        }
301
302        // Random Access Container
303        BOOST_UBLAS_INLINE
304        size_type size () const {
305            return size_;
306        }
307        BOOST_UBLAS_INLINE
308        size_type capacity () const {
309            return capacity_;
310        }
311        BOOST_UBLAS_INLINE
312        size_type max_size () const {
313            return 0; //TODO
314        }
315       
316        BOOST_UBLAS_INLINE
317        bool empty () const {
318            return size_ == 0;
319        }
320           
321        // Element access
322        BOOST_UBLAS_INLINE
323        data_reference operator [] (key_type i) {
324#ifndef BOOST_UBLAS_STRICT_MAP_ARRAY
325            pointer it = find (i);
326            if (it == end ())
327                it = insert (end (), value_type (i, mapped_type (0)));
328            BOOST_UBLAS_CHECK (it != end (), internal_logic ());
329            return it->second;
330#else
331            return data_reference (*this, i);
332#endif
333        }
334
335        // Assignment
336        BOOST_UBLAS_INLINE
337        map_array &operator = (const map_array &a) {
338            if (this != &a) {
339                resize (a.size_);
340                std::copy (a.data_, a.data_ + a.size_, data_);
341            }
342            return *this;
343        }
344        BOOST_UBLAS_INLINE
345        map_array &assign_temporary (map_array &a) {
346            swap (a);
347            return *this;
348        }
349
350        // Swapping
351        BOOST_UBLAS_INLINE
352        void swap (map_array &a) {
353            if (this != &a) {
354                std::swap (capacity_, a.capacity_);
355                std::swap (data_, a.data_);
356                std::swap (size_, a.size_);
357            }
358        }
359        BOOST_UBLAS_INLINE
360        friend void swap (map_array &a1, map_array &a2) {
361            a1.swap (a2);
362        }
363
364        // Element insertion and deletion
365       
366        // From Back Insertion Sequence concept
367        // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.   
368        iterator push_back (iterator it, const value_type &p) {
369            if (size () == 0 || (it = end () - 1)->first < p.first) {
370                resize (size () + 1);
371                *(it = end () - 1) = p;
372                return it;
373            }
374            external_logic ().raise ();
375            return it;
376        }
377        // Form Unique Associative Container concept
378        // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.   
379        std::pair<iterator,bool> insert (const value_type &p) {
380            iterator it = detail::lower_bound (begin (), end (), p, detail::less_pair<value_type> ());
381            if (it != end () && it->first == p.first)
382                return std::make_pair (it, false);
383            difference_type n = it - begin ();
384            BOOST_UBLAS_CHECK (size () == 0 || size () == size_type (n), external_logic ());
385            resize (size () + 1);
386            it = begin () + n;    // allow for invalidation
387            std::copy_backward (it, end () - 1, end ());
388            *it = p;
389            return std::make_pair (it, true);
390        }
391        // Form Sorted Associative Container concept
392        // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.   
393        iterator insert (iterator hint, const value_type &p) {
394            return insert (p).first;
395        }
396        // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.   
397        void erase (iterator it) {
398            BOOST_UBLAS_CHECK (begin () <= it && it < end (), bad_index ());
399            std::copy (it + 1, end (), it);
400            resize (size () - 1);
401        }
402        // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.   
403        void erase (iterator it1, iterator it2) {
404            BOOST_UBLAS_CHECK (begin () <= it1 && it1 < it2 && it2 <= end (), bad_index ());
405            std::copy (it2, end (), it1);
406            resize (size () - (it2 - it1));
407        }
408        // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.   
409        void clear () {
410            resize (0);
411        }
412
413        // Element lookup
414        // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.   
415        const_iterator find (key_type i) const {
416            const_iterator it (detail::lower_bound (begin (), end (), value_type (i, mapped_type (0)), detail::less_pair<value_type> ()));
417            if (it == end () || it->first != i)
418                it = end ();
419            return it;
420        }
421        // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.   
422        iterator find (key_type i) {
423            iterator it (detail::lower_bound (begin (), end (), value_type (i, mapped_type (0)), detail::less_pair<value_type> ()));
424            if (it == end () || it->first != i)
425                it = end ();
426            return it;
427        }
428        // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.   
429        const_iterator lower_bound (key_type i) const {
430            return detail::lower_bound (begin (), end (), value_type (i, mapped_type (0)), detail::less_pair<value_type> ());
431        }
432        // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.   
433        iterator lower_bound (key_type i) {
434            return detail::lower_bound (begin (), end (), value_type (i, mapped_type (0)), detail::less_pair<value_type> ());
435        }
436
437        BOOST_UBLAS_INLINE
438        const_iterator begin () const {
439            return data_;
440        }
441        BOOST_UBLAS_INLINE
442        const_iterator end () const {
443            return data_ + size_;
444        }
445
446        BOOST_UBLAS_INLINE
447        iterator begin () {
448            return data_;
449        }
450        BOOST_UBLAS_INLINE
451        iterator end () {
452            return data_ + size_;
453        }
454
455        // Reverse iterators
456        typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
457        typedef std::reverse_iterator<iterator> reverse_iterator;
458
459        BOOST_UBLAS_INLINE
460        const_reverse_iterator rbegin () const {
461            return const_reverse_iterator (end ());
462        }
463        BOOST_UBLAS_INLINE
464        const_reverse_iterator rend () const {
465            return const_reverse_iterator (begin ());
466        }
467        BOOST_UBLAS_INLINE
468        reverse_iterator rbegin () {
469            return reverse_iterator (end ());
470        }
471        BOOST_UBLAS_INLINE
472        reverse_iterator rend () {
473            return reverse_iterator (begin ());
474        }
475
476        // Allocator
477        allocator_type get_allocator () {
478            return alloc_;
479        }
480
481    private:
482        // Provide destroy as a non member function
483        BOOST_UBLAS_INLINE
484        static void static_destroy (reference p) {
485            (&p) -> ~value_type ();
486        }
487        ALLOC alloc_;
488        size_type capacity_;
489        pointer data_;
490        size_type size_;
491    };
492
493
494    namespace detail {
495        template<class A, class T>
496        struct map_traits {
497            typedef typename A::mapped_type &reference;
498        };
499        template<class I, class T, class ALLOC>
500        struct map_traits<map_array<I, T, ALLOC>, T > {
501            typedef typename map_array<I, T, ALLOC>::data_reference reference;
502        };
503
504        // reserve helpers for map_array and generic maps
505        // ISSUE should be in map_traits but want to use on all compilers
506
507        template<class M>
508        BOOST_UBLAS_INLINE
509        void map_reserve (M &/* m */, typename M::size_type /* capacity */) {
510        }
511        template<class I, class T, class ALLOC>
512        BOOST_UBLAS_INLINE
513        void map_reserve (map_array<I, T, ALLOC> &m, typename map_array<I, T, ALLOC>::size_type capacity) {
514            m.reserve (capacity);
515        }
516
517        template<class M>
518        struct map_capacity_traits {
519            typedef typename M::size_type type ;
520            type operator() ( M const& m ) const {
521               return m.size ();
522            }
523        } ;
524
525        template<class I, class T, class ALLOC>
526        struct map_capacity_traits< map_array<I, T, ALLOC> > {
527            typedef typename map_array<I, T, ALLOC>::size_type type ;
528            type operator() ( map_array<I, T, ALLOC> const& m ) const {
529               return m.capacity ();
530            }
531        } ;
532
533        template<class M>
534        BOOST_UBLAS_INLINE
535        typename map_capacity_traits<M>::type map_capacity (M const& m) {
536            return map_capacity_traits<M>() ( m );
537        }
538    }
539
540}}}
541
542#endif
Note: See TracBrowser for help on using the repository browser.