From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Status: No, score=-3.9 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by dcvr.yhbt.net (Postfix) with ESMTP id 4B8401F4D7 for ; Thu, 19 May 2022 20:15:09 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; unprotected) header.d=pobox.com header.i=@pobox.com header.b="r0iU803b"; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244460AbiESUPG (ORCPT ); Thu, 19 May 2022 16:15:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230395AbiESUPF (ORCPT ); Thu, 19 May 2022 16:15:05 -0400 Received: from pb-smtp2.pobox.com (pb-smtp2.pobox.com [64.147.108.71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BD4FC9ECD for ; Thu, 19 May 2022 13:15:04 -0700 (PDT) Received: from pb-smtp2.pobox.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 7BBA211C0D7; Thu, 19 May 2022 16:15:02 -0400 (EDT) (envelope-from junio@pobox.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=pobox.com; h=from:to:cc :subject:references:date:in-reply-to:message-id:mime-version :content-type; s=sasl; bh=V5mrra170BK3LVjOnChQ05ubImbaMmrwCqQ7eZ b0pug=; b=r0iU803bbi6BgZhQXXi7it4Dq8CIRyuaQfMCBz/dHROs4w6TCQpcd4 QrrhsnUDNcV32WTZHKEMydIE6TEFoQwv+3wjniuH7fvaM/o+be+g6TZIzuprHUph p8SnVsZC2gsUYlgPs3NdOJMJgLMEOIiNCGIBrt4N/xAFaGo8YJwX0= Received: from pb-smtp2.nyi.icgroup.com (unknown [127.0.0.1]) by pb-smtp2.pobox.com (Postfix) with ESMTP id 39A6011C0D5; Thu, 19 May 2022 16:15:02 -0400 (EDT) (envelope-from junio@pobox.com) Received: from pobox.com (unknown [34.83.65.128]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pb-smtp2.pobox.com (Postfix) with ESMTPSA id 0341411C0D4; Thu, 19 May 2022 16:15:00 -0400 (EDT) (envelope-from junio@pobox.com) From: Junio C Hamano To: "Derrick Stolee via GitGitGadget" Cc: git@vger.kernel.org, vdye@github.com, shaoxuan.yuan02@gmail.com, Derrick Stolee , Derrick Stolee Subject: Re: [PATCH v2 05/10] cache-tree: implement cache_tree_find_path() References: Date: Thu, 19 May 2022 13:14:59 -0700 In-Reply-To: (Derrick Stolee via GitGitGadget's message of "Thu, 19 May 2022 17:52:33 +0000") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Pobox-Relay-ID: 5CA823D4-D7B0-11EC-B116-CB998F0A682E-77302942!pb-smtp2.pobox.com Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org "Derrick Stolee via GitGitGadget" writes: > +struct cache_tree *cache_tree_find_path(struct cache_tree *it, const char *path) > +{ > + const char *slash; > + int namelen; > + struct cache_tree_sub *down; > + > + if (!it) > + return NULL; > + slash = strchrnul(path, '/'); > + namelen = slash - path; > + it->entry_count = -1; > + if (!*slash) { > + int pos; > + pos = cache_tree_subtree_pos(it, path, namelen); > + if (0 <= pos) > + return it->down[pos]->cache_tree; > + return NULL; > + } > + down = find_subtree(it, path, namelen, 0); > + if (down) > + return cache_tree_find_path(down->cache_tree, slash + 1); > + return NULL; > +} The tail recursion (and the one in the orginal) may want to be trivially converted to iteration with "goto". It is somewhat surprising that we didn't have any external interface to expose a sub-part of the cache_tree at all so far. It may be because the API was so well designed that the abstraction did not have to be exposed. I dunno. > diff --git a/cache-tree.h b/cache-tree.h > index 8efeccebfc9..f75f8e74dcd 100644 > --- a/cache-tree.h > +++ b/cache-tree.h > @@ -29,6 +29,8 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *); > > int cache_tree_subtree_pos(struct cache_tree *it, const char *path, int pathlen); > > +struct cache_tree *cache_tree_find_path(struct cache_tree *it, const char *path); > + > void cache_tree_write(struct strbuf *, struct cache_tree *root); > struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);