use of java.util.ArrayDeque in project suite by stupidsing.
the class InstructionExtractor method extractInstructions.
private void extractInstructions(Node snippet, List<List<Node>> rsList) {
Deque<Node> deque = new ArrayDeque<>();
deque.add(snippet);
Tree tree;
Node value;
while (!deque.isEmpty()) if ((tree = Tree.decompose(deque.pop(), TermOp.AND___)) != null) {
IdentityKey<Node> key = IdentityKey.of(tree);
Integer ip = ipByLabelId.get(key);
if (ip == null) {
ipByLabelId.put(key, ip = rsList.size());
List<Node> rs = tupleToList(tree.getLeft());
if (rs.get(0) == FRAME)
if ((value = label(rs.get(1))) != null) {
rsList.add(List.of(Atom.of("FRAME-BEGIN")));
extractInstructions(value, rsList);
rsList.add(List.of(Atom.of("FRAME-END")));
} else
Fail.t("bad frame definition");
else {
rsList.add(rs);
for (Node op : List_.right(rs, 1)) if ((value = label(op)) != null)
deque.push(value);
deque.push(tree.getRight());
}
} else
rsList.add(List.of(Atom.of("JUMP"), Int.of(ip)));
}
}
use of java.util.ArrayDeque in project suite by stupidsing.
the class Grapher method bind.
public static boolean bind(Node n0, Node n1, Trail trail) {
Map<IdentityKey<Node>, Integer> mapn0 = new HashMap<>();
Map<IdentityKey<Node>, Integer> mapn1 = new HashMap<>();
Grapher g0 = new Grapher();
Grapher g1 = new Grapher();
g0.id = g0.graph_(mapn0, n0);
g1.id = g1.graph_(mapn1, n1);
IntObjMap<IdentityKey<Node>> mapi0 = new IntObjMap<>();
IntObjMap<IdentityKey<Node>> mapi1 = new IntObjMap<>();
for (Entry<IdentityKey<Node>, Integer> e : mapn0.entrySet()) mapi0.put(e.getValue(), e.getKey());
for (Entry<IdentityKey<Node>, Integer> e : mapn1.entrySet()) mapi1.put(e.getValue(), e.getKey());
Set<IntIntPair> set = new HashSet<>();
Deque<IntIntPair> deque = new ArrayDeque<>();
deque.add(IntIntPair.of(g0.id, g1.id));
IntIntPair pair;
while ((pair = deque.pollLast()) != null) if (set.add(pair)) {
GN gn0 = g0.gns.get(pair.t0);
GN gn1 = g1.gns.get(pair.t1);
if (//
gn0.type == ReadType.TERM && //
gn0.terminal instanceof Reference && Binder.bind(gn0.terminal, mapi1.get(pair.t1).key, trail))
;
else if (//
gn1.type == ReadType.TERM && //
gn1.terminal instanceof Reference && Binder.bind(gn1.terminal, mapi0.get(pair.t0).key, trail))
;
else if (gn0.type == gn1.type && Objects.equals(gn0.terminal, gn1.terminal) && gn0.op == gn1.op) {
List<IntIntPair> children0 = gn0.children;
List<IntIntPair> children1 = gn1.children;
int size0 = children0.size();
int size1 = children1.size();
if (size0 == size1)
for (int i = 0; i < size0; i++) {
IntIntPair p0 = children0.get(i);
IntIntPair p1 = children1.get(i);
deque.addLast(IntIntPair.of(p0.t0, p1.t0));
deque.addLast(IntIntPair.of(p0.t1, p1.t1));
}
else
return false;
} else
return false;
}
return true;
}
use of java.util.ArrayDeque in project hive by apache.
the class SemanticAnalyzer method processPositionAlias.
// Process the position alias in GROUPBY and ORDERBY
public void processPositionAlias(ASTNode ast) throws SemanticException {
boolean isBothByPos = HiveConf.getBoolVar(conf, ConfVars.HIVE_GROUPBY_ORDERBY_POSITION_ALIAS);
boolean isGbyByPos = isBothByPos || HiveConf.getBoolVar(conf, ConfVars.HIVE_GROUPBY_POSITION_ALIAS);
boolean isObyByPos = isBothByPos || HiveConf.getBoolVar(conf, ConfVars.HIVE_ORDERBY_POSITION_ALIAS);
Deque<ASTNode> stack = new ArrayDeque<ASTNode>();
stack.push(ast);
while (!stack.isEmpty()) {
ASTNode next = stack.pop();
if (next.getChildCount() == 0) {
continue;
}
boolean isAllCol;
ASTNode selectNode = null;
ASTNode groupbyNode = null;
ASTNode orderbyNode = null;
// get node type
int child_count = next.getChildCount();
for (int child_pos = 0; child_pos < child_count; ++child_pos) {
ASTNode node = (ASTNode) next.getChild(child_pos);
int type = node.getToken().getType();
if (type == HiveParser.TOK_SELECT || type == HiveParser.TOK_SELECTDI) {
selectNode = node;
} else if (type == HiveParser.TOK_GROUPBY) {
groupbyNode = node;
} else if (type == HiveParser.TOK_ORDERBY) {
orderbyNode = node;
}
}
if (selectNode != null) {
int selectExpCnt = selectNode.getChildCount();
// replace each of the position alias in GROUPBY with the actual column name
if (groupbyNode != null) {
for (int child_pos = 0; child_pos < groupbyNode.getChildCount(); ++child_pos) {
ASTNode node = (ASTNode) groupbyNode.getChild(child_pos);
if (node.getToken().getType() == HiveParser.Number) {
if (isGbyByPos) {
int pos = Integer.parseInt(node.getText());
if (pos > 0 && pos <= selectExpCnt) {
groupbyNode.setChild(child_pos, selectNode.getChild(pos - 1).getChild(0));
} else {
throw new SemanticException(ErrorMsg.INVALID_POSITION_ALIAS_IN_GROUPBY.getMsg("Position alias: " + pos + " does not exist\n" + "The Select List is indexed from 1 to " + selectExpCnt));
}
} else {
warn("Using constant number " + node.getText() + " in group by. If you try to use position alias when hive.groupby.position.alias is false, the position alias will be ignored.");
}
}
}
}
// if cbo is enabled, orderby position will be processed in genPlan
if (!HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_CBO_ENABLED) && orderbyNode != null) {
isAllCol = false;
for (int child_pos = 0; child_pos < selectNode.getChildCount(); ++child_pos) {
ASTNode node = (ASTNode) selectNode.getChild(child_pos).getChild(0);
if (node != null && node.getToken().getType() == HiveParser.TOK_ALLCOLREF) {
isAllCol = true;
}
}
for (int child_pos = 0; child_pos < orderbyNode.getChildCount(); ++child_pos) {
ASTNode colNode = null;
ASTNode node = null;
if (orderbyNode.getChildCount() > 0) {
colNode = (ASTNode) orderbyNode.getChild(child_pos).getChild(0);
if (colNode.getChildCount() > 0) {
node = (ASTNode) colNode.getChild(0);
}
}
if (node != null && node.getToken().getType() == HiveParser.Number) {
if (isObyByPos) {
if (!isAllCol) {
int pos = Integer.parseInt(node.getText());
if (pos > 0 && pos <= selectExpCnt && selectNode.getChild(pos - 1).getChildCount() > 0) {
colNode.setChild(0, selectNode.getChild(pos - 1).getChild(0));
} else {
throw new SemanticException(ErrorMsg.INVALID_POSITION_ALIAS_IN_ORDERBY.getMsg("Position alias: " + pos + " does not exist\n" + "The Select List is indexed from 1 to " + selectExpCnt));
}
} else {
throw new SemanticException(ErrorMsg.NO_SUPPORTED_ORDERBY_ALLCOLREF_POS.getMsg());
}
} else {
// if not using position alias and it is a number.
warn("Using constant number " + node.getText() + " in order by. If you try to use position alias when hive.orderby.position.alias is false, the position alias will be ignored.");
}
}
}
}
}
for (int i = next.getChildren().size() - 1; i >= 0; i--) {
stack.push((ASTNode) next.getChildren().get(i));
}
}
}
use of java.util.ArrayDeque in project carbon-apimgt by wso2.
the class MySQLScriptValidationTestCase method testCharacterSetUTF8.
@Test(description = "Test if UTF8 character set has been applied to all tables in script")
public void testCharacterSetUTF8() throws Exception {
try (InputStream is = new FileInputStream(dbscriptPath);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF8"))) {
final String createTableSyntax = "CREATE TABLE";
final char openingBrace = '(';
final String utf8CharSet = "CHARACTER SET utf8 COLLATE utf8_general_ci";
final String endOfStatement = ";";
Deque<String> tables = new ArrayDeque<>();
Map<String, Boolean> tableCharsetUTF8 = new HashMap<>();
boolean isCharsetDetected = false;
boolean isTableDetected = false;
String line;
while ((line = reader.readLine()) != null) {
if (line.contains(createTableSyntax)) {
int startOfCreateTable = line.indexOf(createTableSyntax);
int startOfOpeningBrace = line.indexOf(openingBrace);
tables.add(line.substring(startOfCreateTable + createTableSyntax.length(), startOfOpeningBrace).trim());
isTableDetected = true;
}
if (line.contains(utf8CharSet) && isTableDetected) {
isCharsetDetected = true;
}
if (line.contains(endOfStatement)) {
if (isTableDetected) {
tableCharsetUTF8.put(tables.getLast(), isCharsetDetected);
}
isCharsetDetected = false;
isTableDetected = false;
}
}
for (Map.Entry<String, Boolean> entry : tableCharsetUTF8.entrySet()) {
Assert.assertTrue(entry.getValue(), "Table " + entry.getKey() + " has been defined without " + utf8CharSet + " in mysql db script");
}
}
}
use of java.util.ArrayDeque in project ignite by apache.
the class GridCircularQueueTest method testQueue.
/**
*/
public void testQueue() {
GridCacheQueryManager.CircularQueue<Integer> q = new GridCacheQueryManager.CircularQueue<>(4);
ArrayDeque<Integer> d = new ArrayDeque<>();
for (int i = 0; i < 10; i++) {
q.add(i);
d.add(i);
}
check(q, d);
q.remove(4);
remove(d, 4);
check(q, d);
for (int i = 100; i < 110; i++) {
q.add(i);
d.add(i);
}
check(q, d);
int size = q.size();
q.remove(size);
remove(d, size);
check(q, d);
assertEquals(0, q.size());
GridRandom rnd = new GridRandom();
for (int i = 0; i < 15000; i++) {
switch(rnd.nextInt(2)) {
case 1:
if (q.size() > 0) {
int cnt = 1;
if (q.size() > 1)
cnt += rnd.nextInt(q.size() - 1);
q.remove(cnt);
remove(d, cnt);
break;
}
case 0:
int cnt = rnd.nextInt(50);
for (int j = 0; j < cnt; j++) {
int x = rnd.nextInt();
q.add(x);
d.add(x);
}
break;
}
check(q, d);
}
}
Aggregations