<div dir="ltr">Hi Alpár<br><br>Thank you for your proposition. <br><br>I might be a good think to be able to build an array of NodeMap. For instance, in graph colouring, one want to build a number of colourings (NodeMap<int>) and compare them using a certain criteria. <br>
<br>Unfortunately, I couldn't understand very much the implementation of Array. <br><br>May be one break for people who want to contribute to Lemon is that they are not used to this kind of programming (generic programming, etc). Are there interesting tutorials/sites for those who want to enhance their C++ programming skill so that they can contribute ? <br>
<br>Regards<br><br>--<br><br>Mouaouia Cherif BOUZID<br><br><br><div class="gmail_quote">2012/7/26 Alpár Jüttner <span dir="ltr"><<a href="mailto:alpar@cs.elte.hu" target="_blank">alpar@cs.elte.hu</a>></span><br><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Hi,<br>
<br>
It is a common problem that one would like use an array of objects, but<br>
(s)he can't because the object either (a) not default constructible or<br>
(b) not copyable. Notably, it is a regular question from LEMON users how<br>
to create an array of NodeMap<>.<br>
<br>
If the object is at least default constructible, than you can use<br>
new[]/delete[], otherwise (such as for NodeMap) the only reasonable<br>
solution is using std::vector<Object *> and manually and individually<br>
allocating/deleting the objects.<br>
<br>
Note, that even std::array introduced by C++11 fails to solve this<br>
problem, for (1) its size is determined compile-time, and (2) does not<br>
resolve (a).<br>
<br>
Instead, I propose another Array implementation, please find the<br>
prototype at the end of this mail.<br>
<br>
This is a fixed-size array, but the size is determined run time, i.e. at<br>
construction. Moreover, the objects in the array are constructed using<br>
the parameters passed to Array itself. Thus you can do this:<br>
<br>
ListDigraph g;<br>
ListDigraph::Node n;<br>
...<br>
int size = 12;<br>
Array<ListDigraph::NodeMap<int> > map_array(size, g);<br>
map_array[2][n]=5;<br>
map_array[3][n]+=11;<br>
<br>
or even:<br>
<br>
Array<ListDigraph::NodeMap<bool> > map_array(size, g, true);<br>
map_array[2][n]=false;<br>
<br>
What do you think of this idea? Does it worth including into LEMON?<br>
<br>
Plus a design question - do we need the _size member? Omitting it would<br>
be result in a narrower layer above the simple pointer it actually<br>
wraps. On the other, it allows a more stl-like interface with size(),<br>
iterators, begin(), end() etc. But to be honest, I see very little use<br>
of them here, except for size().<br>
<br>
Regards,<br>
Alpar<br>
<br>
<br>
// *****************************************************<br>
// * Array class *<br>
// *****************************************************<br>
<br>
template<class T ><br>
class Array {<br>
T * _array;<br>
size_t _size;<br>
<br>
///Copying is not allowed.<br>
Array(const Array &);<br>
///Copying is not allowed.<br>
Array &operator=(const Array &);<br>
<br>
public:<br>
explicit Array(size_t size) : _array(new T[size]), _size(size) {}<br>
template <class T1><br>
explicit Array(size_t size, T1 t1)<br>
: _array( static_cast<T*>(operator new[] (size*sizeof(T)))),<br>
_size(size)<br>
{<br>
for(size_t i=0;i<size;i++)<br>
new (_array+i) T(t1);<br>
}<br>
template <class T1, class T2><br>
explicit Array(size_t size, T1 t1, T2 t2)<br>
: _array( static_cast<T*>(operator new[] (size*sizeof(T)))),<br>
_size(size)<br>
{<br>
for(size_t i=0;i<size;i++)<br>
new (_array+i) T(t1,t2);<br>
}<br>
template <class T1, class T2, class T3><br>
explicit Array(size_t size, T1 t1, T2 t2, T3 t3)<br>
: _array( static_cast<T*>(operator new[] (size*sizeof(T)))),<br>
_size(size)<br>
{<br>
for(size_t i=0;i<size;i++)<br>
new (_array+i) T(t1,t2,t3);<br>
}<br>
template <class T1, class T2, class T3, class T4><br>
explicit Array(size_t size, T1 t1, T2 t2, T3 t3, T4 t4)<br>
: _array( static_cast<T*>(operator new[] (size*sizeof(T)))),<br>
_size(size)<br>
{<br>
for(size_t i=0;i<size;i++)<br>
new (_array+i) T(t1,t2,t3,t4);<br>
}<br>
template <class T1, class T2, class T3, class T4, class T5><br>
explicit Array(size_t size, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)<br>
: _array( static_cast<T*>(operator new[] (size*sizeof(T)))),<br>
_size(size)<br>
{<br>
for(size_t i=0;i<size;i++)<br>
new (_array+i) T(t1,t2,t3,t4,t5);<br>
}<br>
<br>
~Array() { delete [] _array; }<br>
operator T* () { return _array; }<br>
size_t size() { return _size; }<br>
};<br>
}<br>
<br>
<br>
_______________________________________________<br>
Lemon-devel mailing list<br>
<a href="mailto:Lemon-devel@lemon.cs.elte.hu">Lemon-devel@lemon.cs.elte.hu</a><br>
<a href="http://lemon.cs.elte.hu/mailman/listinfo/lemon-devel" target="_blank">http://lemon.cs.elte.hu/mailman/listinfo/lemon-devel</a><br>
<br>
<br>
<br>
_______________________________________________<br>
Lemon-user mailing list<br>
<a href="mailto:Lemon-user@lemon.cs.elte.hu">Lemon-user@lemon.cs.elte.hu</a><br>
<a href="http://lemon.cs.elte.hu/mailman/listinfo/lemon-user" target="_blank">http://lemon.cs.elte.hu/mailman/listinfo/lemon-user</a><br>
</blockquote></div><br><br clear="all"><br>
</div>