Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualJosh Klint

Posted 04 May 2013 - 01:40 PM

Here's what I came up with. The hardest situation is breaking from the loop without doing any kind of cleanup on the iterator. I tried doing some smart pointer stuff, but I generally stay away from stuff like that:
List list;
        Link* link[3];
        ListIterator it;
        int i;
 
        //============================================
        // Test: Remove first element
        //============================================
 
        i=0;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                System::Print(it.link->value);
                if (link[i])
                {
                        link[i]->Release();
                        link[i]=NULL;
                }
        }
 
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
 
        //============================================
        // Test: Remove second element
        //============================================
 
        i=1;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                System::Print(it.link->value);
                if (link[i])
                {
                        link[i]->Release();
                        link[i]=NULL;
                }
        }
 
        it = ListIterator();
 
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
       
        //============================================
        // Test: Remove third element
        //============================================
 
        i=2;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                System::Print(it.link->value);
                if (link[i])
                {
                        link[i]->Release();
                        link[i]=NULL;
                }
        }
       
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
 
        //============================================
        // Test: Break during loop
        //============================================
 
        i=2;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                break;
        }
        it = ListIterator();
       
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
However, there are a lot of problems you can cause unless that code above isn't followed strictly. rip-off's suggestion was probably the best. Still, this strikes me as a really fundamental problem that ought to have a good solution.

#2Josh Klint

Posted 04 May 2013 - 01:40 PM

Here's what I came up with. The hardest situation is breaking from the loop without doing any kind of cleanup on the iterator. I tried doing some smart pointer stuff, but I generally stay away from stuff like that:
List list;
        Link* link[3];
        ListIterator it;
        int i;
 
        //============================================
        // Test: Remove first element
        //============================================
 
        i=0;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                System::Print(it.link->value);
                if (link[i])
                {
                        link[i]->Release();
                        link[i]=NULL;
                }
        }
 
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
 
        //============================================
        // Test: Remove second element
        //============================================
 
        i=1;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                System::Print(it.link->value);
                if (link[i])
                {
                        link[i]->Release();
                        link[i]=NULL;
                }
        }
 
        it = ListIterator();
 
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
       
        //============================================
        // Test: Remove third element
        //============================================
 
        i=2;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                System::Print(it.link->value);
                if (link[i])
                {
                        link[i]->Release();
                        link[i]=NULL;
                }
        }
       
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
 
        //============================================
        // Test: Break during loop
        //============================================
 
        i=2;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                break;
        }
        it = ListIterator();
       
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
However, there are a lot of problems you can cause by just calling some unnecessary commands. rip-off's suggestion was probably the best. Still, this strikes me as a really fundamental problem that ought to have a good solution.

#1Josh Klint

Posted 04 May 2013 - 11:28 AM

Here's what I came up with. The hardest situation is breaking from the loop without doing any kind of cleanup on the iterator. I tried doing some smart pointer stuff, but I generally stay away from stuff like that:
List list;
        Link* link[3];
        ListIterator it;
        int i;
 
        //============================================
        // Test: Remove first element
        //============================================
 
        i=0;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                System::Print(it.link->value);
                if (link[i])
                {
                        link[i]->Release();
                        link[i]=NULL;
                }
        }
 
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
 
        //============================================
        // Test: Remove second element
        //============================================
 
        i=1;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                System::Print(it.link->value);
                if (link[i])
                {
                        link[i]->Release();
                        link[i]=NULL;
                }
        }
 
        it = ListIterator();
 
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
       
        //============================================
        // Test: Remove third element
        //============================================
 
        i=2;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                System::Print(it.link->value);
                if (link[i])
                {
                        link[i]->Release();
                        link[i]=NULL;
                }
        }
       
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();
 
        //============================================
        // Test: Break during loop
        //============================================
 
        i=2;
 
        link[0] = list.AddLast(1);
        link[1] = list.AddLast(2);
        link[2] = list.AddLast(3);
 
        it = list.Begin();
 
        for (it = list.Begin(); it != list.End(); it++)
        {
                break;
        }
        it = ListIterator();
       
        //Check
        for (i=0; i<3; i++)
        {
                if (link[i]) Debug::Assert(link[i]->refcount==1);
        }
 
        list.Clear();

PARTNERS