This file is indexed.

/usr/include/osgEarth/Containers is in libosgearth-dev 2.7.0+dfsg-2+b3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
* Copyright 2015 Pelican Mapping
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>
*/
#ifndef OSGEARTH_CONTAINERS_H
#define OSGEARTH_CONTAINERS_H 1

#include <osgEarth/Common>
#include <osgEarth/ThreadingUtils>
#include <osg/ref_ptr>
#include <osg/observer_ptr>
#include <osg/State>
#include <list>
#include <vector>
#include <set>
#include <map>

namespace osgEarth
{
    /**
     * A std::map-like map that uses a vector and a getUID method for keying.
     * DATA must have a getUID() method.
     */
    template<typename KEY,typename DATA>
    struct vector_map
    {
        struct ENTRY {
            inline const KEY&  key()  const { return _key; }
            inline const DATA& data() const { return _data; }
            inline DATA&       data()       { return _data; }
            KEY   _key;
            DATA _data;
        };
        typedef std::vector<ENTRY> container_t;

        //typedef std::vector<KEY>  keys_t;
        //typedef std::vector<DATA> container_t;

        typedef typename container_t::iterator       iterator;
        typedef typename container_t::const_iterator const_iterator;

        container_t _container;        

        inline DATA& operator[](const KEY& key) {
            for(unsigned i=0; i<_container.size(); ++i) {
                if (_container[i]._key == key) {
                    return _container[i]._data;
                }
            }
            _container.resize(_container.size()+1);
            //_container.push_back(ENTRY());
            _container.back()._key = key;
            return _container.back()._data;
        }

        inline DATA* find(const KEY& key) {
            for(unsigned i=0; i<_container.size(); ++i) {
                if (_container[i]._key == key) {
                    return &_container[i]._data;
                }
            }
            return 0L;
        }

        inline const DATA* find(const KEY& key) const {
            for(unsigned i=0; i<_container.size(); ++i) {
                if (_container[i]._key == key) {
                    return &_container[i]._data;
                }
            }
            return 0L;
        }

        inline const_iterator begin() const { return _container.begin(); }
        inline const_iterator end()   const { return _container.end(); }
        inline iterator begin()             { return _container.begin(); }
        inline iterator end()               { return _container.end(); }

        inline bool empty() const { return _container.empty(); }

        inline void clear() { _container.clear(); }
        
        //iterator erase(iterator& i) {
        //    iterator j = i; ++j;
        //    *i = _data.at(_data.size()-1);
        //    _data.resize(_data.size()-1);
        //    return j;
        //}

        inline void erase(const KEY& key) {
            for(unsigned i=0; i<_container.size(); ++i) {
                if (_container[i]._key == key) {
                    if (i+1 < _container.size()) {
                        _container[i] = _container[_container.size()-1];
                    }
                    _container.resize(_container.size()-1);
                    break;
                }
            }
        }

        inline int size() const { return _container.size(); }

        template<typename InputIterator>
        void insert(InputIterator a, InputIterator b) {
            for(InputIterator i = a; i != b; ++i) (*this)[i->first] = i->second;
        }
    };

    /**
     * A std::map-like container that is faster than std::map for small amounts
     * of data accessed by a single user
     */
    template<typename KEY, typename DATA>
    struct fast_map
    {
        typedef std::pair<KEY,DATA> entry_t;
        typedef std::list<entry_t>  list_t;

        typedef typename list_t::iterator       iterator;
        typedef typename list_t::const_iterator const_iterator;

        list_t _data;
        KEY    _lastKey;

        DATA& operator[] (const KEY& key) {
            for( iterator i = _data.begin(); i != _data.end(); ++i ) {
                if ( i->first == key ) {
                    if ( _lastKey == key && i != _data.begin() ) {
                        _data.insert(begin(), *i);
                        _data.erase(i);
                        return _data.front().second;
                    }
                    else {
                        _lastKey = key;
                        return i->second;
                    }
                }
            }
            _data.push_back(entry_t(key,DATA()));
            return _data.back().second;
        }

        iterator find( const KEY& key ) {
            for( iterator i = _data.begin(); i != _data.end(); ++i ) {
                if ( i->first == key ) {
                    return i;
                }
            }
            return end();
        }

        const_iterator find( const KEY& key ) const {
            for( const_iterator i = _data.begin(); i != _data.end(); ++i ) {
                if ( i->first == key ) {
                    return i;
                }
            }
            return end();
        }

        const_iterator begin() const { return _data.begin(); }
        const_iterator end() const { return _data.end(); }
        iterator begin() { return _data.begin(); }
        iterator end() { return _data.end(); }

        bool empty() const { return _data.empty(); }

        void clear() { _data.clear(); }
        
        iterator erase(iterator& i) { return _data.erase( i ); }

        void erase(const KEY& key) {
            iterator i = find(key);
            if ( i != end() ) {
                erase( i );
            }
        }

        int size() const { return _data.size(); }

        template<typename InputIterator>
        void insert(InputIterator a, InputIterator b) {
            for(InputIterator i = a; i != b; ++i) (*this)[i->first] = i->second;
        }
    };

    //------------------------------------------------------------------------

    struct CacheStats
    {
    public:
        CacheStats( unsigned entries, unsigned maxEntries, unsigned queries, float hitRatio )
            : _entries(entries), _maxEntries(maxEntries), _queries(queries), _hitRatio(hitRatio) { }

        /** dtor */
        virtual ~CacheStats() { }

        unsigned _entries;
        unsigned _maxEntries;
        unsigned _queries;
        float    _hitRatio;
    };

    //------------------------------------------------------------------------

    /**
     * Least-recently-used cache class.
     * K = key type, T = value type
     *
     * usage:
     *    LRUCache<K,T> cache;
     *    cache.put( key, value );
     *    LRUCache.Record rec = cache.get( key );
     *    if ( rec.valid() )
     *        const T& value = rec.value();
     */
    template<typename K, typename T, typename COMPARE=std::less<K> >
    class LRUCache
    {
    public:
        struct Record {
            Record() : _valid(false) { }
            Record(const T& value) : _value(value), _valid(true) { }
            bool valid() const { return _valid; }
            const T& value() const { return _value; }
        private:
            bool _valid;
            T    _value;
            friend class LRUCache;
        };

    protected:
        typedef typename std::list<K>::iterator      lru_iter;
        typedef typename std::list<K>                lru_type;
        typedef typename std::pair<T, lru_iter>      map_value_type;
        typedef typename std::map<K, map_value_type> map_type;
        typedef typename map_type::iterator          map_iter;

        map_type _map;
        lru_type _lru;
        unsigned _max;
        unsigned _buf;
        unsigned _queries;
        unsigned _hits;
        bool     _threadsafe;
        mutable Threading::Mutex _mutex;

    public:
        LRUCache( unsigned max =100 ) : _max(max), _threadsafe(false) {
            _buf = _max/10;
            _queries = 0;
            _hits = 0;
        }
        LRUCache( bool threadsafe, unsigned max =100 ) : _max(max), _threadsafe(threadsafe) {
            _buf = _max/10;
            _queries = 0;
            _hits = 0;
        }

        /** dtor */
        virtual ~LRUCache() { }

        void insert( const K& key, const T& value ) {
            if ( _threadsafe ) {
                Threading::ScopedMutexLock lock(_mutex);
                insert_impl( key, value );
            }
            else {
                insert_impl( key, value );
            }
        }

        bool get( const K& key, Record& out ) {
            if ( _threadsafe ) {
                Threading::ScopedMutexLock lock(_mutex);
                get_impl( key, out );
            }
            else {
                get_impl( key, out );
            }
            return out.valid();
        }

        bool has( const K& key ) {
            if ( _threadsafe ) {
                Threading::ScopedMutexLock lock(_mutex);
                return has_impl( key );
            }
            else {
                return has_impl( key );
            }
        }

        void erase( const K& key ) {
            if ( _threadsafe ) {
                Threading::ScopedMutexLock lock(_mutex);
                erase_impl( key );
            }
            else {
                erase_impl( key );
            }
        }

        void clear() {
            if ( _threadsafe ) {
                Threading::ScopedMutexLock lock(_mutex);
                clear_impl();
            }
            else {
                clear_impl();
            }
        }

        void setMaxSize( unsigned max ) {
            if ( _threadsafe ) {
                Threading::ScopedMutexLock lock(_mutex);
                setMaxSize_impl( max );
            }
            else {
                setMaxSize_impl( max );
            }
        }

        unsigned getMaxSize() const {
            return _max;
        }

        CacheStats getStats() const {
            return CacheStats(
                _lru.size(), _max, _queries, _queries > 0 ? (float)_hits/(float)_queries : 0.0f );
        }

    private:

        void insert_impl( const K& key, const T& value ) {
            map_iter mi = _map.find( key );
            if ( mi != _map.end() ) {
                _lru.erase( mi->second.second );
                mi->second.first = value;
                _lru.push_back( key );
                mi->second.second = _lru.end();
                mi->second.second--;
            }
            else {
                _lru.push_back( key );
                lru_iter last = _lru.end(); last--;
                _map[key] = std::make_pair(value, last);
            }

            if ( _lru.size() > _max ) {
                for( unsigned i=0; i < _buf; ++i ) {
                    const K& key = _lru.front();
                    _map.erase( key );
                    _lru.pop_front();
                }
            }
        }

        void get_impl( const K& key, Record& result ) {
            _queries++;
            map_iter mi = _map.find( key );
            if ( mi != _map.end() ) {
                _lru.erase( mi->second.second );
                _lru.push_back( key );
                lru_iter new_iter = _lru.end(); new_iter--;
                mi->second.second = new_iter;
                _hits++;
                result._value = mi->second.first;
                result._valid = true;
                //return Record( &(mi->second.first) );
            }
            //else {
            //    return Record( 0L );
            //}
        }

        bool has_impl( const K& key ) {
            return _map.find( key ) != _map.end();
        }

        void erase_impl( const K& key ) {
            map_iter mi = _map.find( key );
            if ( mi != _map.end() ) {
                _lru.erase( mi->second.second );
                _map.erase( mi );
            }
        }

        void clear_impl() {
            _lru.clear();
            _map.clear();
            _queries = 0;
            _hits = 0;
        }

        void setMaxSize_impl( unsigned max ) {
            _max = max;
            _buf = max/10;
            while( _lru.size() > _max ) {
                const K& key = _lru.front();
                _map.erase( key );
                _lru.pop_front();
            }
        }

    };

    //--------------------------------------------------------------------

    /**
     * Same of osg::MixinVector, but with a superclass template parameter.
     */
    template<class ValueT, class SuperClass>
    class MixinVector : public SuperClass
    {
        typedef typename std::vector<ValueT> vector_type;
    public:
        typedef typename vector_type::allocator_type allocator_type;
        typedef typename vector_type::value_type value_type;
        typedef typename vector_type::const_pointer const_pointer;
        typedef typename vector_type::pointer pointer;
        typedef typename vector_type::const_reference const_reference;
        typedef typename vector_type::reference reference;
        typedef typename vector_type::const_iterator const_iterator;
        typedef typename vector_type::iterator iterator;
        typedef typename vector_type::const_reverse_iterator const_reverse_iterator;
        typedef typename vector_type::reverse_iterator reverse_iterator;
        typedef typename vector_type::size_type size_type;
        typedef typename vector_type::difference_type difference_type;

        explicit MixinVector() : _impl()
        {
        }

        explicit MixinVector(size_type initial_size, const value_type& fill_value = value_type())
        : _impl(initial_size, fill_value)
        {
        }

        template<class InputIterator>
        MixinVector(InputIterator first, InputIterator last)
        : _impl(first, last)
        {
        }

        MixinVector(const vector_type& other)
        : _impl(other)
        {
        }

        MixinVector(const MixinVector& other)
        : _impl(other._impl)
        {
        }

        MixinVector& operator=(const vector_type& other)
        {
            _impl = other;
            return *this;
        }

        MixinVector& operator=(const MixinVector& other)
        {
            _impl = other._impl;
            return *this;
        }

        virtual ~MixinVector() {}

        void clear() { _impl.clear(); }
        void resize(size_type new_size, const value_type& fill_value = value_type()) { _impl.resize(new_size, fill_value); }
        void reserve(size_type new_capacity) { _impl.reserve(new_capacity); }
        
        void swap(vector_type& other) { _impl.swap(other); }
        void swap(MixinVector& other) { _impl.swap(other._impl); }

        bool empty() const { return _impl.empty(); }
        size_type size() const { return _impl.size(); }
        size_type capacity() const { return _impl.capacity(); }
        size_type max_size() const { return _impl.max_size(); }
        allocator_type get_allocator() const { return _impl.get_allocator(); }

        const_iterator begin() const { return _impl.begin(); }
        iterator begin() { return _impl.begin(); }
        const_iterator end() const { return _impl.end(); }
        iterator end() { return _impl.end(); }

        const_reverse_iterator rbegin() const { return _impl.rbegin(); }
        reverse_iterator rbegin() { return _impl.rbegin(); }
        const_reverse_iterator rend() const { return _impl.rend(); }
        reverse_iterator rend() { return _impl.rend(); }

        const_reference operator[](size_type index) const { return _impl[index]; }
        reference operator[](size_type index) { return _impl[index]; }

        const_reference at(size_type index) const { return _impl.at(index); }
        reference at(size_type index) { return _impl.at(index); }

        void assign(size_type count, const value_type& value) { _impl.assign(count, value); }
        template<class Iter>
        void assign(Iter first, Iter last) { _impl.assign(first, last); }

        void push_back(const value_type& value) { _impl.push_back(value); }
        void pop_back() { _impl.pop_back(); }

        iterator erase(iterator where) { return _impl.erase(where); }
        iterator erase(iterator first, iterator last) { return _impl.erase(first, last); }

        iterator insert(iterator where, const value_type& value) { return _impl.insert(where, value); }

        template<class InputIterator>
        void insert(iterator where, InputIterator first, InputIterator last)
        {
            _impl.insert(where, first, last);
        }

        void insert(iterator where, size_type count, const value_type& value)
        {
            _impl.insert(where, count, value);
        }

        const_reference back() const { return _impl.back(); }
        reference back() { return _impl.back(); }
        const_reference front() const { return _impl.front(); }
        reference front() { return _impl.front(); }

        vector_type& asVector() { return _impl; }
        const vector_type& asVector() const { return _impl; }

        friend inline bool operator==(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl == right._impl; }
        friend inline bool operator==(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl == right; }
        friend inline bool operator==(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left == right._impl; }

        friend inline bool operator!=(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl != right._impl; }
        friend inline bool operator!=(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl != right; }
        friend inline bool operator!=(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left != right._impl; }

        friend inline bool operator<(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl < right._impl; }
        friend inline bool operator<(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl < right; }
        friend inline bool operator<(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left < right._impl; }

        friend inline bool operator>(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl > right._impl; }
        friend inline bool operator>(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl > right; }
        friend inline bool operator>(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left > right._impl; }

        friend inline bool operator<=(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl <= right._impl; }
        friend inline bool operator<=(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl <= right; }
        friend inline bool operator<=(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left <= right._impl; }

        friend inline bool operator>=(const MixinVector<ValueT,SuperClass>& left, const MixinVector<ValueT,SuperClass>& right) { return left._impl >= right._impl; }
        friend inline bool operator>=(const MixinVector<ValueT,SuperClass>& left, const std::vector<ValueT>& right) { return left._impl >= right; }
        friend inline bool operator>=(const std::vector<ValueT>& left, const MixinVector<ValueT,SuperClass>& right) { return left >= right._impl; }

    private:
        vector_type _impl;
    };


    /** Template for per-thread data storage */
    template<typename T>
    struct PerThread
    {
        T& get() {
            Threading::ScopedMutexLock lock(_mutex);
            return _data[Threading::getCurrentThreadId()];
        }
    private:
        std::map<unsigned,T> _data;
        Threading::Mutex     _mutex;
    };
    

    /** Template for thread safe per-object data storage */
    template<typename KEY, typename DATA>
    struct PerObjectMap
    {
        DATA& get(KEY k)
        {
            {
                osgEarth::Threading::ScopedReadLock readLock(_mutex);
                typename std::map<KEY,DATA>::iterator i = _data.find(k);
                if ( i != _data.end() )
                    return i->second;
            }
            {
                osgEarth::Threading::ScopedWriteLock lock(_mutex);
                typename std::map<KEY,DATA>::iterator i = _data.find(k);
                if ( i != _data.end() )
                    return i->second;
                else
                    return _data[k];
            }
        }

        void remove(KEY k)
        {
            osgEarth::Threading::ScopedWriteLock exclusive(_mutex);
            _data.erase( k );
        }

    private:
        std::map<KEY,DATA>                  _data;
        osgEarth::Threading::ReadWriteMutex _mutex;
    };

    /** Template for thread safe per-object data storage */
    template<typename KEY, typename DATA>
    struct PerObjectFastMap
    {
        DATA& get(KEY k)
        {
            {
                osgEarth::Threading::ScopedReadLock readLock(_mutex);
                typename osgEarth::fast_map<KEY,DATA>::iterator i = _data.find(k);
                if ( i != _data.end() )
                    return i->second;
            }
            {
                osgEarth::Threading::ScopedWriteLock lock(_mutex);
                typename osgEarth::fast_map<KEY,DATA>::iterator i = _data.find(k);
                if ( i != _data.end() )
                    return i->second;
                else
                    return _data[k];
            }
        }

        void remove(KEY k)
        {
            osgEarth::Threading::ScopedWriteLock exclusive(_mutex);
            _data.erase( k );
        }

    private:
        osgEarth::fast_map<KEY,DATA>        _data;
        osgEarth::Threading::ReadWriteMutex _mutex;
    };

    /** Template for thread safe per-object data storage */
    template<typename KEY, typename DATA>
    struct PerObjectRefMap
    {
        DATA* get(KEY k)
        {
            osgEarth::Threading::ScopedReadLock lock(_mutex);
            typename std::map<KEY,osg::ref_ptr<DATA > >::const_iterator i = _data.find(k);
            if ( i != _data.end() )
                return i->second.get();

            return 0L;
        }

        DATA* getOrCreate(KEY k, DATA* newDataIfNeeded)
        {
            osg::ref_ptr<DATA> _refReleaser = newDataIfNeeded;
            {
                osgEarth::Threading::ScopedReadLock lock(_mutex);
                typename std::map<KEY,osg::ref_ptr<DATA> >::const_iterator i = _data.find(k);
                if ( i != _data.end() )
                    return i->second.get();
            }

            {
                osgEarth::Threading::ScopedWriteLock lock(_mutex);
                typename std::map<KEY,osg::ref_ptr<DATA> >::iterator i = _data.find(k);
                if ( i != _data.end() )
                    return i->second.get();

                _data[k] = newDataIfNeeded;
                return newDataIfNeeded;
            }
        }

        void remove(KEY k)
        {
            osgEarth::Threading::ScopedWriteLock exclusive(_mutex);
            _data.erase( k );
        }

        void remove(DATA* data)
        {
            osgEarth::Threading::ScopedWriteLock exclusive(_mutex);
            for( typename std::map<KEY,osg::ref_ptr<DATA> >::iterator i = _data.begin(); i != _data.end(); ++i )
            {
                if ( i->second.get() == data )
                {
                    _data.erase( i );
                    break;
                }
            }
        }

    private:
        std::map<KEY,osg::ref_ptr<DATA> >    _data;
        osgEarth::Threading::ReadWriteMutex  _mutex;
    };

    /** Template for thread safe per-object data storage */
    template<typename KEY, typename DATA>
    struct PerObjectObsMap
    {
        DATA* get(KEY k)
        {
            osgEarth::Threading::ScopedReadLock lock(_mutex);
            typename std::map<KEY, osg::observer_ptr<DATA> >::const_iterator i = _data.find(k);
            if ( i != _data.end() )
                return i->second.get();

            return 0L;
        }

        DATA* getOrCreate(KEY k, DATA* newDataIfNeeded)
        {
            osg::ref_ptr<DATA> _refReleaser = newDataIfNeeded;
            {
                osgEarth::Threading::ScopedReadLock lock(_mutex);
                typename std::map<KEY,osg::observer_ptr<DATA> >::const_iterator i = _data.find(k);
                if ( i != _data.end() )
                    return i->second.get();
            }

            {
                osgEarth::Threading::ScopedWriteLock lock(_mutex);
                typename std::map<KEY,osg::observer_ptr<DATA> >::iterator i = _data.find(k);
                if ( i != _data.end() )
                    return i->second.get();

                _data[k] = newDataIfNeeded;
                return newDataIfNeeded;
            }
        }

        void remove(KEY k)
        {
            osgEarth::Threading::ScopedWriteLock exclusive(_mutex);
            _data.erase( k );
        }

        void remove(DATA* data)
        {
            osgEarth::Threading::ScopedWriteLock exclusive(_mutex);
            for( typename std::map<KEY,osg::observer_ptr<DATA> >::iterator i = _data.begin(); i != _data.end(); ++i )
            {
                if ( i->second.get() == data )
                {
                    _data.erase( i );
                    break;
                }
            }
        }

    private:
        std::map<KEY,osg::observer_ptr<DATA> >    _data;
        osgEarth::Threading::ReadWriteMutex       _mutex;
    };


    /** Template for thread safe observer set */
    template<typename T>
    struct ThreadSafeObserverSet
    {
        typedef void (*Functor)(T*);
        typedef void (*ConstFunctor)(const T*);

        void iterate( const Functor& f )
        {
            osgEarth::Threading::ScopedWriteLock lock(_mutex);
            for( typename std::set<T>::iterator i = _data.begin(); i != _data.end(); )
            {
                if ( i->valid() )
                {
                    f( i->get() );
                    ++i;
                }
                else
                {
                    i = _data.erase( i );
                }
            }
        }

        void iterate( const ConstFunctor& f )
        {
            osgEarth::Threading::ScopedReadLock lock(_mutex);
            for( typename std::set<T>::iterator i = _data.begin(); i != _data.end(); )
            {
                if ( i->valid() )
                {
                    f( i->get() );
                    ++i;
                }
                else
                {
                    i = _data.erase( i );
                }
            }
        }

        void insert(T* obj)
        {
            osgEarth::Threading::ScopedWriteLock lock(_mutex);
            _data.insert( obj );
        }

        void remove(T* obj)
        {
            osgEarth::Threading::ScopedWriteLock lock(_mutex);
            _data.erase( obj );
        }

    private:
        std::set<osg::observer_ptr<T> >      _data;
        osgEarth::Threading::ReadWriteMutex  _mutex;
    };
}

#endif // OSGEARTH_CONTAINERS_H