Re: This isn't infinitely recursive is it?
| From: | Jonathan Tapicer | Date: | Thu, 30 Jul 2009 23:52:08 +0000 |
| Subject: | Re: This isn't infinitely recursive is it? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296083@lists.php.net to get a copy of this message | ||
Hi,
Well, you will have an infinite recursion there if the mapping has
cycles, something like A->B, B->C, C->A would generate an invite
recursion.
Checking if the mapping has cycles is pretty simple: you have to
create a directed graph and then go through the graph in DFS marking
each visited node, if you have to mark a node already marked then you
have a cycle. Tarjan's algorithm does that and a little more, see
here: http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
Hope that helps.
Jonathan
On Thu, Jul 30, 2009 at 8:38 PM, Matt Neimeyer<matt@neimeyer.org> wrote:
> I'm cleaning up some inherited code in our data import module. For a
> variety of reasons we have to support old standards of the import
> format. Since some of those old versions were created we have since
> renamed some fields in our data structure. So right now I've a hard
> map for some field names...
>
> function GetMappedField($Field)
> {
> $FieldMap["A"] = "B";
> $FieldMap["C"] = "D";
>
> return isset($FieldMap[$Field])?$FieldMap[$Field]:$Field);
> }
>
> But I've just spent a while tracking down a bug where someone mapped A
> to B and then someone else mapped B to C.
>
> I'm thinking of changing the return to...
>
> return isset($FieldMap[$Field])?GetMappedField($FieldMap[$Field]):$Field);
>
> ...but I'm worried about the recursion. (Which isn't a strength of mine)
>
> I don't THINK I need to worry about circular mappings... but I'm not
> sure how to check for it if I did...
>
> Any suggestions? Thanks!
>
> Matt
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>