00001 /* 00002 * Copyright (c) 2000, 2020, Oracle and/or its affiliates. 00003 * 00004 * Licensed under the Universal Permissive License v 1.0 as shown at 00005 * http://oss.oracle.com/licenses/upl. 00006 */ 00007 #ifndef COH_PARTITION_SET_HPP 00008 #define COH_PARTITION_SET_HPP 00009 00010 #include "coherence/lang.ns" 00011 00012 #include "coherence/io/pof/PofReader.hpp" 00013 #include "coherence/io/pof/PofWriter.hpp" 00014 #include "coherence/io/pof/PortableObject.hpp" 00015 00016 COH_OPEN_NAMESPACE3(coherence,net,partition) 00017 00018 using coherence::io::pof::PofReader; 00019 using coherence::io::pof::PofWriter; 00020 using coherence::io::pof::PortableObject; 00021 00022 00023 /** 00024 * PartitionSet is a light-weight data structure that represents a set of 00025 * partitions that are used in parallel processing. This set quite often 00026 * accompanies a result of partial parallel execution and is used to determine 00027 * whether or not the entire set of partitions was successfully processed. 00028 * 00029 * Note that all PartitionSet operations that take another set as an argument 00030 * assume that both sets have the same partition count. 00031 * 00032 * @since Coherence 3.7.1 00033 * @author tb 2011.08.12 00034 */ 00035 class COH_EXPORT PartitionSet 00036 : public class_spec<PartitionSet, 00037 extends<Object>, 00038 implements<PortableObject> > 00039 { 00040 friend class factory<PartitionSet>; 00041 00042 // ----- Format definition ---------------------------------------------- 00043 00044 public: 00045 /** 00046 * Serialization format indicator. 00047 */ 00048 typedef enum 00049 { 00050 /** 00051 * Indicates that no partitions are marked; MARKED_NONE requires 00052 * no additional data. 00053 */ 00054 marked_none = 0, 00055 00056 /** 00057 * Indicates that a small number of partitions are marked; 00058 * followed by stream of packed integers indicating gaps between 00059 * each marked partition, terminated with a -1. 00060 */ 00061 marked_few = 1, 00062 00063 /** 00064 * Indicates that a large number of partitions are marked; 00065 * followed by a sequence of 64-bit values sufficient to represent 00066 * the cardinality of the PartitionSet. 00067 */ 00068 marked_many = 2, 00069 00070 /** 00071 * Indicates that all partitions are marked; MARKED_ALL requires 00072 * no additional data. 00073 */ 00074 marked_all = 3 00075 } Format; 00076 00077 00078 // ----- Constructors --------------------------------------------------- 00079 00080 public: 00081 /** 00082 * Default constructor. 00083 */ 00084 PartitionSet(); 00085 00086 00087 // ----- pseudo Set operations ------------------------------------------ 00088 00089 public: 00090 /** 00091 * Add the specified partition to the set. 00092 * 00093 * @param nPartition the partition to add 00094 * 00095 * @return true if the specified partition was actually added as a 00096 * result of this call; false otherwise 00097 */ 00098 virtual bool add(int32_t nPartition); 00099 00100 /** 00101 * Add the specified PartitionSet to this set. 00102 * 00103 * @param vPartitions the PartitionSet to add 00104 * 00105 * @return true if all of the partitions were actually added as a 00106 * result of this call; false otherwise 00107 */ 00108 virtual bool add(PartitionSet::View vPartitions); 00109 00110 /** 00111 * Fill the set to contain all the partitions. 00112 */ 00113 virtual void fill(); 00114 00115 /** 00116 * Return an index of the first marked partition that is greater than 00117 * or equal to the specified partition. If no such partition exists 00118 * then -1 is returned. 00119 * 00120 * This method could be used to iterate over all marked partitions: 00121 * <pre> 00122 * for (int i = ps.next(0); i >= 0; i = ps->next(i+1)) 00123 * { 00124 * // process partition 00125 * } 00126 * </pre> 00127 * 00128 * @param nPartition the partition to start checking from (inclusive) 00129 * 00130 * @return the next marked partition, or -1 if no next marked partition 00131 * exists in the set 00132 * 00133 * @throws IndexOutOfBoundsException if the specified partition is 00134 * invalid 00135 */ 00136 virtual int32_t next(int32_t nPartition) const; 00137 00138 00139 // ----- helpers -------------------------------------------------------- 00140 00141 protected: 00142 /** 00143 * Determine the number of trailing zero bits in the passed long value. 00144 * 00145 * @param l a long value 00146 * 00147 * @return the number of trailing zero bits in the value, from 0 00148 * (indicating that the least significant bit is set) to 64 00149 * (indicating that no bits are set) 00150 */ 00151 static int32_t getTrailingZeroCount(int64_t l); 00152 00153 00154 // ----- Object interface ----------------------------------------------- 00155 00156 public: 00157 /** 00158 * {@inheritDoc} 00159 */ 00160 virtual TypedHandle<const String> toString() const; 00161 00162 00163 // ----- PortableObject interface --------------------------------------- 00164 00165 public: 00166 /** 00167 * {@inheritDoc} 00168 */ 00169 virtual void readExternal(PofReader::Handle hIn); 00170 00171 /** 00172 * {@inheritDoc} 00173 */ 00174 virtual void writeExternal(PofWriter::Handle hOut) const; 00175 00176 00177 // ----- data members --------------------------------------------------- 00178 00179 protected: 00180 /** 00181 * Total partition count. 00182 */ 00183 int32_t m_cPartitions; 00184 00185 /** 00186 * A bit array representing the partitions, stored as an array of longs. 00187 */ 00188 FinalHandle< Array<int64_t> > f_halBits; 00189 00190 /** 00191 * A mask for the last long that indicates what bits get used. 00192 */ 00193 int64_t m_lTailMask; 00194 00195 /** 00196 * A cached count of marked partitions; -1 indicates that the value must 00197 * be recalculated. 00198 */ 00199 int32_t m_cMarked; 00200 }; 00201 00202 COH_CLOSE_NAMESPACE3 00203 00204 #endif // COH_PARTITION_SET_HPP