CiftiLib
A C++ library for CIFTI-2 and CIFTI-1 files
Compact3DLookup.h
1 #ifndef __COMPACT_3D_LOOKUP_H__
2 #define __COMPACT_3D_LOOKUP_H__
3 
4 /*LICENSE_START*/
5 /*
6  * Copyright (c) 2014, Washington University School of Medicine
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modification,
10  * are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "CompactLookup.h"
32 
33 namespace cifti
34 {
35 
36  template <typename T>
38  {
39  CompactLookup<CompactLookup<CompactLookup<T> > > m_lookup;//the whole point of this class is to deal with this ugliness
40  public:
42  T& at(const int64_t& index1, const int64_t& index2, const int64_t& index3);
44  T& at(const int64_t index[3]) { return at(index[0], index[1], index[2]); }
46  void insert(const int64_t& index1, const int64_t& index2, const int64_t& index3, const T& value)
47  { at(index1, index2, index3) = value; }
49  void insert(const int64_t index[3], const T& value)
50  { at(index) = value; }
52  T* find(const int64_t& index1, const int64_t& index2, const int64_t& index3);
54  T* find(const int64_t index[3]) { return find(index[0], index[1], index[2]); }
56  const T* find(const int64_t& index1, const int64_t& index2, const int64_t& index3) const;
58  const T* find(const int64_t index[3]) const { return find(index[0], index[1], index[2]); }
60  void clear();
61  };
62 
63  template<typename T>
64  T& Compact3DLookup<T>::at(const int64_t& index1, const int64_t& index2, const int64_t& index3)
65  {
66  return m_lookup[index3][index2][index1];//a lot of complexity is hidden in those operator[]s
67  }
68 
69  template<typename T>
70  T* Compact3DLookup<T>::find(const int64_t& index1, const int64_t& index2, const int64_t& index3)
71  {
72  typename CompactLookup<CompactLookup<CompactLookup<T> > >::iterator iter1 = m_lookup.find(index3);//oh the humanity
73  if (iter1 == m_lookup.end()) return NULL;
74  typename CompactLookup<CompactLookup<T> >::iterator iter2 = iter1->find(index2);
75  if (iter2 == iter1->end()) return NULL;
76  typename CompactLookup<T>::iterator iter3 = iter2->find(index1);
77  if (iter3 == iter2->end()) return NULL;
78  return &(*iter3);
79  }
80 
81  template <typename T>
82  const T* Compact3DLookup<T>::find(const int64_t& index1, const int64_t& index2, const int64_t& index3) const
83  {
84  typename CompactLookup<CompactLookup<CompactLookup<T> > >::const_iterator iter1 = m_lookup.find(index3);
85  if (iter1 == m_lookup.end()) return NULL;
86  typename CompactLookup<CompactLookup<T> >::const_iterator iter2 = iter1->find(index2);
87  if (iter2 == iter1->end()) return NULL;
88  typename CompactLookup<T>::const_iterator iter3 = iter2->find(index1);
89  if (iter3 == iter2->end()) return NULL;
90  return &(*iter3);
91  }
92 
93  template <typename T>
95  {
96  m_lookup.clear();
97  }
98 
99 }
100 
101 #endif //__COMPACT_3D_LOOKUP_H__
T * find(const int64_t index[3])
returns a pointer to the desired element, or NULL if no such element is found
Definition: Compact3DLookup.h:54
namespace for all CiftiLib functionality
Definition: CiftiBrainModelsMap.h:41
void clear()
empties the lookup
Definition: Compact3DLookup.h:94
iterator find(const int64_t &index)
returns an iterator pointing to the desired element, or one equal to end() if no such element is foun...
Definition: CompactLookup.h:169
void insert(const int64_t &index1, const int64_t &index2, const int64_t &index3, const T &value)
add or overwrite an element in the lookup
Definition: Compact3DLookup.h:46
T & at(const int64_t &index1, const int64_t &index2, const int64_t &index3)
creates the element if it didn&#39;t exist, and returns a reference to it
Definition: Compact3DLookup.h:64
T & at(const int64_t index[3])
creates the element if it didn&#39;t exist, and returns a reference to it
Definition: Compact3DLookup.h:44
const T * find(const int64_t index[3]) const
returns a pointer to the desired element, or NULL if no such element is found
Definition: Compact3DLookup.h:58
T * find(const int64_t &index1, const int64_t &index2, const int64_t &index3)
returns a pointer to the desired element, or NULL if no such element is found
Definition: Compact3DLookup.h:70
Definition: CompactLookup.h:39
Definition: CompactLookup.h:76
void insert(const int64_t index[3], const T &value)
add or overwrite an element in the lookup
Definition: Compact3DLookup.h:49
Definition: CompactLookup.h:48
Definition: Compact3DLookup.h:37