Nail to Nail Fingerprint Capture Challenge
API for participant-specific one-to-many template generation and template matching.
be_memory_autoarray.h
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/*
12 * Adapted from "c_array" from "The C++ Programming Language" by Bjarne
13 * Stroustrup (ISBN: 0201700735).
14 */
15
16#ifndef __BE_MEMORY_AUTOARRAY_H__
17#define __BE_MEMORY_AUTOARRAY_H__
18
19#include <algorithm>
20#include <cstddef>
21#include <cstdint>
22#include <cstring>
23#include <stdexcept>
24#include <utility>
25#include <vector>
26
27#include <be_error_exception.h>
28#include <be_memory_autoarrayiterator.h>
29
30namespace BiometricEvaluation
31{
32 namespace Memory
33 {
43 template<class T>
45 {
46 public:
48 using value_type = T;
50 using size_type = size_t;
51
53 using iterator = AutoArrayIterator<false, T>;
56 AutoArrayIterator<true, T>;
57
59 using reference = T&;
61 using const_reference = const T&;
62
71 operator T*();
72
81 operator const T*()
82 const;
83
98 ptrdiff_t index);
99
114 ptrdiff_t index)
115 const;
116
134 at(
135 ptrdiff_t index);
136
154 at(
155 ptrdiff_t index) const;
156
167 begin();
168
179 begin()
180 const;
181
192 cbegin()
193 const;
194
205 end();
206
217 end()
218 const;
219
230 cend()
231 const;
232
247 size()
248 const;
249
265 void
266 resize(
267 size_type new_size,
268 bool free = false);
269
287 void
288 copy(
289 const T *buffer);
290
307 void
308 copy(
309 const T *buffer,
311
329 std::vector<T>
330 to_vector()
331 const;
332
344 explicit AutoArray(
345 size_type size = 0);
346
358 AutoArray(
359 const AutoArray &copy);
360
370 AutoArray(
371 AutoArray &&rvalue)
372 noexcept;
373
381 AutoArray(
382 std::initializer_list<T> ilist);
383
399 AutoArray&
400 operator=(
401 const AutoArray &other);
402
415 AutoArray&
416 operator=(
417 AutoArray &&other)
418 noexcept(
419 noexcept(
420 std::swap(std::declval<value_type&>(),
421 std::declval<value_type&>()))
422 &&
423 noexcept(
424 std::swap(std::declval<size_type&>(),
425 std::declval<size_type&>())));
426
427
429 ~AutoArray();
430
431 private:
433 value_type *_data;
435 size_type _size;
437 size_type _capacity;
438 };
439
440 /**************************************************************/
441 /* Useful type definitions of an AutoArray of basic types. */
442 /**************************************************************/
446
448 template<typename T>
449 bool
450 operator==(
451 const AutoArray<T> &lhs,
452 const AutoArray<T> &rhs);
453
455 template<typename T>
456 bool
457 operator!=(
458 const AutoArray<T> &lhs,
459 const AutoArray<T> &rhs);
460
462 template<typename T>
463 bool
464 operator<(
465 const AutoArray<T> &lhs,
466 const AutoArray<T> &rhs);
467
469 template<typename T>
470 bool
471 operator<=(
472 const AutoArray<T> &lhs,
473 const AutoArray<T> &rhs);
474
476 template<typename T>
477 bool
478 operator>(
479 const AutoArray<T> &lhs,
480 const AutoArray<T> &rhs);
481
483 template<typename T>
484 bool
485 operator>=(
486 const AutoArray<T> &lhs,
487 const AutoArray<T> &rhs);
488 }
489}
490
491/******************************************************************************/
492/* Method implementations. */
493/******************************************************************************/
494template<class T>
497 const
498{
499 return (_size);
500}
501
502template<class T>
503void
505 size_type new_size,
506 bool free)
507{
508 /* If we've already allocated at least new_size space, then bail */
509 if (!free && (new_size <= _capacity)) {
510 _size = new_size;
511 return;
512 }
513
514 T* new_data = nullptr;
515 if (new_size != 0) {
516 new_data = new (std::nothrow) T[new_size];
517 if (new_data == nullptr)
518 throw Error::MemoryError("Could not allocate data");
519 }
520
521 /* Copy as much data as will fit into the new buffer */
522 std::copy(&_data[0], &_data[((new_size < _size) ? new_size : _size)],
523 new_data);
524
525 /* Delete the old buffer and assign the new buffer to this object */
526 if (_data != nullptr)
527 delete [] _data;
528 _data = new_data;
529 _size = _capacity = new_size;
530}
531
532template<class T>
533void
535 const T *buffer)
536{
537 std::copy(&buffer[0], &buffer[_size], _data);
538}
539
540template<class T>
541void
543 const T *buffer,
544 size_type size)
545{
546 this->resize(size);
547 std::copy(&buffer[0], &buffer[size], _data);
548}
549
550template<class T>
553 ptrdiff_t index) const
554{
555 if (index < 0)
556 throw std::out_of_range("index");
557 if ((size_type)index < _size)
558 return (_data[index]);
559
560 throw std::out_of_range("index");
561}
562
563template<class T>
566 ptrdiff_t index)
567{
568 if (index < 0)
569 throw std::out_of_range("index");
570 if ((size_type)index < _size)
571 return (_data[index]);
572
573 throw std::out_of_range("index");
574}
575
576template<class T>
577typename std::vector<T>
579 const
580{
581 return {this->cbegin(), this->cend()};
582}
583
584/******************************************************************************/
585/* Conversion Operators. */
586/******************************************************************************/
587template<class T>
589{
590 return (_data);
591}
592
593template<class T>
595 const
596{
597 return (_data);
598}
599
600/******************************************************************************/
601/* Operator Overloads. */
602/******************************************************************************/
603template<class T>
606 ptrdiff_t index)
607{
608 return (_data[index]);
609}
610
611template<class T>
614 ptrdiff_t index)
615 const
616{
617 return (_data[index]);
618}
619
620template<class T>
624{
625 if (this != &other) {
626 _size = _capacity = other._size;
627 if (_data != nullptr) {
628 delete [] _data;
629 _data = nullptr;
630 }
631 if (_size != 0) {
632 _data = new (std::nothrow) T[_size];
633 if (_data == nullptr)
634 throw Error::MemoryError("Could not "
635 "allocate data");
636 std::copy(&(other._data[0]), &(other._data[_size]),
637 _data);
638 }
639 }
640
641 return (*this);
642}
643
644template<class T>
648 noexcept(
649 noexcept(
650 std::swap(std::declval<value_type&>(), std::declval<value_type&>())) &&
651 noexcept(
652 std::swap(std::declval<size_type&>(), std::declval<size_type&>())))
653{
654 using std::swap;
655
656 swap(_size, other._size);
657 swap(_capacity, other._capacity);
658 swap(_data, other._data);
659
660 return (*this);
661}
662
663/******************************************************************************/
664/* Iterators. */
665/******************************************************************************/
666template<class T>
669{
670 return (iterator(this, 0));
671}
672
673template<class T>
676 const
677{
678 return (this->cbegin());
679}
680
681template<class T>
684 const
685{
686 return (const_iterator(this, 0));
687}
688
689template<class T>
692{
693 if (this->size() > std::numeric_limits<typename BiometricEvaluation::
694 Memory::AutoArrayIterator<false, T>::difference_type>::max())
695 throw BiometricEvaluation::Error::StrategyError{"AutoArray too "
696 "large to represent end iterator"};
697
698 return (iterator(this,
699 static_cast<typename BiometricEvaluation::Memory::
700 AutoArrayIterator<false, T>::difference_type>(this->size())));
701}
702
703template<class T>
706 const
707{
708 return (this->cend());
709}
710
711template<class T>
714 const
715{
716 if (this->size() > std::numeric_limits<typename BiometricEvaluation::
717 Memory::AutoArrayIterator<true, T>::difference_type>::max())
718 throw BiometricEvaluation::Error::StrategyError{"AutoArray too "
719 "large to represent end iterator"};
720
721 return (const_iterator(this,
722 static_cast<typename BiometricEvaluation::Memory::
723 AutoArrayIterator<true, T>::difference_type>(this->size())));
724}
725
726/******************************************************************************/
727/* Constructors. */
728/******************************************************************************/
729template<class T>
731 size_type size) :
732 _data(nullptr),
733 _size(size),
734 _capacity(size)
735{
736 if (_size != 0) {
737 _data = new (std::nothrow) T[_size];
738 if (_data == nullptr)
739 throw Error::MemoryError("Could not allocate data");
740 }
741}
742
743template<class T>
745 const AutoArray& copy) :
746 _data(nullptr),
747 _size(copy._size),
748 _capacity(copy._size)
749{
750 if (_size != 0) {
751 _data = new (std::nothrow) T[_size];
752 if (_data == nullptr)
753 throw Error::MemoryError("Could not allocate data");
754 std::copy(&(copy._data[0]), &(copy._data[_size]), _data);
755 }
756}
757
758template<class T>
760 AutoArray &&rvalue)
761 noexcept :
762 _data(rvalue._data),
763 _size(rvalue._size),
764 _capacity(rvalue._capacity)
765{
766 /* Modify for a speedy destruction */
767 rvalue._data = nullptr;
768 rvalue._capacity = 0;
769 rvalue._size = 0;
770}
771
772template<class T>
774 std::initializer_list<T> ilist) : AutoArray(ilist.size())
775{
776 std::copy(ilist.begin(), ilist.end(), _data);
777}
778
779/******************************************************************************/
780/* Destructor. */
781/******************************************************************************/
782template<class T>
784{
785 if (_data != nullptr)
786 delete [] _data;
787}
788
789/******************************************************************************/
790/* Comparison operators. */
791/******************************************************************************/
792
793template<typename T>
794bool
795BiometricEvaluation::Memory::operator==(
798{
799 if (lhs.size() != rhs.size())
800 return (false);
801
802 return (std::equal(lhs.cbegin(), lhs.cend(), rhs.cbegin()));
803}
804
805template<typename T>
806bool
807BiometricEvaluation::Memory::operator!=(
810{
811 return (!(lhs == rhs));
812}
813
814template<typename T>
815bool
816BiometricEvaluation::Memory::operator<(
819{
820 return (std::lexicographical_compare(lhs.cbegin(), lhs.cend(),
821 rhs.cbegin(), rhs.cend()));
822}
823
824template<typename T>
825bool
826BiometricEvaluation::Memory::operator<=(
829{
830 return (!(rhs < lhs));
831}
832
833template<typename T>
834bool
835BiometricEvaluation::Memory::operator>(
838{
839 return (rhs < lhs);
840}
841
842template<typename T>
843bool
844BiometricEvaluation::Memory::operator>=(
847{
848 return (!(lhs < rhs));
849}
850
851#endif /* __BE_MEMORY_AUTOARRAY_H__ */
852
An error occurred when allocating an object.
Definition: be_error_exception.h:169
A StrategyError object is thrown when the underlying implementation of this interface encounters an e...
Definition: be_error_exception.h:270
A C-style array wrapped in the facade of a C++ STL container.
Definition: be_memory_autoarray.h:45
T & reference
Reference to element.
Definition: be_memory_autoarray.h:59
AutoArray & operator=(const AutoArray &other)
Copy assignment operator overload performing a deep copy.
Definition: be_memory_autoarray.h:622
iterator end()
Obtain an iterator to the end of the AutoArray.
Definition: be_memory_autoarray.h:691
iterator begin()
Obtain an iterator to the beginning of the AutoArray.
Definition: be_memory_autoarray.h:668
AutoArray(size_type size=0)
Construct an AutoArray.
Definition: be_memory_autoarray.h:730
const_iterator cend() const
Obtain an iterator to the end of the AutoArray.
Definition: be_memory_autoarray.h:713
~AutoArray()
Destructor.
Definition: be_memory_autoarray.h:783
void copy(const T *buffer)
Deep-copy the contents of a buffer into this AutoArray.
Definition: be_memory_autoarray.h:534
const_iterator cbegin() const
Obtain an iterator to the beginning of the AutoArray.
Definition: be_memory_autoarray.h:683
const T & const_reference
Const reference element.
Definition: be_memory_autoarray.h:61
size_type size() const
Obtain the number of accessible elements.
Definition: be_memory_autoarray.h:496
AutoArrayIterator< false, T > iterator
Iterator of element.
Definition: be_memory_autoarray.h:53
reference operator[](ptrdiff_t index)
Subscripting operator overload with unchecked access.
Definition: be_memory_autoarray.h:605
std::vector< T > to_vector() const
Obtain a copy of elements in this AutoArray as a vector.
Definition: be_memory_autoarray.h:578
size_t size_type
Type of subscripts, counts, etc.
Definition: be_memory_autoarray.h:50
T value_type
Type of element.
Definition: be_memory_autoarray.h:48
AutoArrayIterator< true, T > const_iterator
Const iterator of element.
Definition: be_memory_autoarray.h:56
reference at(ptrdiff_t index)
Subscript into the AutoArray with checked access.
Definition: be_memory_autoarray.h:565
void resize(size_type new_size, bool free=false)
Change the number of accessible elements.
Definition: be_memory_autoarray.h:504