Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 623355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:05:03+00:00 2026-05-13T19:05:03+00:00

Read the edit below for more information. I have some code below that I

  • 0

Read the edit below for more information.

I have some code below that I use to split a generic list of Object when the item is of a certain type.

    public static IEnumerable<object>[] Split(this  IEnumerable<object> tokens, TokenType type) {

        List<List<object>> t = new List<List<object>>();
        int currentT = 0;
        t.Add(new List<object>());
        foreach (object list in tokens) {
            if ((list is Token) && (list as Token).TokenType == type) {
                currentT++;
                t.Add(new List<object>());

            }
            else if ((list is TokenType) && ((TokenType)list )== type) {
                currentT++;
                t.Add(new List<object>());

            }
            else {
                t[currentT].Add(list);
            }
        }

        return t.ToArray();
    }

I dont have a clear question as much as I am curious if anyone knows of any ways I can optimize this code. I call it many times and it seems to be quite the beast as far as clock cycles go. Any ideas? I can also make it a Wiki if anyone is interested, maybe we can keep track of the latest changes.

Update: Im trying to parse out specific tokens. Its a list of some other class and Token classes. Token has a property (enum) of TokenType. I need to find all the Token classes and split on each of them.

{a b c T d e T f g h T i j k l T m} 

would split like

{a b c}{d e}{f g h}{i j k l}{m}

EDIT UPDATE:
It seems like all of my speed problems come into the constant creation and addition of Generic Lists. Does anyone know how I can go about this without that?
This is the profile of what is happening if it helps anyone.

alt text

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:05:03+00:00Added an answer on May 13, 2026 at 7:05 pm

    This is the best I could do to eliminate as much of the allocation times as possible for the function (should only allocate when it goes over the capacity, which should be no more than what is required to create the largest sub list in the results). I’ve tested this implementation and it works as you described.

    Please note that the results of the prior sub list are destroyed when the next list in the group is accessed.

    public static IEnumerable<IEnumerable> Split(this  IEnumerable tokens, TokenType type)
    {
        ArrayList currentT = new ArrayList();
        foreach (object list in tokens)
        {
            Token token = list as Token;
            if ((token != null) && token.TokenType == type)
            {
                yield return currentT;
                currentT.Clear();
                //currentT = new ArrayList(); <-- Use this instead of 'currentT.Clear();' if you want the returned lists to be a different instance
            }
            else if ((list is TokenType) && ((TokenType)list) == type)
            {
                yield return currentT;
                currentT.Clear();
                //currentT = new ArrayList(); <-- Use this instead of 'currentT.Clear();' if you want the returned lists to be a different instance
            }
            else
            {
                currentT.Add(list);
            }
        }
    }
    

    EDIT
    Here’s another version that doesn’t make use of another list at all (shouldn’t be doing any allocations). Not sure how well this will compare, but it does work as requested (also I’ve got no idea how this one will go if you try to cache a sub ‘array’).

    Also, both of these require a “using System.Collections” statement (in addition to the Generic namespace).

    private static IEnumerable SplitInnerLoop(IEnumerator iter, TokenType type)
    {
        do
        {
            Token token = iter.Current as Token;
            if ((token != null) && token.TokenType == type)
            {
                break;
            }
            else if ((iter.Current is TokenType) && ((TokenType)iter.Current) == type)
            {
                break;
            }
            else
            {
                yield return iter.Current;
            }
        } while (iter.MoveNext());
    }
    
    public static IEnumerable<IEnumerable> Split(this  IEnumerable tokens, TokenType type)
    {
        IEnumerator iter = tokens.GetEnumerator();
        while (iter.MoveNext())
        {
            yield return SplitInnerLoop(iter, type);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The code you provided works as you would expect. (At… May 14, 2026 at 9:55 pm
  • Editorial Team
    Editorial Team added an answer Timestamp is a data type and a built-in function in… May 14, 2026 at 9:55 pm
  • Editorial Team
    Editorial Team added an answer To create a new text file FileOutputStream object=new FileOutputStream("a.txt",true); object.write(byte[]);… May 14, 2026 at 9:55 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.