前言 :
在內核中還有另一種同步物件, 這就是信號燈. 和事件對向一樣, 信號燈在使用者模式下和核心模式下是完全統一的, 只不過操作方式不太一樣. 在使用者模式下, 信號燈物件用控制碼代表, 而在核心模式下, 信號燈物件用 KSEMAPHORE 資料結構表示.
核心模式下的信號燈 :
在使用信號燈物件前, 需要對信號燈物件進行初始化. 使用內核函式 KeInitializeSemaphore 對信號燈初始化, 其函式宣告如下 :
參數說明 :
KeReadStateSemaphore 函式可以讀取信號燈當前的計數. 釋放信號燈會增加信號燈計數, 它對應的內核函式是 KeReleaseSemaphore. 程式設計師可以用這個函式指定增量值. 獲得信號燈可以使用 KeWaitXXX 系列函式. 如果能獲得, 就將計數減一, 否則陷入等待, 下面程式碼展示了如何在驅動程式中使用信號燈物件 :
底下是執行結果 :
This is a blog to track what I had learned and share knowledge with all who can take advantage of them
標籤
- [ 英文學習 ]
- [ 計算機概論 ]
- [ 深入雲計算 ]
- [ 雜七雜八 ]
- [ Algorithm in Java ]
- [ Data Structures with Java ]
- [ IR Class ]
- [ Java 文章收集 ]
- [ Java 代碼範本 ]
- [ Java 套件 ]
- [ JVM 應用 ]
- [ LFD Note ]
- [ MangoDB ]
- [ Math CC ]
- [ MongoDB ]
- [ MySQL 小學堂 ]
- [ Python 考題 ]
- [ Python 常見問題 ]
- [ Python 範例代碼 ]
- [心得扎記]
- [網路教學]
- [C 常見考題]
- [C 範例代碼]
- [C/C++ 範例代碼]
- [Intro Alg]
- [Java 代碼範本]
- [Java 套件]
- [Linux 小技巧]
- [Linux 小學堂]
- [Linux 命令]
- [ML In Action]
- [ML]
- [MLP]
- [Postgres]
- [Python 學習筆記]
- [Quick Python]
- [Software Engineering]
- [The python tutorial]
- 工具收集
- 設計模式
- 資料結構
- ActiveMQ In Action
- AI
- Algorithm
- Android
- Ansible
- AWS
- Big Data 研究
- C/C++
- C++
- CCDH
- CI/CD
- Coursera
- Database
- DB
- Design Pattern
- Device Driver Programming
- Docker
- Docker 工具
- Docker Practice
- Eclipse
- English Writing
- ExtJS 3.x
- FP
- Fraud Prevention
- FreeBSD
- GCC
- Git
- Git Pro
- GNU
- Golang
- Gradle
- Groovy
- Hadoop
- Hadoop. Hadoop Ecosystem
- Java
- Java Framework
- Java UI
- JavaIDE
- JavaScript
- Jenkins
- JFreeChart
- Kaggle
- Kali/Metasploit
- Keras
- KVM
- Learn Spark
- LeetCode
- Linux
- Lucene
- Math
- ML
- ML Udemy
- Mockito
- MPI
- Nachos
- Network
- NLP
- node js
- OO
- OpenCL
- OpenMP
- OSC
- OSGi
- Pandas
- Perl
- PostgreSQL
- Py DS
- Python
- Python 自製工具
- Python Std Library
- Python tools
- QEMU
- R
- Real Python
- RIA
- RTC
- Ruby
- Ruby Packages
- Scala
- ScalaIA
- SQLAlchemy
- TensorFlow
- Tools
- UML
- Unix
- Verilog
- Vmware
- Windows 技巧
- wxPython
2011年1月31日 星期一
2011年1月30日 星期日
[ Data Structures with Java ] Section 9.3 : ArrayList Applications
Preface :
This section includes two applications that demonstrate use of the ArrayList class. The method join() concatenates one ArrayList onto the end of another. We will develop join() and use it in a simple program. An ArrayList is useful when the amount of storage required is not know in advance. A particularly interesting example of this is the closet-pair program. Input any number of points in the plane and determine a pair of points that are closest together. We will conclude this section by developing an algorithm that solves the closest-pair problem and using it in an application.
Joining ArrayLists :
The concatenation of one string onto the end of another is the model for joining two ArrayList objects. The method join() takes two lists listA and listB as arguments. A loop scans the elements of listB and copies them onto the end of listA by using the add() method.
The algorithm begins by identifying the size of each ArrayList. A call to the method ensureCapacity() uses the sum of the two sizes as an argument. If ArrayList listA doesn't not have sufficient space to hold the incoming elements from listB, the operation makes a single allocation of new memory that contains the current elements in listA and space for the elements in listB. A simple loop scans the elements in listB and appends them to listA.
The Closest-Pair Problem :
The closest-pair problem requires finding the two closest points in a set of n points in the plane. Assume we specify each point using the standard Cartesian coordinate notation (x, y) and that the distance between two points Pi = (xi, yi) and Pj=(xj, yj) is :
A simple approach to the problem is to evaluate the distance between every pair of distinct points and determine the pair with the smallest distance. We do not want to determine the distance between the same pair of points twice. For instance d(P1, P8) = d(P8, P2). We avoid doing this by only considering pairs(Pi, Pj) where i
Our algorithm is similar to the selection sort algorithm that we discussed in Chapter 4. In a nested loop structure, we find the distance between P0 and the set of points {P1, P2, ... Pn-1}, keeping track of the minimum distance. We continue by computing the distance between P1 and the set of points {P2, P3, ... Pn-1}. In general, pass i of the algorithm computes the distances between Pi and the set of points {Pi+1, ... , Pn-1}. To optimize our algorithm's performance, we do not compute d(Pi, Pj) but compute d(Pi, Pj)^2. The square root takes additional time to compute and general cannot be computed exactly.
The method closestPair() implements the algorithm. Its arguments are ArrayList variables x and y containing the x-coordinate and the y-coordinate of the points. The method returns an array of two integers that contains the indices of the two closet points :
Analysis of the Closest-Pair Algorithm :
Pass 0 of the algorithm compares n-1 points, and pass 1 compares n-2 points. The total number of comparisons is :
(n-1) + (n-2) + ... + 1 = n(n-1)/2
The algorithm has running time O(n^2) ; in other words, it is quadratic. The technique we used is an example of a brute force algorithm. Brute force is a straightforward approach to solving a problem when we have something to search for or when we wish to optimize some property. It is usually based directly on the problem's statement. Normally, the technique enumerates all possible configurations of the input and picks the best of these enumerated configurations. There are important applications of the closest-pair problem. For instance, the algorithm will tell an air traffic controller which pair of n planes is closest during approach to the airport. For this reason, our brute algorithm is too slow. Actually there is a divide-and-conquer O(nlog2(n)) algorithm for the problem but it is too involved to discuss here.
This section includes two applications that demonstrate use of the ArrayList class. The method join() concatenates one ArrayList onto the end of another. We will develop join() and use it in a simple program. An ArrayList is useful when the amount of storage required is not know in advance. A particularly interesting example of this is the closet-pair program. Input any number of points in the plane and determine a pair of points that are closest together. We will conclude this section by developing an algorithm that solves the closest-pair problem and using it in an application.
Joining ArrayLists :
The concatenation of one string onto the end of another is the model for joining two ArrayList objects. The method join() takes two lists listA and listB as arguments. A loop scans the elements of listB and copies them onto the end of listA by using the add() method.
The algorithm begins by identifying the size of each ArrayList. A call to the method ensureCapacity() uses the sum of the two sizes as an argument. If ArrayList listA doesn't not have sufficient space to hold the incoming elements from listB, the operation makes a single allocation of new memory that contains the current elements in listA and space for the elements in listB. A simple loop scans the elements in listB and appends them to listA.
- 函式 join() 代碼 :
- public static
void join(ArrayList listA, ArrayList listB) { - int sizeA = listA.size(), sizeB = listB.size();
- listA.ensureCapacity(sizeA+sizeB);
- for(T t:listB)
- listA.add(t);
- }
The Closest-Pair Problem :
The closest-pair problem requires finding the two closest points in a set of n points in the plane. Assume we specify each point using the standard Cartesian coordinate notation (x, y) and that the distance between two points Pi = (xi, yi) and Pj=(xj, yj) is :
A simple approach to the problem is to evaluate the distance between every pair of distinct points and determine the pair with the smallest distance. We do not want to determine the distance between the same pair of points twice. For instance d(P1, P8) = d(P8, P2). We avoid doing this by only considering pairs(Pi, Pj) where i
Our algorithm is similar to the selection sort algorithm that we discussed in Chapter 4. In a nested loop structure, we find the distance between P0 and the set of points {P1, P2, ... Pn-1}, keeping track of the minimum distance. We continue by computing the distance between P1 and the set of points {P2, P3, ... Pn-1}. In general, pass i of the algorithm computes the distances between Pi and the set of points {Pi+1, ... , Pn-1}. To optimize our algorithm's performance, we do not compute d(Pi, Pj) but compute d(Pi, Pj)^2. The square root takes additional time to compute and general cannot be computed exactly.
The method closestPair() implements the algorithm. Its arguments are ArrayList variables x and y containing the x-coordinate and the y-coordinate of the points. The method returns an array of two integers that contains the indices of the two closet points :
- 函式 closestPair() 代碼 :
- protected static double sqr(double x) {
- return x*x;
- }
- public static int[] closestPair(ArrayList
x, ArrayList y) { - int n = x.size(), p1=-1, p2=-1;
- int[] closest = new int[2];
- double minumDist = java.lang.Double.MAX_VALUE,tempDist=0;
- double x1,x2,y1,y2;
- for(int i=0; i<(n-1); i++) {
- x1 = x.get(i);
- y1 = y.get(i);
- for(int j=(i+1); j
- x2 = x.get(j);
- y2 = y.get(j);
- //System.out.print("x1="+x1+",y1="+y1+"; x2="+x2+",y2="+y2);
- tempDist = sqr((x1-x2))+sqr((y1-y2));
- //System.out.println(" >> Check "+tempDist+"...(MinNow="+minumDist+")");
- if(tempDist
- //System.out.println("\tGot P"+i+" and P"+j);
- p1=i; p2=j;
- minumDist = tempDist;
- }
- }
- }
- closest[0] = p1;
- closest[1] = p2;
- return closest;
- }
- public static void main(String args[]) {
- ArrayList
x = new ArrayList (), y = new ArrayList(); - double[] xPt = {-1, -1.45, -1, -1, 0.75, 1, 0.8, 1.65, -1.2, 3, 2, 2.7, 1.35, 0.5};
- double[] yPt = {1, 1.3, 0, 3, -1.75, -1, 0.7, 1, -1.3, 0, 2, 1.3, 1.1, 2.45};
- for(int i=0; i
- x.add(xPt[i]);
- y.add(yPt[i]);
- }
- int[] closestPoints = closestPair(x,y);
- System.out.printf("The closest points are (%.2f, %.2f) and (%.2f, %.2f)\n",
- xPt[closestPoints[0]], yPt[closestPoints[0]],
- xPt[closestPoints[1]], yPt[closestPoints[1]]);
- System.out.println("Distance="+
- Math.sqrt(sqr(
- xPt[closestPoints[0]]-xPt[closestPoints[1]])+sqr(yPt[closestPoints[0]]-yPt[closestPoints[1]])));
- }
Analysis of the Closest-Pair Algorithm :
Pass 0 of the algorithm compares n-1 points, and pass 1 compares n-2 points. The total number of comparisons is :
(n-1) + (n-2) + ... + 1 = n(n-1)/2
The algorithm has running time O(n^2) ; in other words, it is quadratic. The technique we used is an example of a brute force algorithm. Brute force is a straightforward approach to solving a problem when we have something to search for or when we wish to optimize some property. It is usually based directly on the problem's statement. Normally, the technique enumerates all possible configurations of the input and picks the best of these enumerated configurations. There are important applications of the closest-pair problem. For instance, the algorithm will tell an air traffic controller which pair of n planes is closest during approach to the airport. For this reason, our brute algorithm is too slow. Actually there is a divide-and-conquer O(nlog2(n)) algorithm for the problem but it is too involved to discuss here.
訂閱:
文章 (Atom)
[Git 常見問題] error: The following untracked working tree files would be overwritten by merge
Source From Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 # git clean -d -fx 方案2: 今天在服务器上 gi...
-
前言 : 為什麼程序管理這麼重要呢?這是因為: * 首先,本章一開始就談到的,我們在操作系統時的各項工作其實都是經過某個 PID 來達成的 (包括你的 bash 環境), 因此,能不能進行某項工作,就與該程序的權限有關了。 * 再來,如果您的 Linux 系統是個...
-
屬性 : 系統相關 - 檔案與目錄 語法 : du [參數] [檔案] 參數 | 功能 -a | 顯示目錄中個別檔案的大小 -b | 以bytes為單位顯示 -c | 顯示個別檔案大小與總和 -D | 顯示符號鏈結的來源檔大小 -h | Hum...
-
來源自 這裡 說明 : split 是 Perl 中非常有用的函式之一,它可以將一個字串分割並將之置於陣列中。若無特別的指定,該函式亦使用 RE 與 $_ 變數 語法 : * split /PATTERN/,EXPR,LIMIT * split /...