note
	description: "RRD values retrieved by a fetch operation"
	author: "Patrick Ruckstuhl <patrick@tario.org>"
	date: "$Date$"
	revision: "$Revision$"

class
	RRD_VALUES
inherit
	PLATFORM
		export
			{NONE} all
		end

	DISPOSABLE

create
	make

feature {NONE} -- Initialization

	make (a_start_time: NATURAL_64; an_end_time: NATURAL_64; a_step: NATURAL_64; a_ds_count: NATURAL_64; a_ds_names: POINTER; a_values: POINTER)
			-- Create.
		require
			start_before_end: a_start_time <= an_end_time
		do
			start_time := a_start_time
			end_time := an_end_time
			step := a_step
			ds_count := a_ds_count
			create ds_names.share_from_pointer (a_ds_names, ds_count.as_integer_32)
			create values.share_from_pointer (a_values, array_position_by_time_and_datasource (end_time, ds_count - 1))
		ensure
			start_time_set: start_time = a_start_time
			end_time_set: end_time = an_end_time
			step_set: step = a_step
			ds_count_set: ds_count = a_ds_count
			ds_names_created: ds_names /= Void
			values_created: values /= Void
		end

feature -- Access

	start_time: NATURAL_64
		-- Start time.

	end_time: NATURAL_64
		-- End time.

	step: NATURAL_64
		-- Step.

	ds_count: NATURAL_64
		-- Datasource count.

feature -- Queries

	item_at_time (a_time: NATURAL_64; a_datasource: NATURAL_64): REAL_64
			-- Retrieve the value at a certain time of a certain datasource.
		require
			time_valid: a_time >= start_time and a_time < end_time
			datasource_valid: a_datasource < ds_count
		do
			Result := values.read_real_64 (array_position_by_time_and_datasource (a_time, a_datasource))
		end

	datasource_name (a_datasource: NATURAL_64): STRING
			-- Retrieve the name of a datasource.
		require
			datasource_valid: a_datasource < ds_count
		local
			l_c_string: C_STRING
		do
			create l_c_string.make_shared_from_pointer (ds_names.read_pointer (a_datasource.as_integer_32))
			Result := l_c_string.string
		ensure
			Result_ok: Result /= Void
		end

feature {NONE} -- Implementation

	ds_names: MANAGED_POINTER
		-- Datesource names.

	values: MANAGED_POINTER
		-- Values.

	array_position_by_time_and_datasource (a_time: NATURAL_64; a_datasource: NATURAL_64): INTEGER
			-- Compute the array position by the time and datasource number.
		require
			time_valid: a_time >= start_time and a_time <= end_time
			datasource_valid: a_datasource < ds_count
		do
			Result := ((a_time - start_time) // step * ds_count).as_integer_32 * real_64_bytes
		ensure
			Result_valid: result >= 0
		end

	dispose
			-- Free the memory of the names and values.
		local
			i: NATURAL_64
		do
				-- free the memory used by the name strings
			from
				i := 0
			until
				i >= ds_count
			loop
				ds_names.read_pointer (i.as_integer_32).memory_free
				i := i + 1
			end

				-- free the arrays
			ds_names.item.memory_free
			values.item.memory_free
		end
end