自定义xml配置文件之dtd文件校验
用了很多第三方库,也看了些源码,总是想如果自己写一个类似的库,读取xml配置文件(properties配置文件比较简单) 该如何给配置文件添加头,添加校验,因为xml配置文件相对于properties配置文件结构更加清晰。最近刚好在看spring 1.2.9版本的源码,就来总结一下如何定义一个dtd文件用于约束xml文件的配置。这里之所以选择这个非常久远的低版本,主要是因为代码少,便于阅读。虽然版本很低,但是其核心设计,核心类都没变,比如ApplicationContext ,各种 ApplicationEvent ,各种Listenser及解析XML的入口BeanDefinition, BeanDefinitionParser, BeanDefinitionReader等。这里记录一下spring各种版本的获取地址:Spring各种版本源码官方仓库 。找了好久。废话有点多,下面进入正题。
关于dtd文件的结构
随便找一个第三方库的dtd文件,我这里直接把spring beans的模块中的dtd文件拿出来:
<?xml version="1.0" encoding="UTF-8"?> <!--
Spring XML Beans DTD, version 1.2
Authors: Rod Johnson, Juergen Hoeller, Alef Arendsen, Colin Sampaleanu, Rob Harrop This defines a simple and consistent way of creating a namespace
of JavaBeans objects, managed by a Spring BeanFactory, read by
XmlBeanDefinitionReader (with DefaultXmlBeanDefinitionParser). This document type is used by most Spring functionality, including
web application contexts, which are based on bean factories. Each "bean" element in this document defines a JavaBean.
Typically the bean class is specified, along with JavaBean properties
and/or constructor arguments. Bean instances can be "singletons" (shared instances) or "prototypes"
(independent instances). Further scopes are supposed to be built on top
of the core BeanFactory infrastructure and are therefore not part of it. References among beans are supported, that is, setting a JavaBean property
or a constructor argument to refer to another bean in the same factory
(or an ancestor factory). As alternative to bean references, "inner bean definitions" can be used.
Singleton flags of such inner bean definitions are effectively ignored:
Inner beans are typically anonymous prototypes. There is also support for lists, sets, maps, and java.util.Properties
as bean property types or constructor argument types. As the format is simple, a DTD is sufficient, and there's no need
for a schema at this point. XML documents that conform to this DTD should declare the following doctype: <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
--> <!--
The document root. A document can contain bean definitions only,
imports only, or a mixture of both (typically with imports first).
-->
<!ELEMENT beans (
description?,
(import | alias | bean)*
)> <!--
Default values for all bean definitions. Can be overridden at
the "bean" level. See those attribute definitions for details.
-->
<!ATTLIST beans default-lazy-init (true | false) "false">
<!ATTLIST beans default-autowire (no | byName | byType | constructor | autodetect) "no">
<!ATTLIST beans default-dependency-check (none | objects | simple | all) "none">
<!ATTLIST beans default-init-method CDATA #IMPLIED>
<!ATTLIST beans default-destroy-method CDATA #IMPLIED> <!--
Element containing informative text describing the purpose of the enclosing
element. Always optional.
Used primarily for user documentation of XML bean definition documents.
-->
<!ELEMENT description (#PCDATA)> <!--
Specifies an XML bean definition resource to import.
-->
<!ELEMENT import EMPTY> <!--
The relative resource location of the XML bean definition file to import,
for example "myImport.xml" or "includes/myImport.xml" or "../myImport.xml".
-->
<!ATTLIST import resource CDATA #REQUIRED> <!--
Defines an alias for a bean, which can reside in a different definition file.
-->
<!ELEMENT alias EMPTY> <!--
The name of the bean to define an alias for.
-->
<!ATTLIST alias name CDATA #REQUIRED> <!--
The alias name to define for the bean.
-->
<!ATTLIST alias alias CDATA #REQUIRED> <!--
Defines a single (usually named) bean. A bean definition may contain nested tags for constructor arguments,
property values, lookup methods, and replaced methods. Mixing constructor
injection and setter injection on the same bean is explicitly supported.
-->
<!ELEMENT bean (
description?,
(constructor-arg | property | lookup-method | replaced-method)*
)> <!--
Beans can be identified by an id, to enable reference checking. There are constraints on a valid XML id: if you want to reference your bean
in Java code using a name that's illegal as an XML id, use the optional
"name" attribute. If neither is given, the bean class name is used as id
(with an appended counter like "#2" if there is already a bean with that name).
-->
<!ATTLIST bean id ID #IMPLIED> <!--
Optional. Can be used to create one or more aliases illegal in an id.
Multiple aliases can be separated by any number of spaces, commas, or
semi-colons (or indeed any mixture of the three).
-->
<!ATTLIST bean name CDATA #IMPLIED> <!--
Each bean definition must specify the fully qualified name of the class,
except if it pure serves as parent for child bean definitions.
-->
<!ATTLIST bean class CDATA #IMPLIED> <!--
Optionally specify a parent bean definition. Will use the bean class of the parent if none specified, but can
also override it. In the latter case, the child bean class must be
compatible with the parent, i.e. accept the parent's property values
and constructor argument values, if any. A child bean definition will inherit constructor argument values,
property values and method overrides from the parent, with the option
to add new values. If init method, destroy method, factory bean and/or factory
method are specified, they will override the corresponding parent settings. The remaining settings will always be taken from the child definition:
depends on, autowire mode, dependency check, singleton, lazy init.
-->
<!ATTLIST bean parent CDATA #IMPLIED> <!--
Is this bean "abstract", i.e. not meant to be instantiated itself but
rather just serving as parent for concrete child bean definitions.
Default is "false". Specify "true" to tell the bean factory to not try to
instantiate that particular bean in any case. Note: This attribute will not be inherited by child bean definitions.
Hence, it needs to be specified per concrete bean definition.
-->
<!ATTLIST bean abstract (true | false) #IMPLIED> <!--
Is this bean a "singleton" (one shared instance, which will
be returned by all calls to getBean() with the id),
or a "prototype" (independent instance resulting from each call to
getBean(). Default is singleton. Singletons are most commonly used, and are ideal for multi-threaded
service objects. Note: This attribute will not be inherited by child bean definitions.
Hence, it needs to be specified per concrete bean definition.
-->
<!ATTLIST bean singleton (true | false) #IMPLIED> <!--
If this bean should be lazily initialized.
If false, it will get instantiated on startup by bean factories
that perform eager initialization of singletons. Note: This attribute will not be inherited by child bean definitions.
Hence, it needs to be specified per concrete bean definition.
-->
<!ATTLIST bean lazy-init (true | false | default) "default"> <!--
Optional attribute controlling whether to "autowire" bean properties.
This is an automagical process in which bean references don't need to be coded
explicitly in the XML bean definition file, but Spring works out dependencies. There are 5 modes: 1. "no"
The traditional Spring default. No automagical wiring. Bean references
must be defined in the XML file via the <ref> element. We recommend this
in most cases as it makes documentation more explicit. 2. "byName"
Autowiring by property name. If a bean of class Cat exposes a dog property,
Spring will try to set this to the value of the bean "dog" in the current factory.
If there is no matching bean by name, nothing special happens;
use dependency-check="objects" to raise an error in that case. 3. "byType"
Autowiring if there is exactly one bean of the property type in the bean factory.
If there is more than one, a fatal error is raised, and you can't use byType
autowiring for that bean. If there is none, nothing special happens;
use dependency-check="objects" to raise an error in that case. 4. "constructor"
Analogous to "byType" for constructor arguments. If there isn't exactly one bean
of the constructor argument type in the bean factory, a fatal error is raised. 5. "autodetect"
Chooses "constructor" or "byType" through introspection of the bean class.
If a default constructor is found, "byType" gets applied. The latter two are similar to PicoContainer and make bean factories simple to
configure for small namespaces, but doesn't work as well as standard Spring
behaviour for bigger applications. Note that explicit dependencies, i.e. "property" and "constructor-arg" elements,
always override autowiring. Autowire behavior can be combined with dependency
checking, which will be performed after all autowiring has been completed. Note: This attribute will not be inherited by child bean definitions.
Hence, it needs to be specified per concrete bean definition.
-->
<!ATTLIST bean autowire (no | byName | byType | constructor | autodetect | default) "default"> <!--
Optional attribute controlling whether to check whether all this
beans dependencies, expressed in its properties, are satisfied.
Default is no dependency checking. "simple" type dependency checking includes primitives and String
"object" includes collaborators (other beans in the factory)
"all" includes both types of dependency checking Note: This attribute will not be inherited by child bean definitions.
Hence, it needs to be specified per concrete bean definition.
-->
<!ATTLIST bean dependency-check (none | objects | simple | all | default) "default"> <!--
The names of the beans that this bean depends on being initialized.
The bean factory will guarantee that these beans get initialized before. Note that dependencies are normally expressed through bean properties or
constructor arguments. This property should just be necessary for other kinds
of dependencies like statics (*ugh*) or database preparation on startup. Note: This attribute will not be inherited by child bean definitions.
Hence, it needs to be specified per concrete bean definition.
-->
<!ATTLIST bean depends-on CDATA #IMPLIED> <!--
Optional attribute for the name of the custom initialization method
to invoke after setting bean properties. The method must have no arguments,
but may throw any exception.
-->
<!ATTLIST bean init-method CDATA #IMPLIED> <!--
Optional attribute for the name of the custom destroy method to invoke
on bean factory shutdown. The method must have no arguments,
but may throw any exception. Note: Only invoked on singleton beans!
-->
<!ATTLIST bean destroy-method CDATA #IMPLIED> <!--
Optional attribute specifying the name of a factory method to use to
create this object. Use constructor-arg elements to specify arguments
to the factory method, if it takes arguments. Autowiring does not apply
to factory methods. If the "class" attribute is present, the factory method will be a static
method on the class specified by the "class" attribute on this bean
definition. Often this will be the same class as that of the constructed
object - for example, when the factory method is used as an alternative
to a constructor. However, it may be on a different class. In that case,
the created object will *not* be of the class specified in the "class"
attribute. This is analogous to FactoryBean behavior. If the "factory-bean" attribute is present, the "class" attribute is not
used, and the factory method will be an instance method on the object
returned from a getBean call with the specified bean name. The factory
bean may be defined as a singleton or a prototype. The factory method can have any number of arguments. Autowiring is not
supported. Use indexed constructor-arg elements in conjunction with the
factory-method attribute. Setter Injection can be used in conjunction with a factory method.
Method Injection cannot, as the factory method returns an instance,
which will be used when the container creates the bean.
-->
<!ATTLIST bean factory-method CDATA #IMPLIED> <!--
Alternative to class attribute for factory-method usage.
If this is specified, no class attribute should be used.
This should be set to the name of a bean in the current or
ancestor factories that contains the relevant factory method.
This allows the factory itself to be configured using Dependency
Injection, and an instance (rather than static) method to be used.
-->
<!ATTLIST bean factory-bean CDATA #IMPLIED> <!--
Bean definitions can specify zero or more constructor arguments.
This is an alternative to "autowire constructor".
Arguments correspond to either a specific index of the constructor argument
list or are supposed to be matched generically by type. Note: A single generic argument value will just be used once, rather than
potentially matched multiple times (as of Spring 1.1). constructor-arg elements are also used in conjunction with the factory-method
element to construct beans using static or instance factory methods.
-->
<!ELEMENT constructor-arg (
description?,
(bean | ref | idref | value | null | list | set | map | props)?
)> <!--
The constructor-arg tag can have an optional index attribute,
to specify the exact index in the constructor argument list. Only needed
to avoid ambiguities, e.g. in case of 2 arguments of the same type.
-->
<!ATTLIST constructor-arg index CDATA #IMPLIED> <!--
The constructor-arg tag can have an optional type attribute,
to specify the exact type of the constructor argument. Only needed
to avoid ambiguities, e.g. in case of 2 single argument constructors
that can both be converted from a String.
-->
<!ATTLIST constructor-arg type CDATA #IMPLIED> <!--
A short-cut alternative to a child element "ref bean=".
-->
<!ATTLIST constructor-arg ref CDATA #IMPLIED> <!--
A short-cut alternative to a child element "value".
-->
<!ATTLIST constructor-arg value CDATA #IMPLIED> <!--
Bean definitions can have zero or more properties.
Property elements correspond to JavaBean setter methods exposed
by the bean classes. Spring supports primitives, references to other
beans in the same or related factories, lists, maps and properties.
-->
<!ELEMENT property (
description?,
(bean | ref | idref | value | null | list | set | map | props)?
)> <!--
The property name attribute is the name of the JavaBean property.
This follows JavaBean conventions: a name of "age" would correspond
to setAge()/optional getAge() methods.
-->
<!ATTLIST property name CDATA #REQUIRED> <!--
A short-cut alternative to a child element "ref bean=".
-->
<!ATTLIST property ref CDATA #IMPLIED> <!--
A short-cut alternative to a child element "value".
-->
<!ATTLIST property value CDATA #IMPLIED> <!--
A lookup method causes the IoC container to override the given method and return
the bean with the name given in the bean attribute. This is a form of Method Injection.
It's particularly useful as an alternative to implementing the BeanFactoryAware
interface, in order to be able to make getBean() calls for non-singleton instances
at runtime. In this case, Method Injection is a less invasive alternative.
-->
<!ELEMENT lookup-method EMPTY> <!--
Name of a lookup method. This method should take no arguments.
-->
<!ATTLIST lookup-method name CDATA #IMPLIED> <!--
Name of the bean in the current or ancestor factories that the lookup method
should resolve to. Often this bean will be a prototype, in which case the
lookup method will return a distinct instance on every invocation. This
is useful for single-threaded objects.
-->
<!ATTLIST lookup-method bean CDATA #IMPLIED> <!--
Similar to the lookup method mechanism, the replaced-method element is used to control
IoC container method overriding: Method Injection. This mechanism allows the overriding
of a method with arbitrary code.
-->
<!ELEMENT replaced-method (
(arg-type)*
)> <!--
Name of the method whose implementation should be replaced by the IoC container.
If this method is not overloaded, there's no need to use arg-type subelements.
If this method is overloaded, arg-type subelements must be used for all
override definitions for the method.
-->
<!ATTLIST replaced-method name CDATA #IMPLIED> <!--
Bean name of an implementation of the MethodReplacer interface
in the current or ancestor factories. This may be a singleton or prototype
bean. If it's a prototype, a new instance will be used for each method replacement.
Singleton usage is the norm.
-->
<!ATTLIST replaced-method replacer CDATA #IMPLIED> <!--
Subelement of replaced-method identifying an argument for a replaced method
in the event of method overloading.
-->
<!ELEMENT arg-type (#PCDATA)> <!--
Specification of the type of an overloaded method argument as a String.
For convenience, this may be a substring of the FQN. E.g. all the
following would match "java.lang.String":
- java.lang.String
- String
- Str As the number of arguments will be checked also, this convenience can often
be used to save typing.
-->
<!ATTLIST arg-type match CDATA #IMPLIED> <!--
Defines a reference to another bean in this factory or an external
factory (parent or included factory).
-->
<!ELEMENT ref EMPTY> <!--
References must specify a name of the target bean.
The "bean" attribute can reference any name from any bean in the context,
to be checked at runtime.
Local references, using the "local" attribute, have to use bean ids;
they can be checked by this DTD, thus should be preferred for references
within the same bean factory XML file.
-->
<!ATTLIST ref bean CDATA #IMPLIED>
<!ATTLIST ref local IDREF #IMPLIED>
<!ATTLIST ref parent CDATA #IMPLIED> <!--
Defines a string property value, which must also be the id of another
bean in this factory or an external factory (parent or included factory).
While a regular 'value' element could instead be used for the same effect,
using idref in this case allows validation of local bean ids by the xml
parser, and name completion by helper tools.
-->
<!ELEMENT idref EMPTY> <!--
ID refs must specify a name of the target bean.
The "bean" attribute can reference any name from any bean in the context,
potentially to be checked at runtime by bean factory implementations.
Local references, using the "local" attribute, have to use bean ids;
they can be checked by this DTD, thus should be preferred for references
within the same bean factory XML file.
-->
<!ATTLIST idref bean CDATA #IMPLIED>
<!ATTLIST idref local IDREF #IMPLIED> <!--
Contains a string representation of a property value.
The property may be a string, or may be converted to the
required type using the JavaBeans PropertyEditor
machinery. This makes it possible for application developers
to write custom PropertyEditor implementations that can
convert strings to objects. Note that this is recommended for simple objects only.
Configure more complex objects by populating JavaBean
properties with references to other beans.
-->
<!ELEMENT value (#PCDATA)> <!--
The value tag can have an optional type attribute, to specify the
exact type that the value should be converted to. Only needed
if the type of the target property or constructor argument is
too generic: for example, in case of a collection element.
-->
<!ATTLIST value type CDATA #IMPLIED> <!--
Denotes a Java null value. Necessary because an empty "value" tag
will resolve to an empty String, which will not be resolved to a
null value unless a special PropertyEditor does so.
-->
<!ELEMENT null (#PCDATA)> <!--
A list can contain multiple inner bean, ref, collection, or value elements.
Java lists are untyped, pending generics support in Java 1.5,
although references will be strongly typed.
A list can also map to an array type. The necessary conversion
is automatically performed by the BeanFactory.
-->
<!ELEMENT list (
(bean | ref | idref | value | null | list | set | map | props)*
)> <!--
A set can contain multiple inner bean, ref, collection, or value elements.
Java sets are untyped, pending generics support in Java 1.5,
although references will be strongly typed.
-->
<!ELEMENT set (
(bean | ref | idref | value | null | list | set | map | props)*
)> <!--
A Spring map is a mapping from a string key to object.
Maps may be empty.
-->
<!ELEMENT map (
(entry)*
)> <!--
A map entry can be an inner bean, ref, value, or collection.
The key of the entry is given by the "key" attribute or child element.
-->
<!ELEMENT entry (
key?,
(bean | ref | idref | value | null | list | set | map | props)?
)> <!--
Each map element must specify its key as attribute or as child element.
A key attribute is always a String value.
-->
<!ATTLIST entry key CDATA #IMPLIED> <!--
A short-cut alternative to a "key" element with a "ref bean=" child element.
-->
<!ATTLIST entry key-ref CDATA #IMPLIED> <!--
A short-cut alternative to a child element "value".
-->
<!ATTLIST entry value CDATA #IMPLIED> <!--
A short-cut alternative to a child element "ref bean=".
-->
<!ATTLIST entry value-ref CDATA #IMPLIED> <!--
A key element can contain an inner bean, ref, value, or collection.
-->
<!ELEMENT key (
(bean | ref | idref | value | null | list | set | map | props)
)> <!--
Props elements differ from map elements in that values must be strings.
Props may be empty.
-->
<!ELEMENT props (
(prop)*
)> <!--
Element content is the string value of the property.
Note that whitespace is trimmed off to avoid unwanted whitespace
caused by typical XML formatting.
-->
<!ELEMENT prop (#PCDATA)> <!--
Each property element must specify its key.
-->
<!ATTLIST prop key CDATA #REQUIRED>
spring-beans.dtd文件内容文本
下面解释一下:
1.从上面文件的内容头部
<?xml version="1.0" encoding="UTF-8"?>
可以看出dtd文件也是一种xml文件,并且指定了编码方式
2.接着下面一段是注释,没啥好说的
3.紧接着的
<!--
The document root. A document can contain bean definitions only,
imports only, or a mixture of both (typically with imports first).
-->
<!ELEMENT beans (
description?,
(import | alias | bean)*
)>
a.定义了根元素,根元素的名称是beans,下面可以有description元素, ?是正则表达式,表示这个元素可有,可无,按字面意思就是这个元素是整个配置文件的描述
b.后面的import ,alias, bean 也是和description同级的兄弟元素, 后面的 * 表示每一个元素可以没有,也可以有多个.
4.接下来的
<!--
Default values for all bean definitions. Can be overridden at
the "bean" level. See those attribute definitions for details.
-->
<!ATTLIST beans default-lazy-init (true | false) "false">
<!ATTLIST beans default-autowire (no | byName | byType | constructor | autodetect) "no">
<!ATTLIST beans default-dependency-check (none | objects | simple | all) "none">
<!ATTLIST beans default-init-method CDATA #IMPLIED>
<!ATTLIST beans default-destroy-method CDATA #IMPLIED>
ATTLIST 这一看就是 attribute list的缩写吧,嗯,就是定义属性列表, 前面的beans 指的是当前的属性属于哪个元素, 后面的default-lazy-init, default-autowire 就是属性名称,()里面的值是当前属性的可选值 | 是或者的意思,后面带“”表示如果没有配置,这个属性使用的默认值。
这里default-init-method这个属性的值是 CDATA 类型的,CDATA 是一个数据类型CharactorData的缩写,也即初始化方法的值是个字符串,后面的 #IMPLITED 表示当前这个属性可以忽略,可配置,也可不配置 是 implementation ignored的缩写
自定义xml配置文件之dtd文件校验的更多相关文章
- Winform中自定义xml配置文件后对节点进行读取与写入
场景 Winform中自定义xml配置文件,并配置获取文件路径: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648 ...
- Winform中对自定义xml配置文件进行Xml节点的添加与删除
场景 Winform中自定义xml配置文件后对节点进行读取与写入: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10053213 ...
- Winform中自定义xml配置文件,并配置获取文件路径
场景 在Winform程序中,需要将一些配置项存到配置文件中,这时就需要自定义xml的配置文件格式.并在一些工具类中去获取配置文件的路径并加载其内容. 关注公众号霸道的程序猿获取编程相关电子书.教程推 ...
- 使用eclipse XML catalog绑定dtd文件
有时候我们想编辑struts或spring的xml配置文件的时候,输了“<”之后eclipse却没有提示关键字,这是因为eclipse需要到网络下载dtd文件而由于网络原因没下载下来所以无法提示 ...
- spring与jpa整合 简化persistence.xml配置文件 使用属性文件 数据源dbcp访问数据库
===========appliction.xml配置文件======================= <?xml version="1.0" encoding=" ...
- 【HTML/XML 9】XML中的DTD文件
导读:DTD是Document type definition(文档类型定义的缩写),是一套关于标记符的语法规则,它是XML文件的验证机制,数以XML文件的组成部分.XML文档是一种描述标记语言的语言 ...
- 2.xml约束技术----------dtd约束
1.xml的约束 (1)为什么需要定义约束了 比如现在定义一个person的xml文件,只想要这个文件里面保存人的信息,比如name age等,但是如果在xml文件中写了一个元素<猫>,发 ...
- mybatis逆向工程自动生成实体类、接口以及映射Mapper.xml配置文件
Mybatis的逆向工程非常简单,只要一个配置文件和一个Main方法就可以实现,下面以maven工程为例: (1)在pom.xml中引入依赖包 <dependency> <group ...
- struts.xml配置文件(package,namespace,action)
struts2.0 xml配置 struts.xml文件结构 struts.xml文件是整个Struts2框架的核心. struts.xml文件内定义了Struts2的系列Action,定义Actio ...
随机推荐
- thrift编译java的问题-(安装thrift0.8.0成功-编译mapkeeper.java成功)
上一次帖子说了thrift编译java出现错误,由于只用到cpp版的,就将此略过.但是老版本的ycsb不是很好用,于是决定以locall的方式编译mapkeeper供最新版ycsb使用.目前根据 ht ...
- 我的CSDN博客
从csdn搬过来的: csdn地址:http://blog.csdn.net/WR_technology
- Windows下Tesseract4.0识别与中文手写字体训练
一 . tesseract 4.0 安装及使用 1. tesseract 4.0 安装 安装包下载地址: http://digi.bib.uni-mannheim.de/tesseract/tesse ...
- P2383 狗哥玩木棒
题目背景 狗哥又趁着语文课干些无聊的事了... 题目描述 现给出一些木棒长度,那么狗哥能否用给出的木棒(木棒全用完)组成一个正方形呢? 输入输出格式 输入格式: 输入文件中的第一行是一个整数n表示测试 ...
- JVM介绍(一)
JVM是运行在操作系统之上的,它与硬件没有直接的交互 类装载器ClassLoader 负责加载class文件,class文件在文件开头有特定的文件标示,并且ClassLoader只负责class文件的 ...
- C语言之fileno()函数--获取已经打开的文件的文件描述符(小技巧)
open函数相关的: /* open 是系统调用 返回的是文件句柄*/ #include <sys/stat.h> #include <fcntl.h> int open(c ...
- 微信小程序把玩(三十)wx.request(object) API
这里通过干活集中营的API接口真实请求下数据.如果提示URL 域名不合法,请在 mp 后台配置后重试修改asdebug.js两行代码即可可看下面图 百牛信息技术bainiu.ltd整理发布于博客园 定 ...
- CS231n 2016 通关 第三章-Softmax 作业
在完成SVM作业的基础上,Softmax的作业相对比较轻松. 完成本作业需要熟悉与掌握的知识: cell 1 设置绘图默认参数 mport random import numpy as np from ...
- Bishops
题意: 给定一个 $n*n$ 的国际棋盘,求问在上面放 $K$ 个象的方案数. 解法: 首先可以发现黑格和白格互不干扰,这样我们可以将黑格,白格分别求出. 考虑 $f(i,j)$ 表示坐标化后考虑长度 ...
- linux中的条件变量
1 大家可能知道互斥量是线程程序中必须的工具了,但是也不能是万能的,就比如某个线程正在等待共享数据某个条件的发生,这个时候会发生什么呢.它就可能重复的尝试对互斥对象锁定和解锁来检查共享数据结构. 2 ...