Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More efficient looping? [closed]

So I'm writing a simple struct to act like an Array of strings but with some handy operators and other functions that I've always wanted to see in strings. Specifically the method I'm working on right now is the / operator. The problem is, it won't add on any remainders at the end like I want it to.

What it's supposed to do, is take an array of strings, like {"Hello", "Test1", "Test2", "Goodbye", "More?", "Qwerty"} and, say I want to divide by 4, it should return { {"Hello", "Test1", "Test2", "Goodbye"}, {"More?", "Qwerty"} } but it doesn't.

The whole class (the method I want to improve is the / operator, but if you see anything else I can work on please point it out) (I know barely any of it is commented. Sorry about that, didn't expect anyone else to see this code aside from me.):

public struct StringCollection
{
    private String[] value;

    public StringCollection(params String[] s)
    {
        this.value = s;
    }

    public StringCollection(StringCollection current, String ad)
    {
        if (current.value == null) {
            current.value = new String[0] { };
        }
        this.value = new String[current.value.Length+1];
            for (int i=0; i<this.value.Length; i++)
            {
                try {
                    this.value[i] = current[i];
                } catch {
                    break;
                }
            }
            this.value[this.value.Length-1] = ad;
    }
    public StringCollection(StringCollection x, params StringCollection[] y)
    {
        this.value = x.value;
        for (int j=0;j<y.Length;j++)
        {
            for (int i=0;i<y[j].value.Length;i++)
            {
                this += y[j][i];
            }
        }
    }

    public static StringCollection[] operator /(StringCollection x, int y)
    {
        StringCollection[] result = null;
        if (((int)x.value.Length/y) == ((double)x.value.Length)/y)
            result = new StringCollection[y];
        else
            result = new StringCollection[y+1];
        for (int j=0;j<y;j++)
        {
            for (int i=0;i<((int)x.value.Length/y);i++)
            {
                result[j] += x.value[i+(int)((x.value.Length/y)*j)];
            }
        }
        if (((int)x.value.Length/y) != ((double)x.value.Length)/y)
        {
                            // This is the part that isn't working.
            for (int i=0;i<(((int)x.value.Length/y)*result[0].value.Length)-x.value.Length;i++) 
            {
                result[result.Length-1] += x.value[i+((result[0].value.Length)*result.Length-2)];
            }
        }
        return result;
    }
    public String this[int index]
    {
        get {
            return this.value[index];
        }
        set {
            this.value[index] = value;
        }
    }

}

What it does is basically takes your array (single array) and splits it into a bunch of arrays that are the same size, then it adds on the remainder in a new array at the end.

like image 940
Winderps Avatar asked Nov 28 '25 16:11

Winderps


1 Answers

Firstly your question isn't really related to loops at all, or at least loops are only addressed in your code. You should have titled this differently.

Secondly your array adding/removing could be improved; i.e. adding 1 to array size every time and removing 1 then re-copying the entire array every time is a time-sink.

Now onto your question, your code should basically look like this:

//Make your return array
int retLen = x.Length / y;      

//Add space for the remainder
if(x.Length % y != 0)
  retLen++;

var ret = new StringCollection[retLen];

//Reusing variables is a good way to save memory, but watch naming conventions as this can be confusing
retLen = 0;

var tempCollection = new StringCollection();

for (int i = 0; i < x.Length; i++)
{
  tempCollection = new StringCollection(tempCollection, x[i]);

  if(i % y == 0 || i == x.Length - 1)
  {
    ret[retLen++] = tempCollection;
    tempCollection = new StringCollection();
    retLen = 0;
  }    
}

return ret;

I really don't like that you don't have a Add function in this struct, just so we're clear. the tempCollection = new StringCollection(tempCollection, x[i]); is f$*kin' TERRIBLE when it comes to time CPU time to create all those new objects.

Pretty sure you'll need to tweak that to make sure all items are entered properly, but that was a first attempt, so ... meh o.O Figured since no one was actually going to answer you I'd take the time.

EDIT: Found a bug, forgot to set retLen back to 0 when adding to ret

like image 105
Ben K. Avatar answered Nov 30 '25 05:11

Ben K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!