Compile-Time std::map

Lutger lutger.blijdestijn at gmail.com
Sat Apr 28 07:34:27 PDT 2007


NN wrote:
> Ok, from the beginning :)
> While writing I think all problems are solved, but I'm not sure.
> 
> I want to do binary search on any array:
> 
> int[] sorted_array = {1,2,3};
> binary_search(sorted_array, 1); // complexity O(log N)
> 
> But i can do it only if array is sorted.
> I want to write any array and sort it in compile time:
> 
> int[] sorted_array = compile_time_sort({3,2,1});
> 
> Is it possible ?

Yes since the latest dmd release this is not hard to do, but only when 
using array literals of course. This crappy sort method works in compile 
time, pretty amazing how easy it is to do such things!

T[] selectionSort(T)(T[] array)
{
	T temp;
	for(int i = 0; i < array.length; ++i)
	{
		int min = i;
		for(int j = i + 1; j < array.length; ++j)
			if (array[j] < array[min])
				min = j;
		temp = array[i];
		array[i] = array[min];
		array[min] = temp;
	}
	return array;
}



More information about the Digitalmars-d mailing list