]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 1996-2025 The Squid Software Foundation and contributors | |
3 | * | |
4 | * Squid software is distributed under GPLv2+ license and includes | |
5 | * contributions from numerous individuals and organizations. | |
6 | * Please see the COPYING and CONTRIBUTORS files for details. | |
7 | */ | |
8 | ||
9 | #ifndef SQUID_SRC_DESCRIPTORSET_H | |
10 | #define SQUID_SRC_DESCRIPTORSET_H | |
11 | ||
12 | #include <iosfwd> | |
13 | ||
14 | /* TODO: Should we use std::set<int> with its flexibility? Our implementation | |
15 | has constant overhead, which is smaller than log(n) of std::set. | |
16 | */ | |
17 | /// An unordered collection of unique descriptors with O(1) add/del/has ops | |
18 | class DescriptorSet | |
19 | { | |
20 | public: | |
21 | // for STL compatibility, should we decide to switch to std::set or similar | |
22 | typedef const int *const_iterator; | |
23 | ||
24 | DescriptorSet(); | |
25 | ~DescriptorSet(); | |
26 | ||
27 | /// checks whether fd is in the set | |
28 | bool has(const int fd) const { | |
29 | return 0 <= fd && fd < capacity_ && | |
30 | index_[fd] >= 0; | |
31 | } | |
32 | ||
33 | bool add(int fd); ///< adds if unique; returns true if added | |
34 | bool del(int fd); ///< deletes if there; returns true if deleted | |
35 | int pop(); ///< deletes and returns one descriptor, in unspecified order | |
36 | ||
37 | bool empty() const { return !size_; } ///< number of descriptors in the set | |
38 | ||
39 | /// begin iterator a la STL; may become invalid if the object is modified | |
40 | const_iterator begin() const { return descriptors_; } | |
41 | /// end iterator a la STL; may become invalid if the object is modified | |
42 | const_iterator end() const { return begin() + size_; } | |
43 | ||
44 | /// outputs debugging info about the set | |
45 | void print(std::ostream &os) const; | |
46 | ||
47 | private: | |
48 | // these would be easy to support when needed; prohibit for now | |
49 | DescriptorSet(const DescriptorSet &s); // declared but undefined | |
50 | DescriptorSet &operator =(const DescriptorSet &s); // declared, undefined | |
51 | ||
52 | int *descriptors_; ///< descriptor values in random order | |
53 | int *index_; ///< descriptor:position index into descriptors_ | |
54 | int capacity_; ///< total number of descriptor slots | |
55 | int size_; ///< number of descriptors in the set | |
56 | }; | |
57 | ||
58 | /// convenience wrapper to be used in debugs() context | |
59 | inline std::ostream & | |
60 | operator <<(std::ostream &os, const DescriptorSet &ds) | |
61 | { | |
62 | ds.print(os); | |
63 | return os; | |
64 | } | |
65 | ||
66 | #endif /* SQUID_SRC_DESCRIPTORSET_H */ | |
67 |