File indexing completed on 2025-01-05 04:37:11

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef DHTKCLOSESTNODESSEARCH_H
0007 #define DHTKCLOSESTNODESSEARCH_H
0008 
0009 #include "kbucket.h"
0010 #include "key.h"
0011 #include <map>
0012 
0013 namespace dht
0014 {
0015 class PackedNodeContainer;
0016 
0017 /**
0018  * @author Joris Guisson <joris.guisson@gmail.com>
0019  *
0020  * Class used to store the search results during a K closests nodes search
0021  * Note: we use a std::map because of lack of functionality in QMap
0022  */
0023 class KClosestNodesSearch
0024 {
0025     dht::Key key;
0026     std::map<dht::Key, KBucketEntry> emap;
0027     bt::Uint32 max_entries;
0028 
0029 public:
0030     /**
0031      * Constructor sets the key to compare with
0032      * @param key The key to compare with
0033      * @param max_entries The maximum number of entries can be in the map
0034      * @return
0035      */
0036     KClosestNodesSearch(const dht::Key &key, bt::Uint32 max_entries);
0037     virtual ~KClosestNodesSearch();
0038 
0039     typedef std::map<dht::Key, KBucketEntry>::iterator Itr;
0040     typedef std::map<dht::Key, KBucketEntry>::const_iterator CItr;
0041 
0042     Itr begin()
0043     {
0044         return emap.begin();
0045     }
0046     Itr end()
0047     {
0048         return emap.end();
0049     }
0050 
0051     CItr begin() const
0052     {
0053         return emap.begin();
0054     }
0055     CItr end() const
0056     {
0057         return emap.end();
0058     }
0059 
0060     /// Get the target key of the search3
0061     const dht::Key &getSearchTarget() const
0062     {
0063         return key;
0064     }
0065 
0066     /// Get the number of entries.
0067     bt::Uint32 getNumEntries() const
0068     {
0069         return emap.size();
0070     }
0071 
0072     /**
0073      * Try to insert an entry.
0074      * @param e The entry
0075      */
0076     void tryInsert(const KBucketEntry &e);
0077 
0078     /**
0079      * Pack the search results in a PackedNodeContainer.
0080      * @param cnt Place to store IPv6 nodes
0081      */
0082     void pack(PackedNodeContainer *cnt);
0083 };
0084 
0085 }
0086 
0087 #endif