Compact3DLookup.h
#ifndef __COMPACT_3D_LOOKUP_H__
#define __COMPACT_3D_LOOKUP_H__
/*LICENSE_START*/
/*
* Copyright (c) 2014, Washington University School of Medicine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CompactLookup.h"
namespace cifti
{
template <typename T>
class Compact3DLookup
{
CompactLookup<CompactLookup<CompactLookup<T> > > m_lookup;//the whole point of this class is to deal with this ugliness
public:
T& at(const int64_t& index1, const int64_t& index2, const int64_t& index3);
T& at(const int64_t index[3]) { return at(index[0], index[1], index[2]); }
void insert(const int64_t& index1, const int64_t& index2, const int64_t& index3, const T& value)
{ at(index1, index2, index3) = value; }
void insert(const int64_t index[3], const T& value)
{ at(index) = value; }
T* find(const int64_t& index1, const int64_t& index2, const int64_t& index3);
T* find(const int64_t index[3]) { return find(index[0], index[1], index[2]); }
const T* find(const int64_t& index1, const int64_t& index2, const int64_t& index3) const;
const T* find(const int64_t index[3]) const { return find(index[0], index[1], index[2]); }
void clear();
};
template<typename T>
T& Compact3DLookup<T>::at(const int64_t& index1, const int64_t& index2, const int64_t& index3)
{
return m_lookup[index3][index2][index1];//a lot of complexity is hidden in those operator[]s
}
template<typename T>
T* Compact3DLookup<T>::find(const int64_t& index1, const int64_t& index2, const int64_t& index3)
{
typename CompactLookup<CompactLookup<CompactLookup<T> > >::iterator iter1 = m_lookup.find(index3);//oh the humanity
if (iter1 == m_lookup.end()) return NULL;
typename CompactLookup<CompactLookup<T> >::iterator iter2 = iter1->find(index2);
if (iter2 == iter1->end()) return NULL;
typename CompactLookup<T>::iterator iter3 = iter2->find(index1);
if (iter3 == iter2->end()) return NULL;
return &(*iter3);
}
template <typename T>
const T* Compact3DLookup<T>::find(const int64_t& index1, const int64_t& index2, const int64_t& index3) const
{
typename CompactLookup<CompactLookup<CompactLookup<T> > >::const_iterator iter1 = m_lookup.find(index3);
if (iter1 == m_lookup.end()) return NULL;
typename CompactLookup<CompactLookup<T> >::const_iterator iter2 = iter1->find(index2);
if (iter2 == iter1->end()) return NULL;
typename CompactLookup<T>::const_iterator iter3 = iter2->find(index1);
if (iter3 == iter2->end()) return NULL;
return &(*iter3);
}
template <typename T>
void Compact3DLookup<T>::clear()
{
m_lookup.clear();
}
}
#endif //__COMPACT_3D_LOOKUP_H__
cifti::Compact3DLookup
Definition Compact3DLookup.h:38
cifti::Compact3DLookup::clear
void clear()
empties the lookup
Definition Compact3DLookup.h:94
cifti::Compact3DLookup::at
T & at(const int64_t &index1, const int64_t &index2, const int64_t &index3)
creates the element if it didn't exist, and returns a reference to it
Definition Compact3DLookup.h:64
cifti::Compact3DLookup::at
T & at(const int64_t index[3])
creates the element if it didn't exist, and returns a reference to it
Definition Compact3DLookup.h:44
cifti::Compact3DLookup::insert
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
cifti::Compact3DLookup::find
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
cifti::Compact3DLookup::find
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
cifti::Compact3DLookup::find
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
cifti::Compact3DLookup::insert
void insert(const int64_t index[3], const T &value)
add or overwrite an element in the lookup
Definition Compact3DLookup.h:49
cifti::CompactLookup::const_iterator
Definition CompactLookup.h:77
cifti::CompactLookup::iterator
Definition CompactLookup.h:49
cifti::CompactLookup
Definition CompactLookup.h:40
cifti::CompactLookup::find
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
cifti
namespace for all CiftiLib functionality
Definition CiftiBrainModelsMap.h:42