recursion - C# get position of object recursively -
i have class self-referenced. idmodulopai key points parent, , modulosfilhos children of object.
i have property, profundidade, recursively calc depth of object.
another important property ordem. holds desired order defined user in scope.
id nome idmodulopai ordem profundidade ordemglobal 1 root [null] 0 0 0 2 users 1 0 1 1 3 administration 2 0 2 2 4 logs 2 1 2 3 5 customers 1 0 1 4 6 orders 5 0 2 5
look @ example table.
i'm trying create function similar profundidade, calcs global position. i'm trying last column ordemglobal. than, can order objects ordemglobal , appear in same way in every local need.
based in table, correct position is
root +users +administration +logs +customers +orders
look administration apperars before logs because administration has ordem = 0 , logs has ordem = 1
how can archieve desired behavior?
code of class follows
public class modulomodel { public int id { get; set; } public string nome { get; set; } public int ordem { get; set; } public virtual int profundidade { { return getdepth(this); } } public int? idmodulopai { get; set; } public virtual modulomodel modulopai { get; set; } public virtual icollection<modulomodel> modulosfilhos { get; set; } private int getdepth(modulomodel modulomodel) { if (modulomodel == null) return 0; if (modulomodel.idmodulopai == null) return 0; return getdepth(modulomodel.modulopai) + 1; } }
edit: improved question
i'm tryed like
public virtual int ordemglobal { { return getglobalorder(this); } } private int getglobalorder(modulomodel modulomodel) { if (modulomodel == null) return 0; if (modulomodel.modulopai == null) return 0; int smallersiblings = modulomodel.modulopai.modulosfilhos.where(x => x.ordem < modulomodel.ordem).count(); return (getglobalorder(modulomodel.modulopai) + smallersiblings + 1; }
but confused, , aren't returning desired information.
here icomparer<modulomodel>
sorts order want.
public class modulomodelcomparer : comparer<modulomodel> { public override int compare(modulomodel x, modulomodel y) { //they same node. if (x.equals(y)) return 0; //cache values don't need getdepth call times var xprofundidade = x.profundidade; var yprofundidade = y.profundidade; //find shared parent if (xprofundidade > yprofundidade) { //x child of y if (x.modulopai.equals(y)) return 1; return compare(x.modulopai, y); } else if (yprofundidade > xprofundidade) { //y child of x if (x.equals(y.modulopai)) return -1; return compare(x, y.modulopai); } else { //they both share parent not same node, compare on ordem. if (x.modulopai.equals(y.modulopai)) return x.ordem.compareto(y.ordem); //they same level have diffrent parents, go layer return compare(x.modulopai, y.modulopai); } } }
here test program uses it
class test { public static void main() { var root = createmodel(1, "root", null, 0); var users = createmodel(2, "users", root, 0); var administration = createmodel(3, "administration", users, 0); var logs = createmodel(4, "logs", users, 1); var customers = createmodel(5, "customers", root, 0); var orders = createmodel(6, "orders", customers, 0); list<modulomodel> list = new list<modulomodel> {root, users, administration, logs, customers, orders}; list.sort(new modulomodelcomparer()); foreach (var modulomodel in list) { console.writeline(modulomodel.nome); } console.readline(); } private static modulomodel createmodel(int id, string nome, modulomodel modulopai, int ordem) { var model = new modulomodel {id = id, nome = nome, idmodulopai = modulopai?.id, modulopai = modulopai, modulosfilhos = new hashset<modulomodel>(), ordem = ordem}; modulopai?.modulosfilhos.add(model); return model; } }
hopefully enough on right track.
Comments
Post a Comment