Tech Off Post

Single Post Permalink

View Thread: SimpleCollection/Buffer for C#? (Think text editor buffers except with objects)
  • User profile image

    I use List<> quite a bit and find that most of the time when I value simplicity of the code, it doesn't seem to do enough.

    I want this to compile and not throw exceptions at any line:

    var st=new SimpleThing<string>(default=""); // Thing that works a lot like text editor buffer except with objects instead of characters
    st[4]="fifth"; // conceptually similar to Console.Left=4; Console.Write("f"); Console.Left=(last position); except that "f" was the object in this case string "fifth";
    st.Index=3; // conceptually similar to Console.Left=3;
    st.Write("Fourth"); //and increment internal index/position, just like Console.Write moves to next position
    st.Write("overwriting fifth"); 
    st.Insert(4,"new fifth"); //now [5]="overwriting fifth", internal position not touched 
    //st.Insert(otherst); // would enumerate over otherst insert contents before "overwriting fifth" and after "new fifth"
    //var thisisTrue = st.Current == otherst.Take(1); //otherst[0] could throw if default was not set
    var q=st[1]; // q=="", if default param not set, throw exception, if default=null then its null.
    foreach(var t in st) // stuff can be modified, removed etc without exceptions during the loop
        //st.Delete(t); // same as pressing Delete key on keyboard, everything coming after would move if this was not commented, the next one taking place of current
        st.Clear(t); // first "Fourth" would become "" (since default="")
        //all others that weren't default or not set would be enumerated and be cleared to default
    foreach(var t in st)
    //... nothing to enumerate over
    Console.Write(st[666]);// write the default value
    output (with added quotes since last line was the default ""):
    "new fifth"
    "overwriting fifth"

    (There should be a way to specify the type of the used index, so it can be eg. ulong or int so you could enter negative indexes as well, in which case enumeration starts from the lowest index, which could be negative)



    Had a bit of a blunder on the foreach there. It should've been st.Delete(t); and st.Clear(t); - correcting that. I'm still not sure it can be made to work at all.

    Alternative that could work is

    while(st.Next) { st.Clear() } ... might do but that's not so simple anymore, losing the foreach word and now it's starting from whatever location the pointer/index was at.