From c570556b697cb30829b2cac2d1c5fd86d501022e Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Thu, 18 Feb 2016 23:55:25 -0800 Subject: 2675 Make nth_type non-recursive in advance of hoisting the call into the caller. --- 058shape_shifting_container.cc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to '058shape_shifting_container.cc') diff --git a/058shape_shifting_container.cc b/058shape_shifting_container.cc index 7b11a61e..6ad33fd6 100644 --- a/058shape_shifting_container.cc +++ b/058shape_shifting_container.cc @@ -396,20 +396,18 @@ void test_replace_middle_type_ingredient_with_multiple() { const type_tree* nth_type(const type_tree* base, long long int n) { assert(n >= 0); - if (n == 0) { - if (base && base->left) return base->left; - return base; - } - return nth_type(base->right, n-1); + for (; n > 0; --n) + base = base->right; + if (base && base->left) return base->left; + return base; } const string_tree* nth_type_name(const string_tree* base, long long int n) { assert(n >= 0); - if (n == 0) { - if (base && base->left) return base->left; - return base; - } - return nth_type_name(base->right, n-1); + for (; n > 0; --n) + base = base->right; + if (base && base->left) return base->left; + return base; } bool has_nth_type(const type_tree* base, long long int n) { -- cgit 1.4.1-2-gfad0