Beispiel #1
0
vector<int> Solution::twoSum(vector<int> &numbers, int target){
	vector<ValueIndex> arr;
	int n = numbers.size();
	for (int i = 0; i < n; i++)
		arr.push_back(ValueIndex(numbers[i], i));
	sort(arr.begin(), arr.end(), compare);
	int i = 0, j = arr.size() - 1;
	while (i < j) {
		int sum = arr[i].value + arr[j].value;
		if (sum == target){
			int a = arr[i].index + 1;
			int b = arr[j].index + 1;
			if (a>b) swap(a, b);
			return vector<int>({ a, b });
		}
		if (sum < target)
			i++;
		else j--;
	}
	return vector<int>(-1,-1);
}
Beispiel #2
0
 const ValueType& operator()(const glm::ivec3& index) const {
   return values_[ValueIndex(index)];
 }
Beispiel #3
0
 const ValueType& operator()(int i, int j, int k) const {
   return values_[ValueIndex(i, j, k)];
 }
Beispiel #4
0
 std::unique_ptr<RamValue> translateValue(const AstArgument& arg, const ValueIndex& index = ValueIndex()) {
     return translateValue(&arg, index);
 }
Beispiel #5
0
    std::unique_ptr<RamValue> translateValue(const AstArgument* arg, const ValueIndex& index = ValueIndex()) {

        std::unique_ptr<RamValue> val;
        if (!arg) return val;

        if (const AstVariable *var = dynamic_cast<const AstVariable *>(arg)) {
            ASSERT(index.isDefined(*var) && "variable not grounded");
            const Location& loc = index.getDefinitionPoint(*var);
            val = std::unique_ptr<RamValue>(new RamElementAccess(loc.level, loc.component, loc.name));
        } else if (dynamic_cast<const AstUnnamedVariable*>(arg)) {
            return nullptr;         // utilized to identify _ values
        } else if (const AstConstant *c = dynamic_cast<const AstConstant *>(arg)) {
            val = std::unique_ptr<RamValue>(new RamNumber(c->getIndex()));
        } else if (const AstUnaryFunctor *uf = dynamic_cast<const AstUnaryFunctor *>(arg)) {
            switch(uf->getFunction()) {
            case AstUnaryFunctor::ORDINAL:
                val = std::unique_ptr<RamValue>(new RamOrd(translateValue(uf->getOperand(), index)));
                break;
            case AstUnaryFunctor::NEGATION:
                val = std::unique_ptr<RamValue>(new RamNegation(translateValue(uf->getOperand(), index)));
                break;
            default:
                ASSERT(false && "unknown unary function");
            }
        } else if (const AstBinaryFunctor *bf = dynamic_cast<const AstBinaryFunctor *>(arg)) {
            val = std::unique_ptr<RamValue>(new RamBinaryOperator(bf->getFunction(),
                    translateValue(bf->getLHS(), index),
                    translateValue(bf->getRHS(), index)));
        } else if (dynamic_cast<const AstCounter *>(arg)){
            val = std::unique_ptr<RamValue>(new RamAutoIncrement());
        } else if (const AstRecordInit* init = dynamic_cast<const AstRecordInit*>(arg)) {
            std::vector<std::unique_ptr<RamValue>> values;
            for(const auto& cur : init->getArguments()) {
                values.push_back(translateValue(cur, index));
            }
            val = std::unique_ptr<RamValue>(new RamPack(std::move(values)));
        } else if (const AstAggregator* agg = dynamic_cast<const AstAggregator*>(arg)) {
            // here we look up the location the aggregation result gets bound
            auto loc = index.getAggregatorLocation(*agg);
            val = std::unique_ptr<RamValue>(new RamElementAccess(loc.level, loc.component, loc.name));
        } else {
            std::cout << "Unsupported node type of " << arg << ": " << typeid(*arg).name() << "\n";
            ASSERT(false && "unknown AST node type not permissible");
        }

        return val;

    }