<commit_before>#ifndef COMMON_CELLSPACEPARTITION_H
#define COMMON_CELLSPACEPARTITION_H

#include <list>
#include <vector>

#include "Math.h"

namespace Common {

template<class T>
struct Cell {
	std::list<T> members;
	AABB box;
	Cell(const AABB& b) : box(b) { }
};

template<class T>
class CellSpacePartition {
	public:
		inline CellSpacePartition(float w, float h,
				unsigned int cellsx, unsigned int cellsy, unsigned int maxentities);
		inline void add(T& t, const Point& p);
		inline void remove(T& t, const Point& p);
		inline void update(T& t, const Point& oldpos, const Point& newpos);
		inline T& queryBegin(const Point& p, float radius);
		inline bool queryEnd() const;
		inline T& queryNext();

	private:
		inline unsigned int positionToIndex(const Point& p) const;
		std::vector<Cell<T>> mCells;
		float mWidth;
		float mHeight;

		unsigned int mNumCellsX;
		unsigned int mNumCellsY;

		unsigned int mMaxEntities;

		float mCellWidth;
		float mCellHeight;

		typename std::vector<T> mQueryResult;
		unsigned int mQueryIndex;
		unsigned int mQueryResultNum;
};

template<class T>
CellSpacePartition<T>::CellSpacePartition(float w, float h, unsigned int cellsx, unsigned int cellsy,
		unsigned int maxentities)
	: mWidth(w),
	mHeight(h),
	mNumCellsX(cellsx),
	mNumCellsY(cellsy),
	mMaxEntities(maxentities),
	mQueryResultNum(0)
{
	mCellWidth  = mWidth  / mNumCellsX;
	mCellHeight = mHeight / mNumCellsY;
	for(int j = 0; j < mNumCellsY; j++) {
		for(int i = 0; i < mNumCellsX; i++) {
			mCells.push_back(Cell<T>(AABB(Point((i + 0.5f) * mCellWidth - mWidth * 0.5f,
							(j + 0.5f) * mCellHeight - mHeight * 0.5f),
						Point(mCellWidth * 0.5f,
							mCellHeight * 0.5f))));
		}
	}

	std::cout << "Total " << mCells.size() << " cells.\n";

	mQueryResult.resize(mMaxEntities);
}

template<class T><commit_msg>fix incorrect memory allocation in cell space partitioning<commit_after>#ifndef COMMON_CELLSPACEPARTITION_H
#define COMMON_CELLSPACEPARTITION_H

#include <list>
#include <vector>
#include <cmath>

#include "Math.h"

namespace Common {

template<class T>
struct Cell {
	std::list<T> members;
	AABB box;
	Cell(const AABB& b) : box(b) { }
};

template<class T>
class CellSpacePartition {
	public:
		inline CellSpacePartition(float w, float h,
				unsigned int cellsx, unsigned int cellsy, unsigned int maxentities);
		inline void add(T& t, const Point& p);
		inline void remove(T& t, const Point& p);
		inline void update(T& t, const Point& oldpos, const Point& newpos);
		inline T& queryBegin(const Point& p, float radius);
		inline bool queryEnd() const;
		inline T& queryNext();

	private:
		inline unsigned int positionToIndex(const Point& p) const;
		std::vector<Cell<T>> mCells;
		float mWidth;
		float mHeight;

		unsigned int mNumCellsX;
		unsigned int mNumCellsY;

		unsigned int mMaxEntities;

		float mCellWidth;
		float mCellHeight;

		typename std::vector<T> mQueryResult;
		unsigned int mQueryIndex;
		unsigned int mQueryResultNum;
};

template<class T>
CellSpacePartition<T>::CellSpacePartition(float w, float h, unsigned int cellsx, unsigned int cellsy,
		unsigned int maxentities)
	: mWidth(w),
	mHeight(h),
	mNumCellsX(cellsx),
	mNumCellsY(cellsy),
	mMaxEntities(maxentities),
	mQueryResultNum(0)
{
	mCellWidth  = ceil(mWidth  / (float)mNumCellsX);
	mCellHeight = ceil(mHeight / (float)mNumCellsY);
	for(int j = 0; j < mNumCellsY; j++) {
		for(int i = 0; i < mNumCellsX; i++) {
			mCells.push_back(Cell<T>(AABB(Point((i + 0.5f) * mCellWidth - mWidth * 0.5f,
							(j + 0.5f) * mCellHeight - mHeight * 0.5f),
						Point(mCellWidth * 0.5f,
							mCellHeight * 0.5f))));
		}
	}

	std::cout << "Total " << mCells.size() << " cells.\n";

	mQueryResult.resize(mMaxEntities);
}

template<class T>
{'fraction_non_alphanumeric': 0.12426194310252281, 'fraction_numerical': 0.008051529790660225, 'mean_word_length': 4.497050147492625, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6396000', 'n_tokens_mistral': 1481, 'n_tokens_neox': 1347, 'n_words': 362}