NIST Biometric Evaluation Framework
Software components for biometric technology evaluations
be_memory_orderedmap.h
Go to the documentation of this file.
1/*
2 * This software was developed at the National Institute of Standards and
3 * Technology (NIST) by employees of the Federal Government in the course
4 * of their official duties. Pursuant to title 17 Section 105 of the
5 * United States Code, this software is not subject to copyright protection
6 * and is in the public domain. NIST assumes no responsibility whatsoever for
7 * its use by other parties, and makes no guarantees, expressed or implied,
8 * about its quality, reliability, or any other characteristic.
9 */
10
11#ifndef __ORDERED_MAP_H__
12#define __ORDERED_MAP_H__
13
14#include <iterator>
15#include <list>
16#include <memory>
17#include <unordered_map>
18
19
20namespace BiometricEvaluation
21{
22 namespace Memory
23 {
24 /* Forward declarations */
25 template<class Key, class T> class OrderedMap;
26 template<class Key, class T> class OrderedMapIterator;
27 template<class Key, class T> class OrderedMapConstIterator;
28
30 template<class Key, class T>
32 {
33 public:
34 /*
35 * Satisfy std::iterator_traits<> expectations.
36 */
37
40 std::bidirectional_iterator_tag;
42 using value_type = std::pair<Key, T>;
44 using difference_type = std::ptrdiff_t;
49
50 friend class OrderedMap<Key, T>;
51 friend class OrderedMapConstIterator<Key, T>;
52
57
63 operator*()
64 const;
65
72 const;
73
76 operator++();
77
81 int);
82
85 operator--();
86
90 int);
91
103 bool
105 const OrderedMapIterator &rhs)
106 const;
107
119 bool
121 const OrderedMapIterator &rhs)
122 const;
123
124 private:
136 const OrderedMap<Key, T> *orderedMap,
137 const typename std::list<Key>::iterator listIter);
138
140 const OrderedMap<Key, T> *_orderedMap;
142 typename std::list<Key>::iterator _listIter;
144 mutable std::pair<Key, T> _currentPair;
145 };
146
148 template<class Key, class T>
150 {
151 public:
152 /*
153 * Satisfy std::iterator_traits<> expectations.
154 */
155
158 std::bidirectional_iterator_tag;
160 using value_type = std::pair<Key, T>;
162 using difference_type = std::ptrdiff_t;
164 using pointer = const value_type*;
166 using reference = const value_type&;
167
168 friend class OrderedMap<Key, T>;
169
174 const OrderedMapIterator<Key, T> &iterator);
177
183 operator*()
184 const;
185
190 pointer
191 operator->()
192 const;
193
196 operator++();
197
201 int);
202
205 operator--();
206
210 int);
211
223 bool
225 const OrderedMapConstIterator &rhs)
226 const;
227
239 bool
241 const OrderedMapConstIterator &rhs)
242 const;
243
244 private:
256 const OrderedMap<Key, T> *orderedMap,
257 const typename std::list<Key>::iterator listIter);
258
260 const OrderedMap<Key, T> *_orderedMap;
262 mutable typename std::list<Key>::iterator _listIter;
264 mutable std::pair<Key, T> _currentPair;
265 };
266
267
272 template<class Key, class T>
274 {
275 public:
276 using container = typename std::unordered_map<Key, T>;
279
280 using size_type = typename container::size_type;
281 using value_type = typename container::value_type;
282 using key_type = Key;
283 using mapped_type = T;
284
285 using key_equal = typename container::key_equal;
286
287 friend class OrderedMapIterator<Key, T>;
288 friend class OrderedMapConstIterator<Key, T>;
289
291 OrderedMap();
292
307 bool
308 push_back(
309 const value_type &value);
310
323 void
324 erase(
325 iterator pos);
326
334 void
335 erase(
336 const Key &key);
337
343 begin();
344
350 begin()
351 const;
352
358 cbegin()
359 const;
360
367 end();
368
375 end()
376 const;
377
384 cend()
385 const;
386
392 size()
393 const;
394
408 bool
409 keyExists(
410 const Key &key)
411 const;
412
421 find(
422 const Key &key)
423 const;
424
425 std::shared_ptr<value_type>
427 const Key &key)
428 const;
429
440 T&
442 const Key &key);
443
446 key_eq()
447 const;
448
450 ~OrderedMap();
451
452 private:
454 container *_elements;
456 std::list<Key> *_ordering;
457 };
458 }
459}
460
461template<class Key, class T>
463 _elements(new container()),
464 _ordering(new std::list<Key>())
465{
466
467}
468
469template<class Key, class T>
471{
472 if (_elements != nullptr)
473 delete _elements;
474 if (_ordering != nullptr)
475 delete _ordering;
476}
477
478template<class Key, class T>
479bool
481 const value_type &value)
482{
483 if (_elements->insert(value).second) {
484 _ordering->push_back(value.first);
485 return (true);
486 } else
487 return (false);
488}
489
490template<class Key, class T>
491void
493 iterator pos)
494{
495 _ordering->remove(pos->first);
496 _elements->erase(pos);
497}
498
499template<class Key, class T>
500void
502 const Key &key)
503{
504 _ordering->remove(key);
505 _elements->erase(_elements->find(key));
506}
507
508template<class Key, class T>
511{
512 return (OrderedMapIterator<Key, T>(this, _ordering->begin()));
513}
514
515template<class Key, class T>
518 const
519{
520 return (OrderedMapIterator<Key, T>(this, _ordering->begin()));
521}
522
523template<class Key, class T>
526 const
527{
528 return (OrderedMapIterator<Key, T>(this, _ordering->begin()));
529}
530
531template<class Key, class T>
534{
535 return (OrderedMapIterator<Key, T>(this, _ordering->end()));
536}
537
538template<class Key, class T>
541 const
542{
543 return (OrderedMapIterator<Key, T>(this, _ordering->end()));
544}
545
546template<class Key, class T>
549 const
550{
551 return (OrderedMapIterator<Key, T>(this, _ordering->end()));
552}
553
554template<class Key, class T>
557 const
558{
559 return (_elements->size());
560}
561
562template<class Key, class T>
563bool
565 const Key &key)
566 const
567{
568 return (_elements->find(key) != _elements->end());
569}
570
571template<class Key, class T>
572T&
574 const Key &key)
575{
576 std::pair<typename container::iterator, bool> result =
577 _elements->insert(std::make_pair(key, T()));
578
579 if (result.second) {
580 /* New insertion */
581 _ordering->push_back(key);
582 return (result.first->second);
583 } else
584 /* Already in list */
585 return (result.first->second);
586}
587
588template<class Key, class T>
591 const Key &key)
592 const
593{
594 return (OrderedMapIterator<Key, T>(this,
595 std::find(_ordering->begin(), _ordering->end(), key)));
596}
597
598template<class Key, class T>
599std::shared_ptr<
602 const Key &key)
603 const
604{
605 typename container::const_iterator it = _elements->find(key);
606 if (it != _elements->end())
607 return (std::shared_ptr<
609 new typename OrderedMap<Key, T>::value_type(it->first,
610 it->second)));
611 return (std::shared_ptr<
613}
614
615template<class Key, class T>
618 const
619{
620 return (_elements->key_eq());
621}
622
623/*
624 * OrderedMapIterator Implementation
625 */
626
627template<class Key, class T>
629 _orderedMap(nullptr),
630 _listIter()
631{
632
633}
634
635template<class Key, class T>
637 const OrderedMap<Key, T> *orderedMap,
638 const typename std::list<Key>::iterator listIter) :
639 _orderedMap(orderedMap),
640 _listIter(listIter)
641{
642
643}
644
645template<class Key, class T>
648 const
649{
650 _currentPair = *(_orderedMap->_elements->find(*_listIter));
651 return (_currentPair);
652}
653
654template<class Key, class T>
657 const
658{
659 _currentPair = *(_orderedMap->_elements->find(*_listIter));
660 return (&_currentPair);
661}
662
663template<class Key, class T>
666{
667 ++_listIter;
668 return (*this);
669}
670
671template<class Key, class T>
674 int)
675{
676 OrderedMapIterator previousIterator(*this);
677 ++(*this);
678 return (previousIterator);
679}
680
681template<class Key, class T>
684{
685 --_listIter;
686 return (*this);
687}
688
689template<class Key, class T>
692 int)
693{
694 OrderedMapIterator previousIterator(*this);
695 --(*this);
696 return (previousIterator);
697}
698
699template<class Key, class T>
700bool
702 const OrderedMapIterator &rhs)
703 const
704{
705 return ((_orderedMap == rhs._orderedMap) &&
706 (_listIter == rhs._listIter));
707}
708
709template<class Key, class T>
710bool
712 const OrderedMapIterator &rhs)
713 const
714{
715 return (!(this->operator==(rhs)));
716}
717
718template<class Key, class T>
720{
721 /* Don't delete _orderedMap, we don't own it. */
722}
723
724/*
725 * OrderedMapConstIterator Implementation
726 */
727
728template<class Key, class T>
731 _orderedMap(nullptr),
732 _listIter()
733{
734
735}
736
737template<class Key, class T>
740 const OrderedMap<Key, T> *orderedMap,
741 const typename std::list<Key>::iterator listIter) :
742 _orderedMap(orderedMap),
743 _listIter(listIter)
744{
745
746}
747
748template<class Key, class T>
749typename
752 const
753{
754 _currentPair = *(_orderedMap->_elements->find(*_listIter));
755 return (_currentPair);
756}
757
758template<class Key, class T>
759typename
762 const
763{
764 _currentPair = *(_orderedMap->_elements->find(*_listIter));
765 return (&_currentPair);
766}
767
768template<class Key, class T>
771{
772 ++_listIter;
773 return (*this);
774}
775
776template<class Key, class T>
779 int)
780{
781 OrderedMapConstIterator previousIterator(*this);
782 ++(*this);
783 return (previousIterator);
784}
785
786template<class Key, class T>
789{
790 --_listIter;
791 return (*this);
792}
793
794template<class Key, class T>
797 int)
798{
799 OrderedMapConstIterator previousIterator(*this);
800 --(*this);
801 return (previousIterator);
802}
803
804template<class Key, class T>
805bool
807 const OrderedMapConstIterator &rhs)
808 const
809{
810 return ((_orderedMap == rhs._orderedMap) &&
811 (_listIter == rhs._listIter));
812}
813
814template<class Key, class T>
815bool
817 const OrderedMapConstIterator &rhs)
818 const
819{
820 return (!(this->operator==(rhs)));
821}
822
823template<class Key, class T>
826 const OrderedMapIterator<Key, T> &iterator) :
827 _orderedMap(iterator._orderedMap),
828 _listIter(iterator._listIter),
829 _currentPair(iterator._currentPair)
830{
831
832}
833
834template<class Key, class T>
837{
838 /* Don't delete _orderedMap, we don't own it. */
839}
840
841#endif /* __ORDERED_MAP_H__ */
OrderedMapConstIterator & operator--()
Move to the previous pair.
bool operator!=(const OrderedMapConstIterator &rhs) const
Test for iterator equality.
OrderedMapConstIterator & operator++()
Move to the next pair.
const value_type & reference
Reference to the type iterated over.
std::bidirectional_iterator_tag iterator_category
Type of iterator.
const value_type * pointer
Pointer to the type iterated over.
bool operator==(const OrderedMapConstIterator &rhs) const
Test for iterator equality.
std::ptrdiff_t difference_type
Type used to measure distance between iterators.
std::pair< Key, T > value_type
Type when dereferencing iterators.
A map where insertion order is preserved and elements are unique.
T & operator[](const Key &key)
Subscripting operator.
void erase(iterator pos)
Remove an element from the collection.
bool push_back(const value_type &value)
Insert an element at the end of the collection.
std::shared_ptr< value_type > find_quick(const Key &key) const
bool keyExists(const Key &key) const
Determine if a value exists in the container.
typename container::key_equal key_equal
typename container::size_type size_type
const OrderedMapIterator< Key, T > find(const Key &key) const
Obtain an iterator to a particular key.
typename std::unordered_map< Key, T > container
typename container::value_type value_type
OrderedMapIterator & operator++()
Move to the next pair.
bool operator!=(const OrderedMapIterator &rhs) const
Test for iterator equality.
std::ptrdiff_t difference_type
Type used to measure distance between iterators.
std::bidirectional_iterator_tag iterator_category
Type of iterator.
bool operator==(const OrderedMapIterator &rhs) const
Test for iterator equality.
std::pair< Key, T > value_type
Type when dereferencing iterators.
value_type & reference
Reference to the type iterated over.
OrderedMapIterator & operator--()
Move to the previous pair.
value_type * pointer
Pointer to the type iterated over.
This software was developed at the National Institute of Standards and Technology (NIST) by employees...