<commit_before>/*
    Copyright 2011 Brain Research Institute, Melbourne, Australia

    Written by <NAME>, 2012.

    This file is part of MRtrix.

    MRtrix is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MRtrix is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MRtrix.  If not, see <http://www.gnu.org/licenses/>.

*/



#ifndef __dwi_fixel_map_h__
#define __dwi_fixel_map_h__


#include "image/buffer_scratch.h"
#include "image/loop.h"
#include "image/nav.h"
#include "image/voxel.h"

#include "dwi/fmls.h"
#include "dwi/directions/mask.h"


namespace MR
{
  namespace DWI
  {



    template <class Fixel>
    class Fixel_map
    {

      public:
        template <typename Set>
        Fixel_map (const Set& i) :
        info_ (i),
        data  (info_, "fixel map voxels"),
        accessor (data)
        {
          Image::Voxel< Image::BufferScratch<MapVoxel*> > v (data);
          Image::Loop loop;
          for (loop.start (v); loop.ok(); loop.next (v))
            v.value() = NULL;
          // fixels[0] is an invaid fixel, as provided by the relevant empty constructor
          // This allows index 0 to be used as an error code, simplifying the implementation of MapVoxel and Iterator
          fixels.push_back (Fixel());
        }

        class MapVoxel;
        typedef Image::Voxel< Image::BufferScratch<MapVoxel*> > VoxelAccessor;

        virtual ~Fixel_map()
        {
          Image::Loop loop;
          VoxelAccessor v (accessor);
          for (loop.start (v); loop.ok(); loop.next (v)) {
            if (v.value()) {
              delete v.value();
              v.value() = NULL;
            }
          }
        }


        class Iterator;
        class ConstIterator;

        Iterator      begin (VoxelAccessor& v)       { return Iterator      (v.value(), *this); }
        ConstIterator begin (VoxelAccessor& v) const { return ConstIterator (v.value(), *this); }

        Fixel&       operator[] (const size_t i)       { return fixels[i]; }
        const Fixel& operator[] (const size_t i) const { return fixels[i]; }

        virtual bool operator() (const FMLS::FOD_lobes& in);

        const Image::Info& info() const { return info_; }


      protected:

        const class Info : public Image::Info {
          public:
          template <typename T>
          Info (const T& i) :
          Image::Info (i.info())
          {
            set_ndim (3);
          }
          Info () : Image::Info () { }
        } info_;

        Image::BufferScratch<MapVoxel*> data;
        const VoxelAccessor accessor; // Functions can copy-construct their own voxel accessor from this and retain const-ness
        std::vector<Fixel> fixels;


        Fixel_map (const Fixel_map& that) : info_ (that.data), data (info_) { assert (0); }


    };




    template <class Fixel>
    class Fixel_map<Fixel>::MapVoxel
    {
      public:
        MapVoxel (const FMLS::FOD_lobes& in, const size_t first) :
          first_fixel_index (first),
          count (in.size()),
          lookup_table (new uint8_t[in.lut.size()])
        {
          memcpy (lookup_table, &in.lut[0], in.lut.size() * sizeof (uint8_t));
        }

        MapVoxel (const size_t first, const size_t size) :
          first_fixel_index (first),
          count (size),
          lookup_table (NULL) { }

        ~MapVoxel()
        {
          if (lookup_table) {
            delete lookup_table;
            lookup_table = NULL;
          }
        }

        size_t first_index() const { return first_fixel_index; }
        size_t num_fixels()  const { return count; }
        bool   empty()       const { return !count; }

        // Direction must have been assigned to a histogram bin first
        size_t dir2fixel (const size_t dir) const
        {
          assert (lookup_table);
          const size_t offset = lookup_table[dir];
          return ((offset == count) ? 0 : (first_fixel_index + offset));
        }


      private:
        size_t first_fixel_index, count;
        uint8_t* lookup_table;

    };



    template <class Fixel>
    class Fixel_map<Fixel>::Iterator<commit_msg>Fix mismatched delete in DWI::Fixel_map::MapVoxel class<commit_after>/*
    Copyright 2011 Brain Research Institute, Melbourne, Australia

    Written by <NAME>, 2012.

    This file is part of MRtrix.

    MRtrix is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MRtrix is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MRtrix.  If not, see <http://www.gnu.org/licenses/>.

*/



#ifndef __dwi_fixel_map_h__
#define __dwi_fixel_map_h__


#include "image/buffer_scratch.h"
#include "image/loop.h"
#include "image/nav.h"
#include "image/voxel.h"

#include "dwi/fmls.h"
#include "dwi/directions/mask.h"


namespace MR
{
  namespace DWI
  {



    template <class Fixel>
    class Fixel_map
    {

      public:
        template <typename Set>
        Fixel_map (const Set& i) :
        info_ (i),
        data  (info_, "fixel map voxels"),
        accessor (data)
        {
          Image::Voxel< Image::BufferScratch<MapVoxel*> > v (data);
          Image::Loop loop;
          for (loop.start (v); loop.ok(); loop.next (v))
            v.value() = NULL;
          // fixels[0] is an invaid fixel, as provided by the relevant empty constructor
          // This allows index 0 to be used as an error code, simplifying the implementation of MapVoxel and Iterator
          fixels.push_back (Fixel());
        }

        class MapVoxel;
        typedef Image::Voxel< Image::BufferScratch<MapVoxel*> > VoxelAccessor;

        virtual ~Fixel_map()
        {
          Image::Loop loop;
          VoxelAccessor v (accessor);
          for (loop.start (v); loop.ok(); loop.next (v)) {
            if (v.value()) {
              delete v.value();
              v.value() = NULL;
            }
          }
        }


        class Iterator;
        class ConstIterator;

        Iterator      begin (VoxelAccessor& v)       { return Iterator      (v.value(), *this); }
        ConstIterator begin (VoxelAccessor& v) const { return ConstIterator (v.value(), *this); }

        Fixel&       operator[] (const size_t i)       { return fixels[i]; }
        const Fixel& operator[] (const size_t i) const { return fixels[i]; }

        virtual bool operator() (const FMLS::FOD_lobes& in);

        const Image::Info& info() const { return info_; }


      protected:

        const class Info : public Image::Info {
          public:
          template <typename T>
          Info (const T& i) :
          Image::Info (i.info())
          {
            set_ndim (3);
          }
          Info () : Image::Info () { }
        } info_;

        Image::BufferScratch<MapVoxel*> data;
        const VoxelAccessor accessor; // Functions can copy-construct their own voxel accessor from this and retain const-ness
        std::vector<Fixel> fixels;


        Fixel_map (const Fixel_map& that) : info_ (that.data), data (info_) { assert (0); }


    };




    template <class Fixel>
    class Fixel_map<Fixel>::MapVoxel
    {
      public:
        MapVoxel (const FMLS::FOD_lobes& in, const size_t first) :
          first_fixel_index (first),
          count (in.size()),
          lookup_table (new uint8_t[in.lut.size()])
        {
          memcpy (lookup_table, &in.lut[0], in.lut.size() * sizeof (uint8_t));
        }

        MapVoxel (const size_t first, const size_t size) :
          first_fixel_index (first),
          count (size),
          lookup_table (NULL) { }

        ~MapVoxel()
        {
          if (lookup_table) {
            delete[] lookup_table;
            lookup_table = NULL;
          }
        }

        size_t first_index() const { return first_fixel_index; }
        size_t num_fixels()  const { return count; }
        bool   empty()       const { return !count; }

        // Direction must have been assigned to a histogram bin first
        size_t dir2fixel (const size_t dir) const
        {
          assert (lookup_table);
          const size_t offset = lookup_table[dir];
          return ((offset == count) ? 0 : (first_fixel_index + offset));
        }


      private:
        size_t first_fixel_index, count;
        uint8_t* lookup_table;

    };



    template <class Fixel>
    class Fixel_map<Fixel>::Iterator
{'fraction_non_alphanumeric': 0.1002615518744551, 'fraction_numerical': 0.004141238012205754, 'mean_word_length': 2.0692307692307694, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5731013', 'n_tokens_mistral': 2847, 'n_tokens_neox': 2716, 'n_words': 856}