#pragma once

namespace gui {

	/**
	 * Class that keeps track of a range of "dirty" elements. That is, elements that need to be
	 * uploaded to the GPU to keep the RAM and GPU representations in sync.
	 */
	class DirtyRange {
		STORM_VALUE;
	public:
		// Create.
		STORM_CTOR DirtyRange() {
			clear();
		}
		STORM_CTOR DirtyRange(Nat element) : begin(element), end(element + 1) {}
		STORM_CTOR DirtyRange(Nat b, Nat e) : begin(b), end(e) {}

		// First dirty element.
		Nat begin;

		// Element after last dirty element.
		Nat end;

		// Number of elements that are dirty.
		Nat STORM_FN count() const {
			return end - min(end, begin);
		}

		// Any elements dirty?
		Bool STORM_FN any() const {
			return begin < end;
		}
		Bool STORM_FN empty() const {
			return begin >= end;
		}

		// Mark an element as dirty.
		void STORM_FN mark(Nat id) {
			begin = min(begin, id);
			end = max(end, id + 1);
		}

		// Mark a range of elements as dirty.
		void STORM_FN mark(Nat begin, Nat end) {
			this->begin = min(this->begin, begin);
			this->end = max(this->end, end);
		}

		// Clear.
		void STORM_FN clear() {
			begin = 0xFFFFFFFF;
			end = 0;
		}
	};

}
