Generated on: Thu Mar 29 07:46:58 PDT 2012 for custom file set | ||
|
||
00001 // Copyright 2010-2012 Google 00002 // Licensed under the Apache License, Version 2.0 (the "License"); 00003 // you may not use this file except in compliance with the License. 00004 // You may obtain a copy of the License at 00005 // 00006 // http://www.apache.org/licenses/LICENSE-2.0 00007 // 00008 // Unless required by applicable law or agreed to in writing, software 00009 // distributed under the License is distributed on an "AS IS" BASIS, 00010 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00011 // See the License for the specific language governing permissions and 00012 // limitations under the License. 00013 00014 #ifndef OR_TOOLS_UTIL_CONST_INT_ARRAY_H_ 00015 #define OR_TOOLS_UTIL_CONST_INT_ARRAY_H_ 00016 00017 #include <string> 00018 #include <vector> 00019 00020 #include "base/basictypes.h" 00021 #include "base/integral_types.h" 00022 #include "base/logging.h" 00023 #include "base/macros.h" 00024 #include "base/scoped_ptr.h" 00025 00026 using std::string; 00027 00028 namespace operations_research { 00029 // This class is used to store constant copies of int64 arrays. 00030 // 00031 // These copies are used inside constraints or expressions. When 00032 // constructed with a C array or a vector, The const int array will 00033 // make a internal copy and own that copy. It will not take ownership 00034 // of the vector/array which can be deleted afterwards. This follows 00035 // the semantics of constraints and expressions which store a 00036 // read-only copy of the data. 00037 // 00038 // Its goals are: 00039 // - to unify the construction code across the optimization libraries. 00040 // - to provide one code to scan these arrays and compute given properties like 00041 // monotonicity. 00042 // 00043 // As const int arrays provide scanning capabilities, the code inside 00044 // a constraint and its factory looks like: 00045 // 00046 // IntExpr* MakeMyExpr(const std::vector<int64>& values) { 00047 // ConstIntArray copy(values); 00048 // if (copy.Status(ConstIntArray::IS_INCREASING)) { 00049 // return new MyOptimizedExpr(copy.Release()); 00050 // } else { 00051 // return new MyGenericExpr(copy.Release()); 00052 // } 00053 // } 00054 // 00055 // With: 00056 // class MyOptimizedExpr : IntExpr { 00057 // public: 00058 // MyOptimizedExpr(std::vector<int64>* data) : values_(data) {} 00059 // private: 00060 // ConstIntArray values_; 00061 // }; 00062 class ConstIntArray { 00063 public: 00064 // These describe static properties of the int64 array. 00065 enum Property { 00066 IS_CONSTANT = 0, 00067 IS_STRICTLY_INCREASING = 1, 00068 IS_INCREASING = 2, 00069 IS_STRICTLY_DECREASING = 3, 00070 IS_DECREASING = 4, 00071 IS_BOOLEAN = 5, // in {0, 1} 00072 IS_POSITIVE = 6, // > 0 00073 IS_NEGATIVE = 7, // < 0 00074 IS_POSITIVE_OR_NULL = 8, // >= 0 00075 IS_NEGATIVE_OR_NULL = 9, // <= 0 00076 NUM_PROPERTY = 10 // Sentinel. 00077 }; 00078 00079 // Build from a vector. Copy the data internally. 00080 explicit ConstIntArray(const std::vector<int64>& data); 00081 // Build from a vector. Copy the data internally. 00082 explicit ConstIntArray(const std::vector<int>& data); 00083 // Build from a C array. Copy the data internally. 00084 ConstIntArray(const int64* const data, int size); 00085 // Build from a C array. Copy the data internally. 00086 ConstIntArray(const int* const data, int size); 00087 // Build from a pointer to a vector (usually created by the 00088 // Release(), or SortedCopy() method). This call will take ownership of 00089 // the data and not make a copy. 00090 explicit ConstIntArray(std::vector<int64>* data); 00091 00092 ~ConstIntArray(); 00093 00094 // Pretty print. 00095 string DebugString() const; 00096 00097 // This code release the ownership of the data into the returned vector. 00098 // After this method is called, data_ points to a null vector. 00099 std::vector<int64>* Release(); 00100 00101 // This will create a copy of the data. 00102 std::vector<int64>* Copy() const; 00103 00104 // This will create a new data holder with the sorted array. 00105 std::vector<int64>* SortedCopy(bool increasing) const; 00106 00107 // This will create a new data holder with the sorted array where 00108 // the duplicate values have been removed. 00109 std::vector<int64>* SortedCopyWithoutDuplicates(bool increasing) const; 00110 00111 // Equality test. 00112 bool Equals(const ConstIntArray& other) const; 00113 00114 // Size of the array. This is not valid after Release() has been called. 00115 int size() const; 00116 00117 // Operator to access the data at the given index. This is not valid 00118 // after a release. 00119 int64 operator[](int64 index) const { 00120 CHECK_NOTNULL(data_.get()); 00121 return (*data_)[index]; 00122 } 00123 00124 // Accessor to value in the array. 00125 int64 get(int64 index) const { 00126 CHECK_NOTNULL(data_.get()); 00127 return (*data_)[index]; 00128 } 00129 00130 // Access to const raw data. 00131 // TODO(user) : deprecate API. 00132 const int64* RawData() const { 00133 return data_->data(); 00134 } 00135 00136 // Check the status of a given info bit. It will scan the array on demand. 00137 // This is not valid after Release() has been called. 00138 bool HasProperty(Property info); 00139 private: 00140 void AndProperty(Property info, bool value); 00141 void Scan(); 00142 scoped_ptr<std::vector<int64> > data_; 00143 bool scanned_; 00144 uint64 status_; 00145 DISALLOW_COPY_AND_ASSIGN(ConstIntArray); 00146 }; 00147 } // namespace operations_research 00148 00149 #endif // OR_TOOLS_UTIL_CONST_INT_ARRAY_H_