From patchwork Thu Jun 25 19:13:40 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 488521 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 815DC1401B5 for ; Fri, 26 Jun 2015 05:00:46 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=Wj8B4UCh; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; q=dns; s= default; b=AQSbg9HYxCnW7PSoZSUykFxKLyR+NiozLtBwqOI3CimtOjzJMhWVN M7d3g/33VzNW5sOoZvrYC5NQZ1on072kNAk28jpWCL1b7Tmnypsvc4b2ZCd93AIy wEgYUXFJow6CDS8AziIdBgAm1zpEKtL7w/f5/IUfAoPhZCBT7MLHGA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references; s= default; bh=9fnUjsNxl4i/AecZt/mNNymRo5E=; b=Wj8B4UChaoeosbH7tHMm 9Rfc9Bsa09b2p0ovS8Aupt1qRD5BFU78XOrjtzq86rPjsp4+AQ1tf3Q0kvl93ZnF 0rVnF/JqS2YgDuXvNH6M6J43QY2hwKrnP2WBm/ljvZeyhs8oJxjLKw1g8DKZcMVX tKPiXlawlv/ADpb2xJ+pI4Q= Received: (qmail 128611 invoked by alias); 25 Jun 2015 19:00:38 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 128584 invoked by uid 89); 25 Jun 2015 19:00:37 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 25 Jun 2015 19:00:35 +0000 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id 43EAE3790F5; Thu, 25 Jun 2015 19:00:34 +0000 (UTC) Received: from c64.redhat.com (vpn-231-174.phx2.redhat.com [10.3.231.174]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t5PJ0XJj021440; Thu, 25 Jun 2015 15:00:33 -0400 From: David Malcolm To: gcc-patches@gcc.gnu.org, jit@gcc.gnu.org Cc: David Malcolm Subject: [PATCH 1/2] Add gcc/typed-splay-tree.h Date: Thu, 25 Jun 2015 15:13:40 -0400 Message-Id: <1435259621-45194-1-git-send-email-dmalcolm@redhat.com> In-Reply-To: <558A846C.70003@starynkevitch.net> References: <558A846C.70003@starynkevitch.net> X-IsSubscribed: yes I found when implementing switch statements for the jit that it was much easier to work with libiberty's splay-tree.h by first wrapping it in a C++ wrapper to add typesafety. This patch adds such a wrapper, implementing the methods I needed. It's unused in this patch, but is used in the followup patch (which only touches the jit). OK for trunk? gcc/ChangeLog: * typed-splay-tree.h: New file. --- gcc/typed-splay-tree.h | 135 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 gcc/typed-splay-tree.h diff --git a/gcc/typed-splay-tree.h b/gcc/typed-splay-tree.h new file mode 100644 index 0000000..1ec4894 --- /dev/null +++ b/gcc/typed-splay-tree.h @@ -0,0 +1,135 @@ +/* A typesafe wrapper around libiberty's splay-tree.h. + Copyright (C) 2015 Free Software Foundation, Inc. + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free +Software Foundation; either version 3, or (at your option) any later +version. + +GCC is distributed in the hope that it will be useful, but WITHOUT ANY +WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#ifndef GCC_TYPED_SPLAY_TREE_H +#define GCC_TYPED_SPLAY_TREE_H + +#include "splay-tree.h" + +/* Typesafe wrapper around libiberty's splay-tree.h. */ +template +class typed_splay_tree +{ + public: + typedef KEY_TYPE key_type; + typedef VALUE_TYPE value_type; + + typedef int (*compare_fn) (key_type, key_type); + typedef void (*delete_key_fn) (key_type); + typedef void (*delete_value_fn) (value_type); + + typed_splay_tree (compare_fn, + delete_key_fn, + delete_value_fn); + ~typed_splay_tree (); + + value_type lookup (key_type k); + value_type predecessor (key_type k); + value_type successor (key_type k); + value_type insert (key_type k, value_type v); + + private: + static value_type node_to_value (splay_tree_node node); + + private: + ::splay_tree m_inner; +}; + +/* Constructor for typed_splay_tree . */ + +template +inline typed_splay_tree:: + typed_splay_tree (compare_fn compare_fn, + delete_key_fn delete_key_fn, + delete_value_fn delete_value_fn) +{ + m_inner = splay_tree_new ((splay_tree_compare_fn)compare_fn, + (splay_tree_delete_key_fn)delete_key_fn, + (splay_tree_delete_value_fn)delete_value_fn); +} + +/* Destructor for typed_splay_tree . */ + +template +inline typed_splay_tree:: + ~typed_splay_tree () +{ + splay_tree_delete (m_inner); +} + +/* Lookup KEY, returning a value if present, and NULL + otherwise. */ + +template +inline VALUE_TYPE +typed_splay_tree::lookup (key_type key) +{ + splay_tree_node node = splay_tree_lookup (m_inner, (splay_tree_key)key); + return node_to_value (node); +} + +/* Return the immediate predecessor of KEY, or NULL if there is no + predecessor. KEY need not be present in the tree. */ + +template +inline VALUE_TYPE +typed_splay_tree::predecessor (key_type key) +{ + splay_tree_node node = splay_tree_predecessor (m_inner, (splay_tree_key)key); + return node_to_value (node); +} + +/* Return the immediate successor of KEY, or NULL if there is no + successor. KEY need not be present in the tree. */ + +template +inline VALUE_TYPE +typed_splay_tree::successor (key_type k) +{ + splay_tree_node node = splay_tree_successor (m_inner, (splay_tree_key)k); + return node_to_value (node); +} + +/* Insert a new node (associating KEY with VALUE). If a + previous node with the indicated KEY exists, its data is replaced + with the new value. */ + +template +inline void +typed_splay_tree::insert (key_type key, + value_type value) +{ + splay_tree_insert (m_inner, + (splay_tree_key)key, + (splay_tree_value)value); +} + +/* Internal function for converting from splay_tree_node to + VALUE_TYPE. */ +template +inline VALUE_TYPE +typed_splay_tree::node_to_value (splay_tree_node node) +{ + if (node) + return (value_type)node->value; + else + return 0; +} + +#endif /* GCC_TYPED_SPLAY_TREE_H */