#pragma once

#include "utils/vector.h"

namespace mesh {

	template <typename T>
	struct Mesh1D{
		uint64_t center_idx;				// max cell index
		uint64_t vertices_idx;				// max vertice index
		utils::Vector<T> centers;			// size N (unknowns at cell centers)
		utils::Vector<T> vertices;			// size N+1 (face positions)

		Mesh1D() = default;

		explicit Mesh1D(const utils::Vector<T>& midpoints){
			centers = midpoints;
			center_idx = centers.size()-1;
			vertices_idx = centers.size();		
		}

		void generate_vertices(T left, T right){
			vertices.resize(vertices_idx+1);
			vertices[0] = left;	
			vertices[vertices_idx] = right;
			for (uint64_t i = 1; i < center_idx+1; ++i){
				vertices[i] = (centers[i-1] + centers[i])*0.5;
			}

		}

		T dx(uint64_t i) const { check(i); return vertices[i+1] - vertices[i]; }
		T center(uint64_t i) const { check(i); return centers[i]; }
		T vertice(uint64_t i) const {; return vertices[i]; }

	    void check(uint64_t i) const {
	        if (i > center_idx) throw std::runtime_error("Mesh1D: cell index out of range");
	    }
	};


} // end namespace mesh