Click here to Skip to main content
15,888,116 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes6-Jul-09 5:17
ProtoBytes6-Jul-09 5:17 
GeneralRe: Sith Interviewing Tactics Pin
molesworth6-Jul-09 4:14
molesworth6-Jul-09 4:14 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes18-Jul-09 5:10
ProtoBytes18-Jul-09 5:10 
GeneralRe: Sith Interviewing Tactics Pin
Super Lloyd18-Jul-09 14:11
Super Lloyd18-Jul-09 14:11 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes18-Jul-09 14:28
ProtoBytes18-Jul-09 14:28 
AnswerRe: Sith Interviewing Tactics Pin
ProtoBytes18-Jul-09 20:19
ProtoBytes18-Jul-09 20:19 
GeneralRe: Sith Interviewing Tactics [modified] Pin
Super Lloyd18-Jul-09 21:38
Super Lloyd18-Jul-09 21:38 
GeneralRe: Sith Interviewing Tactics Pin
Daniel Grunwald20-Jul-09 4:05
Daniel Grunwald20-Jul-09 4:05 
Super Lloyd wrote:


Why would getting the list of all control in a hierarchy this way be any bad?


In this case, it's bad because for every control in the innermost layer, it has to be passed up N levels on the stack (once for each yield return). This means iterating through the tree will be O(N^2) worst-case.

But this can be easily solved using CPS (continuation passing style):
void IterateUITree(Control c, Action<Control> listener)
{
  listener(c);
  foreach(Control child in c.Children)
    IterateUITree(child, listener);
}


But if you actually need a IEnumerable<T>, you need to give up recursion and manage the stack yourself (this also means manually repeating the foreach-magic):
/// <summary>
/// Converts a recursive data structure into a flat list.
/// </summary>
/// <param name="input">The root elements of the recursive data structure.</param>
/// <param name="recursive">The function that gets the children of an element.</param>
/// <returns>Iterator that enumerates the tree structure in preorder.</returns>
public static IEnumerable<T> Flatten<T>(this IEnumerable<T> input, Func<T, IEnumerable<T>> recursion)
{
	Stack<IEnumerator<T>> stack = new Stack<IEnumerator<T>>();
	try {
		stack.Push(input.GetEnumerator());
		while (stack.Count > 0) {
			while (stack.Peek().MoveNext()) {
				T element = stack.Peek().Current;
				yield return element;
				IEnumerable<T> children = recursion(element);
				if (children != null) {
					stack.Push(children.GetEnumerator());
				}
			}
			stack.Pop().Dispose();
		}
	} finally {
		while (stack.Count > 0) {
			stack.Pop().Dispose();
		}
	}
}


Usage:
Control[] roots = { control };
var flatList = roots.Flatten(c=>c.Children);

GeneralRe: Sith Interviewing Tactics Pin
Super Lloyd20-Jul-09 13:05
Super Lloyd20-Jul-09 13:05 
GeneralRe: Sith Interviewing Tactics Pin
Daniel Grunwald20-Jul-09 23:25
Daniel Grunwald20-Jul-09 23:25 
GeneralRe: Sith Interviewing Tactics Pin
Super Lloyd20-Jul-09 23:44
Super Lloyd20-Jul-09 23:44 
GeneralRe: Sith Interviewing Tactics Pin
Super Lloyd21-Jul-09 0:01
Super Lloyd21-Jul-09 0:01 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes21-Jul-09 8:26
ProtoBytes21-Jul-09 8:26 
GeneralRe: Sith Interviewing Tactics Pin
supercat96-Jul-09 6:58
supercat96-Jul-09 6:58 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes6-Jul-09 7:20
ProtoBytes6-Jul-09 7:20 
GeneralRe: Sith Interviewing Tactics Pin
Distind6-Jul-09 6:44
Distind6-Jul-09 6:44 
GeneralRe: Sith Interviewing Tactics [modified] Pin
ProtoBytes6-Jul-09 7:26
ProtoBytes6-Jul-09 7:26 
GeneralRe: Sith Interviewing Tactics Pin
Ian_Sharpe6-Jul-09 10:26
Ian_Sharpe6-Jul-09 10:26 
GeneralRe: Sith Interviewing Tactics Pin
Viral Upadhyay8-Jul-09 4:34
Viral Upadhyay8-Jul-09 4:34 
GeneralRe: Sith Interviewing Tactics Pin
Vozzie27-Jul-09 4:13
Vozzie27-Jul-09 4:13 
GeneralRe: Sith Interviewing Tactics [modified] Pin
ProtoBytes7-Jul-09 14:36
ProtoBytes7-Jul-09 14:36 
GeneralRe: Sith Interviewing Tactics Pin
S. Senthil Kumar11-Jul-09 22:01
S. Senthil Kumar11-Jul-09 22:01 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes12-Jul-09 2:16
ProtoBytes12-Jul-09 2:16 
GeneralRe: Sith Interviewing Tactics [modified] Pin
ProtoBytes7-Jul-09 15:04
ProtoBytes7-Jul-09 15:04 
GeneralRe: Sith Interviewing Tactics Pin
ProtoBytes7-Jul-09 15:31
ProtoBytes7-Jul-09 15:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.